public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lsof at mailbox dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/110402] New: Bogus -Waddress warning that pointer comparison is always true
Date: Sun, 25 Jun 2023 09:58:39 +0000	[thread overview]
Message-ID: <bug-110402-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2023-06-25  9:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-25  9:58 lsof at mailbox dot org [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110402-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).