Bug 104192 - Uninitialized object read is not detected in a constant expression
Summary: Uninitialized object read is not detected in a constant expression
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2022-01-23 14:19 UTC by Fedor Chelnokov
Modified: 2024-11-18 18:55 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-01-23 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2022-01-23 14:19:44 UTC
In the following program, in a constant expression, a temporary object of A is created with all fields initialized, and then function f creates another object of A at the same address, skipping (re)initialization of the field x, which is read afterwards:
```
#include <memory>

struct A {
    int x;
    constexpr A() {}
    constexpr A(int xx) : x(xx) {}
};

constexpr int f(A && a) { 
    a.~A();
    std::construct_at<A>(&a);
    return a.x; 
}

static_assert( f(A{5}) == 5 );
```
This code is accepted by GCC, but other compilers complain about uninitialized object read. Demo: https://gcc.godbolt.org/z/sPGjj75Md

Related discussion: https://stackoverflow.com/q/70820127/7325599
Comment 1 Drea Pinski 2022-01-23 17:44:09 UTC
Confirmed.