]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Rewrite serializeToBuffer
authorFred Morcos <fred.morcos@open-xchange.com>
Tue, 24 Sep 2024 07:56:07 +0000 (09:56 +0200)
committerFred Morcos <fred.morcos@open-xchange.com>
Tue, 24 Sep 2024 09:21:08 +0000 (11:21 +0200)
modules/lmdbbackend/lmdbbackend.cc

index 757324389a5399721c371dc1ea2a14e1b4c00073..0c8c3faae2d4496ae531db9c2cb162481b3b612a 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "ext/lmdb-safe/lmdb-safe.hh"
+#include <cstring>
 #include <lmdb.h>
 #include <memory>
 #include <stdexcept>
@@ -905,26 +906,37 @@ BOOST_SERIALIZATION_SPLIT_FREE(DomainInfo);
 BOOST_IS_BITWISE_SERIALIZABLE(ComboAddress);
 
 template <>
-std::string serializeToBuffer(const LMDBBackend::LMDBResourceRecord& lrr)
+std::string serializeToBuffer(const LMDBBackend::LMDBResourceRecord& value)
 {
-  std::string ret;
-  uint16_t len = lrr.content.length();
-  ret.reserve(2 + len + 7);
-
-  ret.assign((const char*)&len, 2);
-  ret += lrr.content;
-  ret.append((const char*)&lrr.ttl, 4);
-  ret.append(1, (char)lrr.auth);
-  ret.append(1, (char)lrr.disabled);
-  ret.append(1, (char)lrr.ordername);
-  return ret;
+  std::string buffer;
+
+  // Data size of the resource record.
+  uint16_t len = value.content.length();
+
+  // Reserve space to store the size of the resource record + the content of the resource
+  // record + a few other things.
+  buffer.reserve(sizeof(len) + len + sizeof(value.ttl) + sizeof(value.auth) + sizeof(value.disabled) + sizeof(value.ordername));
+
+  // Store the size of the resource record.
+  std::memcpy(&buffer.at(0), &len, sizeof(len));
+
+  // Store the contents of the resource record.
+  buffer += value.content;
+
+  // The few other things.
+  std::memcpy(&buffer.at(sizeof(len) + len), &value.ttl, sizeof(value.ttl));
+  buffer.append(1, (char)value.auth);
+  buffer.append(1, (char)value.disabled);
+  buffer.append(1, (char)value.ordername);
+
+  return buffer;
 }
 
 template <>
-std::string serializeToBuffer(const vector<LMDBBackend::LMDBResourceRecord>& lrrs)
+std::string serializeToBuffer(const vector<LMDBBackend::LMDBResourceRecord>& value)
 {
   std::string ret;
-  for (const auto& lrr : lrrs) {
+  for (const auto& lrr : value) {
     ret += serializeToBuffer(lrr);
   }
   return ret;