[Bug c/101358] New: Warn when saving a pointer to an object with temporary lifetime
josephcsible at gmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Jul 7 05:04:05 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101358
Bug ID: 101358
Summary: Warn when saving a pointer to an object with temporary
lifetime
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: josephcsible at gmail dot com
Target Milestone: ---
Consider this C code:
typedef struct {
int x[1];
} foo;
foo f(void);
int g(void) {
int *p = f().x;
return *p;
}
The g() function is always UB, since the return value of f() has temporary
lifetime, so doing "return *p;" is dereferencing a pointer to an object whose
lifetime has ended. (This is the case both before and after C11's change to
temporary lifetime.) Since it's obvious at compile time that p can never be
used safely, we should have a warning for it, similar to how we have
-Wreturn-local-addr to catch mistakes like this function:
int *h(void) {
int x;
return &x;
}
More information about the Gcc-bugs
mailing list