https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110402
Bug ID: 110402
Summary: Bogus -Waddress warning that pointer comparison is
always true
Product: gcc
Version: 13.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: lsof at mailbox dot org
Target Milestone: ---
Created attachment 55398
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55398&action=edit
testcase
In the following testcase:
struct m { float *v; int t; };
_Bool chk(struct m *m, int key) {
return (m->t = key) < 0 ? 0 : &m->v[key];
}
/* a comma expression can suppress it */
_Bool chk2(struct m *m, int key) {
return (m->t = key, m->t < 0 ? 0 : &m->v[key]);
}
/* however, this does NOT warn */
_Bool chk3(struct m *m, int key) {
return (m->t = key) < 0 ? &m->v[key] : &m->v[key];
}
gcc -Waddress gives this warning:
x.c: In function ‘chk’:
x.c:4:5: warning: the comparison will always evaluate as ‘true’ for the pointer
operand in ‘m->v + (sizetype)((long unsigned int)key * 4)’ must not be NULL
[-Waddress]
4 | return key < 0 ? 0 : &m->v[key];
| ^~~~~~
In particular the second function does not produce the warning (correctly), but
the third function for which the warning actually applies does not give a
warning.