]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Move const maxDNSNameLength to class, and use it where appropriate
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 26 Sep 2022 09:07:24 +0000 (11:07 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 26 Sep 2022 09:07:24 +0000 (11:07 +0200)
pdns/dnsname.cc
pdns/dnsname.hh

index 544197551a5847b60b3f431940f2db27946e02bb..5596620e1b59e4b5e4f601bc43428afaffd1327b 100644 (file)
@@ -44,7 +44,6 @@ std::ostream & operator<<(std::ostream &os, const DNSName& d)
 void DNSName::throwSafeRangeError(const std::string& msg, const char* buf, size_t length)
 {
   std::string dots;
-  const size_t maxDNSNameLength = 255; // Maybe this should be a symbol in DNSName itself
   if (length > maxDNSNameLength) {
     length = maxDNSNameLength;
     dots = "...";
@@ -81,7 +80,7 @@ DNSName::DNSName(const char* p, size_t length)
         if(labellen > 63)
           throwSafeRangeError("label too long to append: ", p, length);
 
-        if(iter-pbegin > 254) // reserve two bytes, one for length and one for the root label
+        if(iter-pbegin > static_cast<ptrdiff_t>(maxDNSNameLength - 1)) // reserve two bytes, one for length and one for the root label
           throwSafeRangeError("name too long to append: ", p, length);
 
         d_storage[lenpos]=labellen;
@@ -90,7 +89,7 @@ DNSName::DNSName(const char* p, size_t length)
     }
     else {
       d_storage=segmentDNSNameRaw(p, length);
-      if(d_storage.size() > 255) {
+      if(d_storage.size() > maxDNSNameLength) {
         throwSafeRangeError("name too long: ", p, length);
       }
     }
@@ -341,7 +340,7 @@ void DNSName::appendRawLabel(const char* start, unsigned int length)
     throw std::range_error("no such thing as an empty label to append");
   if(length > 63)
     throw std::range_error("label too long to append");
-  if(d_storage.size() + length > 254) // reserve one byte for the label length
+  if(d_storage.size() + length > maxDNSNameLength - 1) // reserve one byte for the label length
     throw std::range_error("name too long to append");
 
   if(d_storage.empty()) {
@@ -360,7 +359,7 @@ void DNSName::prependRawLabel(const std::string& label)
     throw std::range_error("no such thing as an empty label to prepend");
   if(label.size() > 63)
     throw std::range_error("label too long to prepend");
-  if(d_storage.size() + label.size() > 254) // reserve one byte for the label length
+  if(d_storage.size() + label.size() > maxDNSNameLength - 1) // reserve one byte for the label length
     throw std::range_error("name too long to prepend");
 
   if(d_storage.empty())
index 63bf40e22dbedaab0dfcb0871d6e5e16caad3532..a8c958da1bbf10250f45f8ec9a3e320dc46699ed 100644 (file)
@@ -77,6 +77,8 @@ inline unsigned char dns_tolower(unsigned char c)
 class DNSName
 {
 public:
+  static const size_t maxDNSNameLength = 255;
+
   DNSName()  {}          //!< Constructs an *empty* DNSName, NOT the root!
   // Work around assertion in some boost versions that do not like self-assignment of boost::container::string
   DNSName& operator=(const DNSName& rhs)
@@ -148,8 +150,8 @@ public:
   }
   DNSName& operator+=(const DNSName& rhs)
   {
-    if(d_storage.size() + rhs.d_storage.size() > 256) // one extra byte for the second root label
-      throwSafeRangeError("name too long", rhs.d_storage.data(), rhs.d_storage.size());
+    if(d_storage.size() + rhs.d_storage.size() > maxDNSNameLength + 1) // one extra byte for the second root label
+      throwSafeRangeError("resulting name too long", rhs.d_storage.data(), rhs.d_storage.size());
     if(rhs.empty())
       return *this;