]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsheader_aligned: Prevent copies 16503/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 14 Nov 2025 09:37:33 +0000 (10:37 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 14 Nov 2025 09:40:03 +0000 (10:40 +0100)
The `dnsheader_aligned` object contains a pointer that references
either the `dnsheader` passed to the constructor if it is properly
aligned, or the internal `dnsheader` member. In the second case,
making a copy would mean we can reference an object that has been
destructed, which is a serious problem.

This commit also ensures copy elision is done `DNSQuestion:getHeader`,
as otherwise the compiler might refuse to compile.

Signed-off-by: Remi Gacogne <remi.gacogne@powerdns.com>
pdns/dns.hh
pdns/dnsdistdist/dnsdist.hh

index 19afdbc841c3cdff43047c0c24a1e12d6f00a657..b946f4fe1b494533023969957c5b5dade87d68a9 100644 (file)
@@ -205,6 +205,11 @@ public:
       d_p = &d_h;
     }
   }
+  dnsheader_aligned(const dnsheader_aligned&) = delete;
+  dnsheader_aligned(dnsheader_aligned&&) = delete;
+  dnsheader_aligned& operator=(const dnsheader_aligned&) = delete;
+  dnsheader_aligned& operator=(dnsheader_aligned&&) = delete;
+  ~dnsheader_aligned() = default;
 
   [[nodiscard]] const dnsheader* get() const
   {
index 6b6cc73f0f52e93fb75a9bbcf6c1cd87e9de0da3..355d67f4ed3b4bd94307501ff03322551ceb4633 100644 (file)
@@ -87,8 +87,7 @@ struct DNSQuestion
     if (data.size() < sizeof(dnsheader)) {
       throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");
     }
-    dnsheader_aligned dh(data.data());
-    return dh;
+    return dnsheader_aligned(data.data());
   }
 
   /* this function is not safe against unaligned access, you should