]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Move most of pdns_stou to Pdns::stoi and Pdns::stou
authorFred Morcos <fred.morcos@open-xchange.com>
Mon, 7 Mar 2022 08:07:23 +0000 (09:07 +0100)
committerFred Morcos <fred.morcos@open-xchange.com>
Tue, 15 Mar 2022 09:05:49 +0000 (10:05 +0100)
This also creates the Pdns namespace and renames pdns_stou to Pdns::stou and separates
signed from unsigned conversion (Pdns::stoi and Pdns::stou) which are now implemented in
terms of Pdns::checked_conv and return a generic integer.

38 files changed:
modules/bindbackend/binddnssec.cc
modules/geoipbackend/geoipbackend.cc
modules/ldapbackend/ldapbackend.cc
modules/ldapbackend/native.cc
modules/pipebackend/pipebackend.cc
pdns/backends/gsql/gsqlbackend.cc
pdns/credentials.cc
pdns/decafsigners.cc
pdns/dnsbackend.cc
pdns/dnsdist.cc
pdns/dnsdistdist/dnsdist-nghttp2.cc
pdns/dnsparser.cc
pdns/dnsparser.hh
pdns/dnssecinfra.cc
pdns/dynloader.cc
pdns/iputils.cc
pdns/iputils.hh
pdns/misc.cc
pdns/misc.hh
pdns/opensslsigners.cc
pdns/packethandler.cc
pdns/pdnsutil.cc
pdns/pkcs11signers.cc
pdns/rcpgenerator.cc
pdns/rec_channel_rec.cc
pdns/rec_control.cc
pdns/recursordist/rec-main.cc
pdns/resolver.cc
pdns/sodiumsigners.cc
pdns/svc-records.cc
pdns/tcpreceiver.cc
pdns/test-ueberbackend_cc.cc
pdns/test-zoneparser_tng_cc.cc
pdns/ws-auth.cc
pdns/zone2json.cc
pdns/zone2ldap.cc
pdns/zone2sql.cc
pdns/zoneparser-tng.cc

index 9e8cbefa50d4223f0afb2e0b6c87f61d33034f20..8e56319538ef47f25ee3d2c19509b3fbdff756ce 100644 (file)
@@ -323,8 +323,8 @@ bool Bind2Backend::getDomainKeys(const DNSName& name, std::vector<KeyData>& keys
     SSqlStatement::row_t row;
     while (d_getDomainKeysQuery_stmt->hasNextRow()) {
       d_getDomainKeysQuery_stmt->nextRow(row);
-      kd.id = pdns_stou(row[0]);
-      kd.flags = pdns_stou(row[1]);
+      pdns::checked_stoi_into(kd.id, row[0]);
+      pdns::checked_stoi_into(kd.flags, row[1]);
       kd.active = (row[2] == "1");
       kd.published = (row[3] == "1");
       kd.content = row[4];
index 68abb293a05d1401a3f4331954149eb1e159fe46..078e7a893d95e9d5cf167369bdbf70d2bfc69acf 100644 (file)
@@ -923,10 +923,10 @@ bool GeoIPBackend::getDomainKeys(const DNSName& name, std::vector<DNSBackend::Ke
         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
           if (regexec(&reg, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
             DNSBackend::KeyData kd;
-            kd.id = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+            pdns::checked_stoi_into(kd.id, glob_result.gl_pathv[i] + regm[3].rm_so);
             kd.active = !strncmp(glob_result.gl_pathv[i] + regm[4].rm_so, "1", 1);
             kd.published = true;
-            kd.flags = pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so);
+            pdns::checked_stoi_into(kd.flags, glob_result.gl_pathv[i] + regm[2].rm_so);
             ifstream ifs(glob_result.gl_pathv[i]);
             ostringstream content;
             char buffer[1024];
@@ -968,7 +968,7 @@ bool GeoIPBackend::removeDomainKey(const DNSName& name, unsigned int id)
       if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
           if (regexec(&reg, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
-            unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+            auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
             if (kid == id) {
               if (unlink(glob_result.gl_pathv[i])) {
                 cerr << "Cannot delete key:" << strerror(errno) << endl;
@@ -1004,7 +1004,7 @@ bool GeoIPBackend::addDomainKey(const DNSName& name, const KeyData& key, int64_t
       if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
           if (regexec(&reg, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
-            unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+            auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
             if (kid >= nextid)
               nextid = kid + 1;
           }
@@ -1040,10 +1040,10 @@ bool GeoIPBackend::activateDomainKey(const DNSName& name, unsigned int id)
       if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
           if (regexec(&reg, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
-            unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+            auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
             if (kid == id && !strcmp(glob_result.gl_pathv[i] + regm[4].rm_so, "0")) {
               ostringstream newpath;
-              newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".1.key";
+              newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".1.key";
               if (rename(glob_result.gl_pathv[i], newpath.str().c_str())) {
                 cerr << "Cannot activate key: " << strerror(errno) << endl;
               }
@@ -1075,10 +1075,10 @@ bool GeoIPBackend::deactivateDomainKey(const DNSName& name, unsigned int id)
       if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
         for (size_t i = 0; i < glob_result.gl_pathc; i++) {
           if (regexec(&reg, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
-            unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+            auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
             if (kid == id && !strcmp(glob_result.gl_pathv[i] + regm[4].rm_so, "1")) {
               ostringstream newpath;
-              newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".0.key";
+              newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".0.key";
               if (rename(glob_result.gl_pathv[i], newpath.str().c_str())) {
                 cerr << "Cannot deactivate key: " << strerror(errno) << endl;
               }
index 175c719ff5b2a342289792683de51b2d630d8f6b..35e5e6f2fa169597ea1403dacaa0390aefd14a5b 100644 (file)
@@ -207,7 +207,7 @@ void LdapBackend::extract_entry_results(const DNSName& domain, const DNSResult&
             if (qtype2 != QType(local_result.qtype).toString())
               continue;
 
-            local_result.ttl = pdns_stou(rdata.substr(pos + 1));
+            pdns::checked_stoi_into(local_result.ttl, rdata.substr(pos + 1));
           }
         }
 
index 0b24a28411a98d05279291c5fdece105f05b044d..51b2e044b6b1c23bde1e04bea60c40880d4934cd 100644 (file)
@@ -372,12 +372,12 @@ bool LdapBackend::getDomainInfo(const DNSName& domain, DomainInfo& di, bool getS
     di.zone = DNSName(domain);
 
     if (result.count("PdnsDomainLastCheck") && !result["PdnsDomainLastCheck"].empty())
-      di.last_check = pdns_stou(result["PdnsDomainLastCheck"][0]);
+      pdns::checked_stoi_into(di.last_check, result["PdnsDomainLastCheck"][0]);
     else
       di.last_check = 0;
 
     if (result.count("PdnsDomainNotifiedSerial") && !result["PdnsDomainNotifiedSerial"].empty())
-      di.notified_serial = pdns_stou(result["PdnsDomainNotifiedSerial"][0]);
+      pdns::checked_stoi_into(di.notified_serial, result["PdnsDomainNotifiedSerial"][0]);
     else
       di.notified_serial = 0;
 
index 5773adcf0bccbfbbffc6f24914c3dd7436490ab3..0f3dcbf5a97e1fac4736e565ddc5be20a416763b 100644 (file)
@@ -315,8 +315,8 @@ bool PipeBackend::get(DNSResourceRecord& r)
         }
         r.qname = DNSName(parts[1 + extraFields]);
         r.qtype = parts[3 + extraFields];
-        r.ttl = pdns_stou(parts[4 + extraFields]);
-        r.domain_id = std::stoi(parts[5 + extraFields]);
+        pdns::checked_stoi_into(r.ttl, parts[4 + extraFields]);
+        pdns::checked_stoi_into(r.domain_id, parts[5 + extraFields]);
 
         if (r.qtype.getCode() != QType::MX && r.qtype.getCode() != QType::SRV) {
           r.content.clear();
index bc094a8055ea1493f27cb138c7b65ef926abf66c..dc9a31947848428a011797fe2396179f1b107597 100644 (file)
@@ -70,7 +70,7 @@ GSQLBackend::GSQLBackend(const string &mode, const string &suffix)
   d_InfoOfAllSlaveDomainsQuery=getArg("info-all-slaves-query");
   d_SuperMasterInfoQuery=getArg("supermaster-query");
   d_GetSuperMasterIPs=getArg("supermaster-name-to-ips");
-  d_AddSuperMaster=getArg("supermaster-add"); 
+  d_AddSuperMaster=getArg("supermaster-add");
   d_RemoveAutoPrimaryQuery=getArg("autoprimary-remove");
   d_ListAutoPrimariesQuery=getArg("list-autoprimaries");
   d_InsertZoneQuery=getArg("insert-zone-query");
@@ -110,7 +110,7 @@ GSQLBackend::GSQLBackend(const string &mode, const string &suffix)
   d_GetLastInsertedKeyIdQuery = getArg("get-last-inserted-key-id-query");
   d_ListDomainKeysQuery = getArg("list-domain-keys-query");
 
-  d_GetAllDomainMetadataQuery = getArg("get-all-domain-metadata-query");  
+  d_GetAllDomainMetadataQuery = getArg("get-all-domain-metadata-query");
   d_GetDomainMetadataQuery = getArg("get-domain-metadata-query");
   d_ClearDomainMetadataQuery = getArg("clear-domain-metadata-query");
   d_ClearDomainAllMetadataQuery = getArg("clear-domain-all-metadata-query");
@@ -315,7 +315,7 @@ bool GSQLBackend::getDomainInfo(const DNSName &domain, DomainInfo &di, bool getS
 
   ASSERT_ROW_COLUMNS("info-zone-query", d_result[0], 7);
 
-  di.id=pdns_stou(d_result[0][0]);
+  pdns::checked_stoi_into(di.id, d_result[0][0]);
   try {
     di.zone=DNSName(d_result[0][1]);
   } catch (...) {
@@ -329,8 +329,8 @@ bool GSQLBackend::getDomainInfo(const DNSName &domain, DomainInfo &di, bool getS
   stringtok(masters, d_result[0][2], " ,\t");
   for(const auto& m : masters)
     di.masters.emplace_back(m, 53);
-  di.last_check=pdns_stou(d_result[0][3]);
-  di.notified_serial = pdns_stou(d_result[0][4]);
+  pdns::checked_stoi_into(di.last_check, d_result[0][3]);
+  pdns::checked_stoi_into(di.notified_serial, d_result[0][4]);
   di.backend=this;
 
   di.serial = 0;
@@ -389,7 +389,7 @@ void GSQLBackend::getUnfreshSlaveInfos(vector<DomainInfo> *unfreshDomains)
     }
 
     try {
-      sd.id=pdns_stou(row[0]);
+      pdns::checked_stoi_into(sd.id, row[0]);
     } catch (const std::exception &e) {
       g_log<<Logger::Warning<<"Could not convert id ("<<row[0]<<") for domain '"<<sd.zone<<"' into an integer: "<<e.what()<<endl;
       continue;
@@ -410,7 +410,7 @@ void GSQLBackend::getUnfreshSlaveInfos(vector<DomainInfo> *unfreshDomains)
     }
 
     try {
-      sd.last_check=pdns_stou(row[3]);
+      pdns::checked_stoi_into(sd.last_check, row[3]);
     } catch (const std::exception &e) {
       g_log<<Logger::Warning<<"Could not convert last_check ("<<row[3]<<") for domain '"<<sd.zone<<"' into an integer: "<<e.what()<<endl;
       continue;
@@ -473,11 +473,11 @@ void GSQLBackend::getUpdatedMasters(vector<DomainInfo> *updatedDomains)
     stringtok( parts, d_result[n][3] );
 
     try {
-      uint32_t serial = parts.size() > 2 ? pdns_stou(parts[2]) : 0;
-      uint32_t notified_serial = pdns_stou( d_result[n][2] );
+      uint32_t serial = parts.size() > 2 ? pdns::checked_stoi<uint32_t>(parts[2]) : 0;
+      auto notified_serial = pdns::checked_stoi<uint32_t>(d_result[n][2]);
 
       if( serial != notified_serial ) {
-        di.id = pdns_stou( d_result[n][0] );
+        pdns::checked_stoi_into(di.id, d_result[n][0]);
         di.zone = DNSName( d_result[n][1] );
         di.serial = serial;
         di.notified_serial = notified_serial;
@@ -999,8 +999,8 @@ bool GSQLBackend::getDomainKeys(const DNSName& name, std::vector<KeyData>& keys)
       //~ for(const auto& val: row) {
         //~ cerr<<"'"<<val<<"'"<<endl;
       //~ }
-      kd.id = pdns_stou(row[0]);
-      kd.flags = pdns_stou(row[1]);
+      pdns::checked_stoi_into(kd.id, row[0]);
+      pdns::checked_stoi_into(kd.flags, row[1]);
       kd.active = row[2] == "1";
       kd.published = row[3] == "1";
       kd.content = row[4];
@@ -1453,7 +1453,7 @@ void GSQLBackend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, boo
       d_getAllDomainsQuery_stmt->nextRow(row);
       ASSERT_ROW_COLUMNS("get-all-domains-query", row, 8);
       DomainInfo di;
-      di.id = pdns_stou(row[0]);
+      pdns::checked_stoi_into(di.id, row[0]);
       try {
         di.zone = DNSName(row[1]);
       } catch (...) {
@@ -1495,8 +1495,8 @@ void GSQLBackend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, boo
       }
 
       try {
-        di.notified_serial = pdns_stou(row[5]);
-        di.last_check = pdns_stou(row[6]);
+        pdns::checked_stoi_into(di.notified_serial, row[5]);
+        pdns::checked_stoi_into(di.last_check, row[6]);
       } catch(...) {
         continue;
       }
@@ -1579,7 +1579,7 @@ bool GSQLBackend::feedRecord(const DNSResourceRecord &r, const DNSName &ordernam
   if (r.qtype == QType::MX || r.qtype == QType::SRV) {
     string::size_type pos = content.find_first_not_of("0123456789");
     if (pos != string::npos) {
-      prio=pdns_stou(content.substr(0,pos));
+      pdns::checked_stoi_into(prio, content.substr(0,pos));
       boost::erase_head(content, pos);
     }
     boost::trim_left(content);
@@ -1929,7 +1929,7 @@ void GSQLBackend::extractRecord(SSqlStatement::row_t& row, DNSResourceRecord& r)
   if (row[1].empty())
       r.ttl = defaultTTL;
   else
-      r.ttl=pdns_stou(row[1]);
+      pdns::checked_stoi_into(r.ttl, row[1]);
 
   if(!d_qname.empty())
     r.qname=d_qname;
@@ -1958,7 +1958,7 @@ void GSQLBackend::extractRecord(SSqlStatement::row_t& row, DNSResourceRecord& r)
 
   r.disabled = !row[5].empty() && row[5][0]=='1';
 
-  r.domain_id=pdns_stou(row[4]);
+  pdns::checked_stoi_into(r.domain_id, row[4]);
 
   if (row.size() > 8) {   // if column 8 exists, it holds an ordername
     if (!row.at(8).empty()) {
@@ -1975,10 +1975,10 @@ void GSQLBackend::extractRecord(SSqlStatement::row_t& row, DNSResourceRecord& r)
 
 void GSQLBackend::extractComment(SSqlStatement::row_t& row, Comment& comment)
 {
-  comment.domain_id = pdns_stou(row[0]);
+  pdns::checked_stoi_into(comment.domain_id, row[0]);
   comment.qname = DNSName(row[1]);
   comment.qtype = row[2];
-  comment.modified_at = pdns_stou(row[3]);
+  pdns::checked_stoi_into(comment.modified_at, row[3]);
   comment.account = std::move(row[4]);
   comment.content = std::move(row[5]);
 }
index d058a948ac1c0cc9b7b3519bc0748f10a155a1a2..3a7534f75705c657a5655cf1bd1bb416ef52bfb2 100644 (file)
@@ -246,14 +246,14 @@ static void parseHashed(const std::string& hash, std::string& salt, std::string&
   }
 
   try {
-    workFactor = pdns_stou(parameters.at(0).substr(3));
+    workFactor = pdns::checked_stoi<uint64_t>(parameters.at(0).substr(3));
     workFactor = static_cast<uint64_t>(1) << workFactor;
     if (workFactor > pwhash_max_work_factor) {
       throw std::runtime_error("Invalid work factor of " + std::to_string(workFactor) + " in hashed password string, maximum is " + std::to_string(pwhash_max_work_factor));
     }
 
-    parallelFactor = pdns_stou(parameters.at(1).substr(2));
-    blockSize = pdns_stou(parameters.at(2).substr(2));
+    parallelFactor = pdns::checked_stoi<uint64_t>(parameters.at(1).substr(2));
+    blockSize = pdns::checked_stoi<uint64_t>(parameters.at(2).substr(2));
 
     auto b64Salt = hash.substr(saltPos, saltEnd - saltPos);
     salt.reserve(pwhash_salt_size);
index 3b348e3c8409a6e1e13ed9e5ce522aed9fd9985c..93585815a9d9793f24942a7e7a27c489c51741a3 100644 (file)
@@ -81,7 +81,7 @@ void DecafED25519DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::m
     PrivateKey: ODIyNjAzODQ2MjgwODAxMjI2NDUxOTAyMDQxNDIyNjI=
   */
 
-  drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+  pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
   string privateKey = stormap["privatekey"];
 
   if (privateKey.length() != DECAF_EDDSA_25519_PRIVATE_BYTES)
@@ -217,7 +217,7 @@ void DecafED448DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::map
     PrivateKey: xZ+5Cgm463xugtkY5B0Jx6erFTXp13rYegst0qRtNsOYnaVpMx0Z/c5EiA9x8wWbDDct/U3FhYWA
   */
 
-  drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+  pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
   string privateKey = stormap["privatekey"];
 
   if (privateKey.length() != DECAF_EDDSA_448_PRIVATE_BYTES)
index 991ab6f3d119f1c62168e18d5c813d9dbb3e9d2b..f0ae76a896c0c950a126289a327c0f83fb207ff0 100644 (file)
@@ -347,11 +347,11 @@ void fillSOAData(const string &content, SOAData &data)
   try {
     data.nameserver = DNSName(parts.at(0));
     data.hostmaster = DNSName(parts.at(1));
-    data.serial = pdns_stou(parts.at(2).c_str());
-    data.refresh = pdns_stou(parts.at(3).c_str());
-    data.retry = pdns_stou(parts.at(4).c_str());
-    data.expire = pdns_stou(parts.at(5).c_str());
-    data.minimum = pdns_stou(parts.at(6).c_str());
+    pdns::checked_stoi_into(data.serial, parts.at(2));
+    pdns::checked_stoi_into(data.refresh, parts.at(3));
+    pdns::checked_stoi_into(data.retry, parts.at(4));
+    pdns::checked_stoi_into(data.expire, parts.at(5));
+    pdns::checked_stoi_into(data.minimum, parts.at(6));
   }
   catch(const std::out_of_range& oor) {
     throw PDNSException("Out of range exception parsing '" + content + "'");
index 24a5c5bd2bb6c50f5ab8ca5de087b0d9e69c2fa7..b3b01859591367d58c8942bab21a62d263b14ae8 100644 (file)
@@ -434,7 +434,7 @@ static bool applyRulesToResponse(LocalStateHolder<vector<DNSDistResponseRuleActi
         break;
         /* non-terminal actions follow */
       case DNSResponseAction::Action::Delay:
-        dr.delayMsec = static_cast<int>(pdns_stou(ruleresult)); // sorry
+        pdns::checked_stoi_into(dr.delayMsec, ruleresult); // sorry
         break;
       case DNSResponseAction::Action::None:
         break;
@@ -833,7 +833,7 @@ bool processRulesResult(const DNSAction::Action& action, DNSQuestion& dq, std::s
     break;
     /* non-terminal actions follow */
   case DNSAction::Action::Delay:
-    dq.delayMsec = static_cast<int>(pdns_stou(ruleresult)); // sorry
+    pdns::checked_stoi_into(dq.delayMsec, ruleresult); // sorry
     break;
   case DNSAction::Action::None:
     /* fall-through */
index 42ad2bc656fe503382ac1735233789d55bbf3a2f..573319a46ca03ae85321b03e04bfed2d3c0d2bb4 100644 (file)
@@ -753,7 +753,7 @@ int DoHConnectionToBackend::on_header_callback(nghttp2_session* session, const n
         return NGHTTP2_ERR_CALLBACK_FAILURE;
       }
       try {
-        stream->second.d_responseCode = pdns_stou(std::string(reinterpret_cast<const char*>(value), valuelen));
+        pdns::checked_stoi_into(stream->second.d_responseCode, std::string(reinterpret_cast<const char*>(value), valuelen));
       }
       catch (...) {
         vinfolog("Error parsing the status header for stream ID %d", frame->hd.stream_id);
index 145e4943ebbaaea6a3ca80a7f63bd0969d7bc460..2e21461208370b6a77c5cdf63f82cf8933c3cf75 100644 (file)
@@ -41,7 +41,7 @@ UnknownRecordContent::UnknownRecordContent(const string& zone)
   }
 
   const string& relevant = (parts.size() > 2) ? parts.at(2) : "";
-  unsigned int total = pdns_stou(parts.at(1));
+  auto total = pdns::checked_stoi<unsigned int>(parts.at(1));
   if (relevant.size() % 2 || (relevant.size() / 2) != total) {
     throw MOADNSException((boost::format("invalid unknown record length: size not equal to length field (%d != 2 * %d)") % relevant.size() % total).str());
   }
index b8b612dc627e98d12a5650a912b2bbcb366ef363..980e327e441ef4eb7dc4fceacd6ad6ec7f0463e1 100644 (file)
@@ -259,7 +259,7 @@ public:
       return iter->second.second;
 
     if (isUnknownType(name)) {
-      return (uint16_t)pdns_stou(name.substr(4));
+      return pdns::checked_stoi<uint16_t>(name.substr(4));
     }
 
     throw runtime_error("Unknown DNS type '"+name+"'");
index 8599058dda23b87aac0fcf432e9e399e2a506560..198bb791aab0e09a269c2b693579d8b7abd772cb 100644 (file)
@@ -123,7 +123,7 @@ std::unique_ptr<DNSCryptoKeyEngine> DNSCryptoKeyEngine::makeFromISCString(DNSKEY
       }
       else if (it->second == KeyTypes::numeric) {
         try {
-          unsigned int num = pdns_stou(value);
+          auto num = pdns::checked_stoi<unsigned int>(value);
           stormap[key] = std::to_string(num);
           if (key == "algorithm") {
             algorithm = num;
@@ -306,7 +306,7 @@ void DNSCryptoKeyEngine::testMakers(unsigned int algo, maker_t* creator, maker_t
       std::tie(key,value)=splitField(sline, ':');
       boost::trim(value);
       if(pdns_iequals(key,"algorithm")) {
-        algorithm = pdns_stou(value);
+        pdns::checked_stoi_into(algorithm, value);
         stormap["algorithm"]=std::to_string(algorithm);
         continue;
       } else if (pdns_iequals(key,"pin")) {
index 877d445354ba8461fb153d97bc4d090070ab8476..e04f84b0eefdea9374ba64133a2448ac3e532295 100644 (file)
@@ -114,7 +114,7 @@ int main(int argc, char **argv)
     else {
       uint16_t port;
       try {
-        port = static_cast<uint16_t>(pdns_stou(::arg()["remote-port"]));
+        pdns::checked_stoi_into(port, ::arg()["remote-port"]);
       }
       catch(...) {
         cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
index 449a6804247d214e9209525598cf3bac9b8877bd..42522bdebaa719de11757fb301c116efd5d88ea6 100644 (file)
@@ -506,7 +506,7 @@ ComboAddress parseIPAndPort(const std::string& input, uint16_t port)
 {
   if (input[0] == '[') { // case 1
     auto both = splitField(input.substr(1), ']');
-    return ComboAddress(both.first, both.second.empty() ? port : static_cast<uint16_t>(pdns_stou(both.second.substr(1))));
+    return ComboAddress(both.first, both.second.empty() ? port : pdns::checked_stoi<uint16_t>(both.second.substr(1)));
   }
 
   string::size_type count = 0;
@@ -527,7 +527,7 @@ ComboAddress parseIPAndPort(const std::string& input, uint16_t port)
     both.first = input.substr(0, cpos);
     both.second = input.substr(cpos + 1);
 
-    uint16_t newport = static_cast<uint16_t>(pdns_stou(both.second));
+    auto newport = pdns::checked_stoi<uint16_t>(both.second);
     return ComboAddress(both.first, newport);
   }
   default: // case 4
index ab948b0298a6cce0f33365e00786cfe8458349e2..225532cb7c8ef1ceb978970ba80373d7c45018e7 100644 (file)
@@ -498,7 +498,7 @@ public:
     d_network = makeComboAddress(split.first);
 
     if (!split.second.empty()) {
-      setBits(static_cast<uint8_t>(pdns_stou(split.second)));
+      setBits(pdns::checked_stoi<uint8_t>(split.second));
     }
     else if (d_network.sin4.sin_family == AF_INET) {
       setBits(32);
index bb62fb40df94843e7c92a56057a08a5625f31341..38a30eaae20c2322e59046777bf44b626b1377f2 100644 (file)
@@ -290,7 +290,7 @@ static void parseService4(const string& descr, ServiceTuple& st)
   }
   st.host = parts[0];
   if (parts.size() > 1) {
-    st.port = pdns_stou(parts[1]);
+    pdns::checked_stoi_into(st.port, parts[1]);
   }
 }
 
@@ -303,7 +303,7 @@ static void parseService6(const string& descr, ServiceTuple& st)
 
   st.host = descr.substr(1, pos - 1);
   if (pos + 2 < descr.length()) {
-    st.port = pdns_stou(descr.substr(pos + 2));
+    pdns::checked_stoi_into(st.port, descr.substr(pos + 2));
   }
 }
 
@@ -693,7 +693,7 @@ int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
       if (pos + 2 > addr.size() || addr[pos+1]!=':')
         return -1;
       try {
-        port = pdns_stou(addr.substr(pos+2));
+        pdns::checked_stoi_into(port, addr.substr(pos + 2));
         portSet = true;
       }
       catch(const std::out_of_range&) {
@@ -1357,7 +1357,7 @@ uint64_t getOpenFileDescriptors(const std::string&)
   while((entry = readdir(dirhdl))) {
     uint32_t num;
     try {
-      num = pdns_stou(entry->d_name);
+      pdns::checked_stoi_into(num, entry->d_name);
     } catch (...) {
       continue; // was not a number.
     }
@@ -1513,28 +1513,6 @@ gid_t strToGID(const string &str)
   return result;
 }
 
-unsigned int pdns_stou(const std::string& str, size_t* idx, int base)
-{
-  if (str.empty()) {
-    return 0; // compatibility
-  }
-
-  unsigned long result;
-  try {
-    result = std::stoul(str, idx, base);
-  }
-  catch (std::invalid_argument& e) {
-    throw std::invalid_argument(string(e.what()) + "; (invalid argument during std::stoul); data was \"" + str + "\"");
-  }
-  catch (std::out_of_range& e) {
-    throw std::out_of_range(string(e.what()) + "; (out of range during std::stoul); data was \"" + str + "\"");
-  }
-  if (result > std::numeric_limits<unsigned int>::max()) {
-    throw std::out_of_range("stoul returned result out of unsigned int range; data was \"" + str + "\"");
-  }
-  return static_cast<unsigned int>(result);
-}
-
 bool isSettingThreadCPUAffinitySupported()
 {
 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
index 595f119b4321d9753780feca794672d469134145..05aa87d4e72b37e972f9402a3300392a70c2926e 100644 (file)
@@ -617,7 +617,105 @@ double DiffTime(const struct timeval& first, const struct timeval& second);
 uid_t strToUID(const string &str);
 gid_t strToGID(const string &str);
 
-unsigned int pdns_stou(const std::string& str, size_t* idx = 0, int base = 10);
+namespace pdns
+{
+/**
+ * \brief Does a checked conversion from one integer type to another.
+ *
+ * \warning The source type `F` and target type `T` must have the same
+ * signedness, otherwise a compilation error is thrown.
+ *
+ * \exception std::out_of_range Thrown if the source value does not fit
+ * in the target type.
+ *
+ * \param[in] from The source value of type `F`.
+ *
+ * \return The target value of type `T`.
+ */
+template <typename T, typename F>
+auto checked_conv(F from) -> T
+{
+  static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer");
+  static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer");
+  static_assert((std::numeric_limits<F>::is_signed && std::numeric_limits<T>::is_signed) || (!std::numeric_limits<F>::is_signed && !std::numeric_limits<T>::is_signed),
+                "checked_conv: The `T` and `F` types must either both be signed or unsigned");
+
+  if (from < std::numeric_limits<T>::min() || from > std::numeric_limits<T>::max()) {
+    throw std::out_of_range("checked_conv: conversion from value that is out of range for target type");
+  }
+
+  return static_cast<T>(from);
+}
+
+/**
+ * \brief Performs a conversion from `std::string&` to integer.
+ *
+ * This function internally calls `std::stoll` and `std::stoull` to do
+ * the conversion from `std::string&` and calls `pdns::checked_conv` to
+ * do the checked conversion from `long long`/`unsigned long long` to
+ * `T`.
+ *
+ * \warning The target type `T` must be an integer, otherwise a
+ * compilation error is thrown.
+ *
+ * \exception std:stoll Throws what std::stoll throws.
+ *
+ * \exception std::stoull Throws what std::stoull throws.
+ *
+ * \exception pdns::checked_conv Throws what pdns::checked_conv throws.
+ *
+ * \param[in] str The input string to be converted.
+ *
+ * \param[in] idx Location to store the index at which processing
+ * stopped.
+ *
+ * \param[in] base The numerical base for conversion.
+ *
+ * \return `str` converted to integer `T`, or 0 if `str` is empty.
+ */
+template <typename T>
+auto checked_stoi(const std::string& str, size_t* idx = nullptr, int base = 10) -> T
+{
+  static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer");
+
+  if (str.empty()) {
+    return 0; // compatibility
+  }
+
+  if constexpr (std::is_unsigned_v<T>) {
+    return pdns::checked_conv<T>(std::stoull(str, idx, base));
+  }
+  else {
+    return pdns::checked_conv<T>(std::stoll(str, idx, base));
+  }
+}
+
+/**
+ * \brief Performs a conversion from `std::string&` to integer.
+ *
+ * This function internally calls `pdns::checked_stoi` and stores its
+ * result in `out`.
+ *
+ * \exception pdns::checked_stoi Throws what pdns::checked_stoi throws.
+ *
+ * \param[out] out `str` converted to integer `T`, or 0 if `str` is
+ * empty.
+ *
+ * \param[in] str The input string to be converted.
+ *
+ * \param[in] idx Location to store the index at which processing
+ * stopped.
+ *
+ * \param[in] base The numerical base for conversion.
+ *
+ * \return `str` converted to integer `T`, or 0 if `str` is empty.
+ */
+template <typename T>
+auto checked_stoi_into(T& out, const std::string& str, size_t* idx = nullptr, int base = 10)
+{
+  out = checked_stoi<T>(str, idx, base);
+}
+}
 
 bool isSettingThreadCPUAffinitySupported();
 int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
index 7138f9705285caaac23eb793a9f388c725972163..2b7a863d3c146bffde6bdd6e98469ca2ac9012b9 100644 (file)
@@ -459,7 +459,7 @@ void OpenSSLRSADNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::map
   auto dmq1 = parse(stormap, "exponent2");
   auto iqmp = parse(stormap, "coefficient");
 
-  drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+  pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
 
   if (drc.d_algorithm != d_algorithm) {
     throw runtime_error(getName() + " tried to feed an algorithm " + std::to_string(drc.d_algorithm) + " to a " + std::to_string(d_algorithm) + " key");
index 59e205c27c79f6639f8ffd4bf0823d062ab691a5..efb5f07936866f1076016c819be62e58af0dad7d 100644 (file)
@@ -229,7 +229,7 @@ bool PacketHandler::addCDS(DNSPacket& p, std::unique_ptr<DNSPacket>& r)
       continue;
     }
     for(auto const &digestAlgo : digestAlgos){
-      rr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(p.qdomain, value.first.getDNSKEY(), pdns_stou(digestAlgo)));
+      rr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(p.qdomain, value.first.getDNSKEY(), pdns::checked_stoi<uint8_t>(digestAlgo)));
       r->addRecord(DNSZoneRecord(rr));
       haveOne=true;
     }
index a64abd0e5c6495234bf8bec8b59b1d1620623131..0c343c7652b12aeda393c4ac05815e1c13fb1aee 100644 (file)
@@ -2490,7 +2490,7 @@ try
       cerr << "Syntax: pdnsutil test-algorithm algonum"<<endl;
       return 0;
     }
-    if (testAlgorithm(pdns_stou(cmds.at(1))))
+    if (testAlgorithm(pdns::checked_stoi<int>(cmds.at(1))))
       return 0;
     return 1;
   }
@@ -2587,7 +2587,7 @@ try
     uint64_t workFactor = CredentialsHolder::s_defaultWorkFactor;
     if (cmds.size() > 1) {
       try {
-        workFactor = pdns_stou(cmds.at(1));
+        pdns::checked_stoi_into(workFactor, cmds.at(1));
       }
       catch (const std::exception& e) {
         cerr<<"Unable to parse the supplied work factor: "<<e.what()<<endl;
@@ -2692,7 +2692,7 @@ try
       cerr << "Syntax: pdnsutil test-speed numcores [signing-server]"<<endl;
       return 0;
     }
-    testSpeed(dk, DNSName(cmds.at(1)), (cmds.size() > 3) ? cmds.at(3) : "", pdns_stou(cmds.at(2)));
+    testSpeed(dk, DNSName(cmds.at(1)), (cmds.size() > 3) ? cmds.at(3) : "", pdns::checked_stoi<int>(cmds.at(2)));
   }
   else if (cmds.at(0) == "verify-crypto") {
     if(cmds.size() != 2) {
@@ -2734,7 +2734,7 @@ try
       return 0;
     }
     DNSName zone(cmds.at(1));
-    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
     if(!id)
     {
       cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
@@ -2758,7 +2758,7 @@ try
       return 0;
     }
     DNSName zone(cmds.at(1));
-    unsigned int id = pdns_stou(cmds.at(2));
+    auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
     if(!id)
     {
       cerr<<"Invalid KEY-ID"<<endl;
@@ -2782,7 +2782,7 @@ try
       return 0;
     }
     DNSName zone(cmds.at(1));
-    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
     if(!id)
     {
       cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
@@ -2806,7 +2806,7 @@ try
       return 0;
     }
     DNSName zone(cmds.at(1));
-    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+    unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
     if(!id)
     {
       cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
@@ -2876,8 +2876,8 @@ try
       else if (pdns_iequals(cmds.at(n), "unpublished")) {
         published = false;
       }
-      else if (pdns_stou(cmds.at(n))) {
-        bits = pdns_stou(cmds.at(n));
+      else if (pdns::checked_stoi<int>(cmds.at(n)) != 0) {
+        pdns::checked_stoi_into(bits, cmds.at(n));
       }
       else {
         cerr << "Unknown algorithm, key flag or size '" << cmds.at(n) << "'" << endl;
@@ -2907,7 +2907,7 @@ try
       return 0;
     }
     DNSName zone(cmds.at(1));
-    unsigned int id = pdns_stou(cmds.at(2));
+    auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
     if (!dk.removeKey(zone, id)) {
        cerr<<"Cannot remove key " << id << " from " << zone <<endl;
       return 1;
@@ -3265,7 +3265,7 @@ try
     }
 
     string zone = cmds.at(1);
-    unsigned int id = pdns_stou(cmds.at(2));
+    auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
     DNSSECPrivateKey dpk=dk.getKeyById(DNSName(zone), id);
     cout << dpk.getKey()->convertToISC() <<endl;
   }
@@ -3302,7 +3302,7 @@ try
     }
     dpk.setKey(key);
 
-    dpk.d_algorithm = pdns_stou(cmds.at(3));
+    pdns::checked_stoi_into(dpk.d_algorithm, cmds.at(3));
 
     if(dpk.d_algorithm == DNSSECKeeper::RSASHA1NSEC3SHA1)
       dpk.d_algorithm = DNSSECKeeper::RSASHA1;
@@ -3393,7 +3393,7 @@ try
     }
 
     DNSName zone(cmds.at(1));
-    unsigned int id = pdns_stou(cmds.at(2));
+    auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
     DNSSECPrivateKey dpk=dk.getKeyById(zone, id);
     cout << zone<<" IN DNSKEY "<<dpk.getDNSKEY().getZoneRepresentation() <<endl;
   }
@@ -3422,8 +3422,8 @@ try
       else if ((tmp_algo = DNSSECKeeper::shorthand2algorithm(cmds.at(n))) > 0) {
         algorithm = tmp_algo;
       }
-      else if (pdns_stou(cmds.at(n)))
-        bits = pdns_stou(cmds.at(n));
+      else if (pdns::checked_stoi<int>(cmds.at(n)) != 0)
+        pdns::checked_stoi_into(bits, cmds.at(n));
       else {
         cerr << "Unknown algorithm, key flag or size '" << cmds.at(n) << "'" << endl;
         return 0;
@@ -3779,7 +3779,7 @@ try
         return 1;
       }
 
-      id = pdns_stou(cmds.at(3));
+      pdns::checked_stoi_into(id, cmds.at(3));
       std::vector<DNSBackend::KeyData> keys;
       if (!B.getDomainKeys(zone, keys)) {
         cerr << "No keys found for zone " << zone << std::endl;
@@ -3801,7 +3801,7 @@ try
         return 1;
       }
       if (cmds.size() > 4) {
-        bits = pdns_stou(cmds.at(4));
+        pdns::checked_stoi_into(bits, cmds.at(4));
       }
       if (bits < 1) {
         cerr << "Invalid bit size " << bits << "given, must be positive integer";
index 3024fc435a726c76319a1935009600d9ca684316..2ea2f68944206889d487acf7118f18220ab7046e 100644 (file)
@@ -327,7 +327,7 @@ class Pkcs11Token {
     }
 
   public:
-    Pkcs11Token(const std::shared_ptr<LockGuarded<Pkcs11Slot>>& slot, const std::string& label, const std::string& pub_label); 
+    Pkcs11Token(const std::shared_ptr<LockGuarded<Pkcs11Slot>>& slot, const std::string& label, const std::string& pub_label);
     ~Pkcs11Token();
 
     bool Login(const std::string& pin) {
@@ -984,7 +984,7 @@ DNSCryptoKeyEngine::storvector_t PKCS11DNSCryptoKeyEngine::convertToISCVector()
 };
 
 void PKCS11DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, stormap_t& stormap) {
-  drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+  pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
   d_module = stormap["engine"];
   d_slot_id = stormap["slot"];
   boost::trim(d_slot_id);
index 6340b55b7a344936dd4999bb1d492213f01fc5eb..9204773d379a51de05a65ce16af24797bf73ca6d 100644 (file)
@@ -93,8 +93,8 @@ void RecordTextReader::xfr32BitInt(uint32_t &val)
     throw RecordTextException("expected digits at position "+std::to_string(d_pos)+" in '"+d_string+"'");
 
   size_t pos;
-  val=pdns_stou(d_string.c_str()+d_pos, &pos);
+  val = pdns::checked_stoi<uint32_t>(d_string.c_str() + d_pos, &pos);
+
   d_pos += pos;
 }
 
index b5d41b77dd5192ad695df85c7f0b8abc13d0aa45..8ed79f3b10a820ed4145d7e2e1e34e14c414f28f 100644 (file)
@@ -809,7 +809,7 @@ static string setMinimumTTL(T begin, T end)
   if (end - begin != 1)
     return "Need to supply new minimum TTL number\n";
   try {
-    SyncRes::s_minimumTTL = pdns_stou(*begin);
+    pdns::checked_stoi_into(SyncRes::s_minimumTTL, *begin);
     return "New minimum TTL: " + std::to_string(SyncRes::s_minimumTTL) + "\n";
   }
   catch (const std::exception& e) {
@@ -823,7 +823,7 @@ static string setMinimumECSTTL(T begin, T end)
   if (end - begin != 1)
     return "Need to supply new ECS minimum TTL number\n";
   try {
-    SyncRes::s_minimumECSTTL = pdns_stou(*begin);
+    pdns::checked_stoi_into(SyncRes::s_minimumECSTTL, *begin);
     return "New minimum ECS TTL: " + std::to_string(SyncRes::s_minimumECSTTL) + "\n";
   }
   catch (const std::exception& e) {
@@ -837,7 +837,7 @@ static string setMaxCacheEntries(T begin, T end)
   if (end - begin != 1)
     return "Need to supply new cache size\n";
   try {
-    g_maxCacheEntries = pdns_stou(*begin);
+    g_maxCacheEntries = pdns::checked_stoi<uint32_t>(*begin);
     return "New max cache entries: " + std::to_string(g_maxCacheEntries) + "\n";
   }
   catch (const std::exception& e) {
@@ -851,7 +851,7 @@ static string setMaxPacketCacheEntries(T begin, T end)
   if (end - begin != 1)
     return "Need to supply new packet cache size\n";
   try {
-    g_maxPacketCacheEntries = pdns_stou(*begin);
+    g_maxPacketCacheEntries = pdns::checked_stoi<uint32_t>(*begin);
     return "New max packetcache entries: " + std::to_string(g_maxPacketCacheEntries) + "\n";
   }
   catch (const std::exception& e) {
@@ -1865,7 +1865,7 @@ static string setEventTracing(T begin, T end)
     return "No event trace enabled value specified\n";
   }
   try {
-    SyncRes::s_event_trace_enabled = pdns_stou(*begin);
+    pdns::checked_stoi_into(SyncRes::s_event_trace_enabled, *begin);
     return "New event trace enabled value: " + std::to_string(SyncRes::s_event_trace_enabled) + "\n";
   }
   catch (const std::exception& e) {
index 75d99c9813c61d221454374ecd61a6ea590222cd..fdb68179026ebe89b6aecc10eba8893cfdc8eb84 100644 (file)
@@ -114,7 +114,7 @@ int main(int argc, char** argv)
       uint64_t workFactor = CredentialsHolder::s_defaultWorkFactor;
       if (commands.size() > 1) {
         try {
-          workFactor = pdns_stou(commands.at(1));
+          pdns::checked_stoi_into(workFactor, commands.at(1));
         }
         catch (const std::exception& e) {
           cerr << "Unable to parse the supplied work factor: " << e.what() << endl;
index cd1116d75f0338bed9aeb42ef8ca49a0bb4ad121..3ade92299f868e62c92e580802567442ea44a267 100644 (file)
@@ -132,7 +132,7 @@ static std::map<unsigned int, std::set<int>> parseCPUMap()
       boost::trim(headers.first);
       boost::trim(headers.second);
 
-      unsigned int threadId = pdns_stou(headers.first);
+      auto threadId = pdns::checked_stoi<unsigned int>(headers.first);
       std::vector<std::string> cpus;
 
       stringtok(cpus, headers.second, ",");
index 065d8226bf031ca91020e2e14bb7abac6d0bec70..d09976108d9fc6c1cf0b6e19f3fe3bdcb17f98de 100644 (file)
@@ -361,7 +361,7 @@ void Resolver::getSoaSerial(const ComboAddress& ipport, const DNSName &domain, u
     throw ResolverException("Query to '" + ipport.toLogString() + "' for SOA of '" + domain.toLogString() + "' produced an unparseable response");
 
   try {
-    *serial=pdns_stou(parts[2]);
+    *serial = pdns::checked_stoi<uint32_t>(parts[2]);
   }
   catch(const std::out_of_range& oor) {
     throw ResolverException("Query to '" + ipport.toLogString() + "' for SOA of '" + domain.toLogString() + "' produced an unparseable serial");
index ee142a6c1c01962d8cad4e3c7e8fd93b34459144..f50c02e7881d508b067269ba930a1ecf66c4ce94 100644 (file)
@@ -71,7 +71,7 @@ void SodiumED25519DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::
     PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ=
   */
 
-  drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+  pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
   string privateKey = stormap["privatekey"];
 
   if (privateKey.length() != crypto_sign_ed25519_SEEDBYTES)
index 666064a49bc8344bcb224a54706f8dd7e378cce2..73b112f470249089b7faa9d28d8ef7482eeb9109 100644 (file)
@@ -47,7 +47,7 @@ SvcParam::SvcParamKey SvcParam::keyFromString(const std::string& k, bool &generi
   if (k.substr(0, 3) == "key") {
     try {
       generic = true;
-      return SvcParam::SvcParamKey(pdns_stou(k.substr(3)));
+      return SvcParam::SvcParamKey(pdns::checked_stoi<uint16_t>(k.substr(3)));
     }
     catch (...) {
     }
index 71c00d633a50a48c2f98ec08181f199a4d33dd85..30cc461096555a16a7d7664574bba03dafc771f1 100644 (file)
@@ -707,7 +707,7 @@ int TCPNameserver::doAXFR(const DNSName &target, std::unique_ptr<DNSPacket>& q,
             zrrs.push_back(zrr);
           } else {
             for(auto const &digestAlgo : digestAlgos) {
-              zrr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(target, value.first.getDNSKEY(), pdns_stou(digestAlgo)));
+              zrr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(target, value.first.getDNSKEY(), pdns::checked_stoi<uint8_t>(digestAlgo)));
               zrrs.push_back(zrr);
             }
           }
@@ -1100,7 +1100,7 @@ int TCPNameserver::doIXFR(std::unique_ptr<DNSPacket>& q, int outsock)
       stringtok(parts, rr->d_content->getZoneRepresentation());
       if (parts.size() >= 3) {
         try {
-          serial=pdns_stou(parts[2]);
+          pdns::checked_stoi_into(serial, parts[2]);
         }
         catch(const std::out_of_range& oor) {
           g_log<<Logger::Warning<<logPrefix<<"invalid serial in IXFR query"<<endl;
index ea8f97f50e1f2ff8e8fb577c36cf00437d834908..dc6c147bad2ade3f38343c0f2de5747439483d13 100644 (file)
@@ -99,7 +99,8 @@ public:
     > MetaDataStorage;
 
   // Initialize our backend ID from the suffix, skipping the '-' that DNSBackend adds there
-  SimpleBackend(const std::string& suffix): d_suffix(suffix), d_backendId(pdns_stou(suffix.substr(1)))
+  SimpleBackend(const std::string& suffix) :
+    d_suffix(suffix), d_backendId(pdns::checked_stoi<decltype(d_backendId)>(suffix.substr(1)))
   {
   }
 
index 2b579d5c69663d6e8441041c3e7b9c0e61df14b5..78f273162fc889a5bfda7512fd598a283fa71a4a 100644 (file)
@@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(test_tng_record_types) {
     unsigned int ttl;
     std::getline(ifs, host, ' ');
     std::getline(ifs, type, ' ');
-    ttl = pdns_stou(type);
+    pdns::checked_stoi_into(ttl, type);
     std::getline(ifs, type, ' ');
     std::getline(ifs, type, ' ');
     std::getline(ifs, data, '\n');
index e3c9274e7f4a00ab2f7e390615626f0c995bb8e3..883cb0618de301d7374a330ee33c4942fe72fba3 100644 (file)
@@ -1124,7 +1124,7 @@ static void apiZoneCryptokeysGET(const DNSName& zonename, int inquireKeyId, Http
 
     std::set<unsigned int> CDSalgos;
     for(auto const &digestAlgo : digestAlgos) {
-      CDSalgos.insert(pdns_stou(digestAlgo));
+      CDSalgos.insert(pdns::checked_stoi<unsigned int>(digestAlgo));
     }
 
     if (value.second.keyType == DNSSECKeeper::KSK || value.second.keyType == DNSSECKeeper::CSK) {
index b365982b188eb858e3db85087a2ae822bb54f6b1..788f9e00124a9f811920577fa3f087f6df9d9523 100644 (file)
@@ -59,9 +59,9 @@ static Json::object emitRecord(const string& zoneName, const DNSName &DNSqname,
   string retval;
   g_numRecords++;
   string content(ocontent);
-  if(qtype == "MX" || qtype == "SRV") { 
-    prio=pdns_stou(content);
-    
+  if(qtype == "MX" || qtype == "SRV") {
+    pdns::checked_stoi_into(prio, content);
+
     string::size_type pos = content.find_first_not_of("0123456789");
     if(pos != string::npos)
       boost::erase_head(content, pos);
index 28039bf48e370f3c270a0561401cc5581e26f632..38c3b7e72808be1e262783fdbe0ed1c7d36c40a4 100644 (file)
@@ -294,7 +294,7 @@ int main( int argc, char* argv[] )
                 }
 
                 if ( !args["domainid"].empty() )
-                        g_domainid = pdns_stou( args["domainid"] );
+                        pdns::checked_stoi_into(g_domainid, args["domainid"]);
                 else
                         g_domainid = 1;
 
index d6c1c5e32abe6c03e85146d91f3581ae773fccdb..d6e61b4f89bf05b5913db3e6667b4623c4721a57 100644 (file)
@@ -162,8 +162,8 @@ static void emitRecord(const DNSName& zoneName, const DNSName &DNSqname, const s
     return; // NSECs do not go in the database
 
   if((qtype == "MX" || qtype == "SRV")) {
-    prio=pdns_stou(content);
-    
+    pdns::checked_stoi_into(prio, content);
+
     string::size_type pos = content.find_first_not_of("0123456789");
     if(pos != string::npos)
       boost::erase_head(content, pos);
index 9c6cc2801ad65dce2a6c6b8dd47ecfff91f2cdaf..d9c1594763f7bb388ae12cc2261a0c39166a8d31 100644 (file)
@@ -129,7 +129,7 @@ unsigned int ZoneParserTNG::makeTTLFromZone(const string& str)
 
   unsigned int val;
   try {
-    val=pdns_stou(str);
+    pdns::checked_stoi_into(val, str);
   }
   catch (const std::out_of_range& oor) {
     throw PDNSException("Unable to parse time specification '"+str+"' "+getLineOfFile());