Skip to content

Fix UB in AlignedNumber

Santiago Ospina De Los Ríos requested to merge cleanup/use-std-align into master

Fix bug where AlignedNumber could not check if placement new alignment is correct. This follows from the discussion in #349 (comment 141238):

The problem comes from the placement-new used to test the "miss-aligned" objects. The test relies in the fact that a placement new in a miss-aligned address creates an object at this address. This is in principle fine. However, de-referencing a miss-aligned pointer is undefined behavior and this happen whenever we use the object for anything (e.g. calling its constructor).

And solves the issue by checking the potential misaligned address during placement new instead of the object itself. Reasoning is given here #349 (comment 141248):

That's because my diff does not check destructors: you cannot due undefined behavior. My diff only checks placement new (notice that there is no placement delete), the other new operators must return a properly aligned address due the alignas(...), so guarding them should not be necessary. This should cover most of the issues, but it will not prevent people from using a misaligned object with reinterpret_cast or a std:: start_lifetime_as[_array]. On the other hand, the constructor/destructor version would also not cover those cases either.

Aside of that, this MR updates the code to use std::align instead of hard-coded bit manipulations that hard to reason about. See #349 (comment 131541).


Notice that I added the changelog entry under Release 2.10 on request to back-port this to %DUNE 2.10.0 by @gruenich.

Merge request reports

Loading