Do not memset(0) POD types

"POD" is the wrong type trait to determine if something can be safely
zero-filled in order to achieve zero initialization. Consider a type
like

  struct POD { int POD::*ptr; };

This is a POD; its value initialization needs to value initialize the
member, and since it's a pointer, that's zero initialization, and that's
setting the pointer to null.

On Itanium, a null pointer to data member is not zero filled; it actually
has the value -1u.

Hence, zero-filling via memset(0) a POD object like the one above is
erroneous. Unfortunately there is no type trait in C++ that we can use to
know if a given datatype can be value initialized by zero-filling -- we
can check for trivial constructability, but that's a necessary
condition, not a sufficient one (POD above is also trivially
constructible).

The test is disabled on MSVC because of a compiler bug.

Fixes #238
This commit is contained in:
Giuseppe D'Angelo committed 2023-02-02 11:29:53 +01:00
1 parent acc5b088f5
commit b694ada294
3 files changed
+25 -7

No files matched your search

@@ -296,9 +296,6 @@ struct is_memzero_initializable
#if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO)
|| dtl::is_floating_point<value_type>::value
#endif
#if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) && defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
|| dtl::is_pod<value_type>::value
#endif
);
};