This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 117b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-08-11


3039. Undefined behavior from implicit object creation ignores observable checkpoints

Section: 6.8.2  [intro.object]     Status: open     Submitter: Jan Schultke     Date: 2025-06-23

(From submission #719.)

Consider:

  int main() {
    unsigned char buffer[1];   // #1, undefined behavior
    std::println("...");       // #2, intended to be observable checkpoint, but also UB
    std::unreachable();        // #3, also undefined behavior
  }

According to 6.8.2 [intro.object] paragraph 11, the implicit object creation at #1 only takes place "if doing so would result in the program having defined behavior", and otherwise the entire program has undefined behavior. However, since #3 arranges for no possible execution to have defined behavior, implicit object creation does not take place, and #1 has undefined behavior, ignoring the observable checkpoint at #2.

Suggested resolution:

  1. Add after 4.1.2 [intro.abstract] paragraph 7 as follows:

    Recommended practice: An implementation should issue a diagnostic when such an operation is executed. [Note 3: ... —end note]

    Certain operations are specified as making a choice to maximize the defined prefix of the execution. The behavior of all such operations in that execution is such that the defined prefix P of the execution is not a proper subset of the defined prefix that would result from any other choices, but the choices are otherwise unspecified.

  2. Change in 6.8.2 [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 operation O implicitly creates and starts the lifetime of zero or more objects of implicit-lifetime types (6.10.1 [intro.execution]) in its specified region of storage if doing so would result in the program having defined behavior chosen to maximize the defined prefix of the execution (4.1.2 [intro.abstract]). If no such set of objects would give the program defined behavior, the behavior of the program is undefined. [Note: If multiple such sets of objects would give the program defined behavior result in a maximal defined prefix for the execution, it is unspecified which such set of objects is created. -- end note]
  3. Change in 6.8.2 [intro.object] paragraph 12 as follows:

    Further, after implicitly creating objects within a specified region of storage, some operations are described as producing a pointer to a suitable created object. These operations select one of the implicitly-created objects whose address is the address of the start of the region of storage, and produce a pointer value that points to that object , if that value would result in the program having defined behavior. If no such pointer value would give the program defined behavior, the behavior of the program is undefined such as to maximize the defined prefix of the execution. [ Note: If multiple such pointer values would give the program defined behavior result in a maximal prefix, it is unspecified which such pointer value is produced. -- end note]