]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
DNSName: Don't call strlen() when the length is already known 8792/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 30 Jan 2020 14:11:53 +0000 (15:11 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 7 Feb 2020 14:31:56 +0000 (15:31 +0100)
pdns/dnslabeltext.rl
pdns/dnsname.cc
pdns/dnsname.hh
pdns/dnswriter.hh

index fe64966fd0cacf8f67a83dbfc3f680d112df850c..826f666845bf3efcbdac60b737757686ce268b0c 100644 (file)
@@ -83,7 +83,7 @@ vector<string> segmentDNSText(const string& input )
 };
 
 
-DNSName::string_t segmentDNSNameRaw(const char* realinput)
+DNSName::string_t segmentDNSNameRaw(const char* realinput, size_t inputlen)
 {
 %%{
         machine dnsnameraw;
@@ -100,7 +100,6 @@ DNSName::string_t segmentDNSNameRaw(const char* realinput)
           return ret;
         }
 
-        unsigned int inputlen=strlen(realinput);
         ret.reserve(inputlen+1);
 
         const char *p = realinput, *pe = realinput + inputlen;
index b2936ec757ea68834e33a287c5a5cb3387b70276..c262d282023b6362f19bc86f8bcfe517b38ee1b7 100644 (file)
@@ -41,7 +41,7 @@ std::ostream & operator<<(std::ostream &os, const DNSName& d)
   return os <<d.toLogString();
 }
 
-DNSName::DNSName(const char* p)
+DNSName::DNSName(const char* p, size_t length)
 {
   if(p[0]==0 || (p[0]=='.' && p[1]==0)) {
     d_storage.assign(1, (char)0);
@@ -49,7 +49,7 @@ DNSName::DNSName(const char* p)
     if(!strchr(p, '\\')) {
       unsigned char lenpos=0;
       unsigned char labellen=0;
-      size_t plen=strlen(p);
+      size_t plen=length;
       const char* const pbegin=p, *pend=p+plen;
       d_storage.reserve(plen+1);
       for(auto iter = pbegin; iter != pend; ) {
@@ -76,7 +76,7 @@ DNSName::DNSName(const char* p)
       d_storage.append(1, (char)0);
     }
     else {
-      d_storage=segmentDNSNameRaw(p); 
+      d_storage=segmentDNSNameRaw(p, length);
       if(d_storage.size() > 255) {
         throw std::range_error("name too long");
       }
index 5028273070ba99ad9fa2dc3b1240beb6e57eef53..ee04ea610a400750adf6586a4399e74dfb06c30d 100644 (file)
@@ -62,8 +62,9 @@ class DNSName
 {
 public:
   DNSName()  {}          //!< Constructs an *empty* DNSName, NOT the root!
-  explicit DNSName(const char* p);      //!< Constructs from a human formatted, escaped presentation
-  explicit DNSName(const std::string& str) : DNSName(str.c_str()) {}; //!< Constructs from a human formatted, escaped presentation
+  explicit DNSName(const char* p): DNSName(p, strlen(p)) {} //!< Constructs from a human formatted, escaped presentation
+  explicit DNSName(const char* p, size_t len);      //!< Constructs from a human formatted, escaped presentation
+  explicit DNSName(const std::string& str) : DNSName(str.c_str(), str.length()) {}; //!< Constructs from a human formatted, escaped presentation
   DNSName(const char* p, int len, int offset, bool uncompress, uint16_t* qtype=nullptr, uint16_t* qclass=nullptr, unsigned int* consumed=nullptr, uint16_t minOffset=0); //!< Construct from a DNS Packet, taking the first question if offset=12. If supplied, consumed is set to the number of bytes consumed from the packet, which will not be equal to the wire length of the resulting name in case of compression.
   
   bool isPartOf(const DNSName& rhs) const;   //!< Are we part of the rhs name?
@@ -468,7 +469,7 @@ namespace std {
     };
 }
 
-DNSName::string_t segmentDNSNameRaw(const char* input); // from ragel
+DNSName::string_t segmentDNSNameRaw(const char* input, size_t inputlen); // from ragel
 bool DNSName::operator==(const DNSName& rhs) const
 {
   if(rhs.empty() != empty() || rhs.d_storage.size() != d_storage.size())
index 16048f16e7dca16fc52031338198f1f9b793afa7..fbfcd4739ad35a8f0f2bfaf9887b9c581e04a48d 100644 (file)
@@ -173,5 +173,4 @@ private:
 typedef vector<pair<string::size_type, string::size_type> > labelparts_t;
 // bool labeltokUnescape(labelparts_t& parts, const DNSName& label);
 std::vector<string> segmentDNSText(const string& text); // from dnslabeltext.rl
-std::deque<string> segmentDNSName(const string& input ); // from dnslabeltext.rl
 #endif