Description
Reference (section label): [intro.object]
Issue description
int main() {
unsigned char buffer[1]; // #1, undefined behavior
std::println("..."); // #2, intended to be observable checkpoint, but also UB
std::unreachable(); // #3, also UB
}
#2
is only an observable checkpoint without #1
, so implementations are permitted to optimize it out. That is because the implicit object creation of zero or more objects inside buffer
only takes place "if doing so would result in the program having defined behavior" ([intro.object] paragraph 11), and otherwise, the whole program has undefined behavior. Since #3
unconditionally gives this program undefined behavior, implicit object creation does not take place, and #1
has undefined behavior.
In other words, the future undefined behavior from #3
travels back into the past to #1
, ignoring the observable checkpoint at #2
.
Suggested resolution
Change [intro.object] paragraph 11 as follows:
Some operations are described as implicitly creating objects within a specified region of storage.
For each operation O that is specified as implicitly creating objects,that operationO implicitly creates and starts the lifetime of zero or more objects of implicit-lifetime types ([basic.types.general]) in its specified region of storage if doing so would result inthe program having defined behavior. If no such set of objects would give the program defined behavior, the behavior of the program is undefined.O becoming part of the defined prefix of the execution; otherwise, the behavior of O is undefined.
If multiple such sets of objects would givethe programO defined behavior, it is unspecified which such set of objects is created.