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.
#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();
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));
}
/*