https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121376
Bug ID: 121376
Summary: Objects with temporary lifetime do not work correctly
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: luigighiron at gmail dot com
Target Milestone: ---
The following program demonstrates the issues with how GCC handles objects with
temporary lifetime:
int main(){
struct{int x[1];}x={};
int*p;
return p=(0,x).x,*x.x=1,*p;
}
The expression (0,x) uses how the comma operator always results in a non-lvalue
expression. So (0,x).x is not pointing at *x.x but rather at a temporary object
which holds a copy of the array. This temporary object lives for the full
expression, so *p later is valid and should have the same value as same value
as when the temporary object was created. Hence, despite changing *x.x to 1 the
program should return zero. GCC does not implement this correctly, and treats
p=(0,x).x the same as p=x.x so it results in one. Note that even though the
temporary object has a non-unique address, x has a unique address so they
shouldn't share addresses.
Clang used to have the same issue, but they have recently fixed it:
https://github.com/llvm/llvm-project/pull/133472.