From: Remi Gacogne Date: Fri, 14 Nov 2025 09:37:33 +0000 (+0100) Subject: dnsheader_aligned: Prevent copies X-Git-Tag: rec-5.4.0-alpha1~76^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F16503%2Fhead;p=thirdparty%2Fpdns.git dnsheader_aligned: Prevent copies 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 --- diff --git a/pdns/dns.hh b/pdns/dns.hh index 19afdbc841..b946f4fe1b 100644 --- a/pdns/dns.hh +++ b/pdns/dns.hh @@ -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 { diff --git a/pdns/dnsdistdist/dnsdist.hh b/pdns/dnsdistdist/dnsdist.hh index 6b6cc73f0f..355d67f4ed 100644 --- a/pdns/dnsdistdist/dnsdist.hh +++ b/pdns/dnsdistdist/dnsdist.hh @@ -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