public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110402] New: Bogus -Waddress warning that pointer comparison is always true
@ 2023-06-25  9:58 lsof at mailbox dot org
  2023-06-25 10:31 ` [Bug c/110402] " schwab@linux-m68k.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: lsof at mailbox dot org @ 2023-06-25  9:58 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-05 18:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-25  9:58 [Bug c/110402] New: Bogus -Waddress warning that pointer comparison is always true lsof at mailbox dot org
2023-06-25 10:31 ` [Bug c/110402] " schwab@linux-m68k.org
2023-06-25 13:06 ` pinskia at gcc dot gnu.org
2023-06-25 14:12 ` lsof at mailbox dot org
2023-06-25 14:26 ` lsof at mailbox dot org
2024-04-05 18:04 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).