boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
96 stars 116 forks source link

Do not memset(0) POD types #239

Closed dangelog closed 1 year ago

dangelog commented 1 year ago

"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).

Fixes #238

dangelog commented 1 year ago

Thanks for running a CI precheck. I think I have fixed the two issues found, 1) there's a run in C++03 mode, so I can't create a vector of a local type. Moved the type outside main(). 2) MSVC miscompiles the code, so the test is skipped there.

igaztanaga commented 1 year ago

Many thanks for the report and the patch!