]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 5119: Null pointer dereference in makeMemNodeDataOffset() (#1623)
authorBen Kallus <49924171+kenballus@users.noreply.github.com>
Mon, 18 Dec 2023 18:43:03 +0000 (18:43 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Sun, 24 Dec 2023 17:07:36 +0000 (17:07 +0000)
    UndefinedBehaviorSanitizer: undefined-behavior mem_node.cc:27:26 in
    runtime error: member access within null pointer of type 'mem_node'

Since only the address of the data member is computed, a compiler is
likely to perform pointer arithmetic rather than dereference a nullptr,
but it is best to replace this UB with a safe and clearer alternative.

src/mem_node.cc

index 0b75ed2ddec73bbe2a2e6fa65a8236ef2b4d18f2..a8a6f11fda5bf8206c0e33352b0abb512fd31b55 100644 (file)
@@ -12,6 +12,9 @@
 #include "mem/Pool.h"
 #include "mem_node.h"
 
+#include <cstddef>
+#include <type_traits>
+
 static ptrdiff_t makeMemNodeDataOffset();
 
 static ptrdiff_t _mem_node_data_offset = makeMemNodeDataOffset();
@@ -23,8 +26,8 @@ static ptrdiff_t _mem_node_data_offset = makeMemNodeDataOffset();
 static ptrdiff_t
 makeMemNodeDataOffset()
 {
-    mem_node *p = nullptr;
-    return ptrdiff_t(&p->data);
+    static_assert(std::is_standard_layout<mem_node>::value, "offsetof(mem_node) is unconditionally supported");
+    return ptrdiff_t(offsetof(mem_node, data));
 }
 
 /*