STL icon indicating copy to clipboard operation
STL copied to clipboard

`<expected>`: Investigate reusing tail padding of the union

Open frederick-vs-ja opened this issue 2 years ago • 4 comments

Given [[msvc::no_unique_address]] landed in Clang 18, we can applied it to union members (possibly with the whole union) in expected.

E.g. in this example

struct IC {
    int i;
    char c;
};
std::expected<IC, int> e;

its possible to make sizeof(e) == 8 because the size of the union is 8, while the first 5 bytes are value bytes, and bool flag can live in the tail padding of the union.

We need to ensure that the tail padding won't be furtherly reused - otherwise, when the expected object is itself potentially-overlapping, reconstruction of a union member may overwrite the data not controlled by the implementation.

See also LLVM-68552, LLVM-68733, and LLVM-69673.

frederick-vs-ja avatar Oct 27 '23 04:10 frederick-vs-ja

Related to #1364. That issue is vNext because C++20 is locked down, but we can do whatever we want for C++23 until /std:c++23 is finalized. Avoiding the tail-padding-reuse problem is definitely very important, thanks for mentioning it and linking libc++'s bug!

StephanTLavavej avatar Nov 01 '23 21:11 StephanTLavavej