public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kees at outflux dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/109071] New: -Warray-bounds warning when array index checked via inline
Date: Wed, 08 Mar 2023 21:04:36 +0000	[thread overview]
Message-ID: <bug-109071-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109071

            Bug ID: 109071
           Summary: -Warray-bounds warning when array index checked via
                    inline
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kees at outflux dot net
  Target Milestone: ---

Created attachment 54611
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54611&action=edit
PoC for -Warray-bounds false positive

The Linux kernel is seeing -Warray-bounds warnings when array indexes are being
checked via inlines. This appears to be in the overly noisy/false positive
territory, but I don't actually know what's going on.

The upstream report is here:
https://lore.kernel.org/lkml/20230306220947.1982272-1-trix@redhat.com/

Originally I thought this was another -fsanitizer=shift issue, but after
reducing the test-case, it seems to be related to inlining or some other aspect
of optimization passes.

If the "assign" function is open-coded in the caller, the warning goes away.

If the index checks are moved before the "assign" calls, the warning goes away.

If there is only 1 call to "assign", the warning goes away.

Fundamentally there should be no warning at all since the value of "index" is
entirely unknown _except_ when it makes the call to "warn".

$ cat test.c
extern void warn(void);

#define MAX_ENTRIES     4

static inline void assign(int val, int *regs, int index)
{
        if (index >= MAX_ENTRIES)
                warn();
        *regs = val;
}

struct nums {
        int vals[MAX_ENTRIES];
};

void sparx5_psfp_sg_set(int *ptr, struct nums *sg, int index)
{
        int *val;

        val = &sg->vals[index];

        assign(0,    ptr, index);
        assign(*val, ptr, index);
}

$ gcc -Wall -O2  -c -o test.o test.c
test.c: In function 'sparx5_psfp_sg_set':
test.c:20:24: warning: array subscript 4 is above array bounds of 'int[4]'
[-Warray-bounds=]
   20 |         val = &sg->vals[index];
      |                ~~~~~~~~^~~~~~~
test.c:13:13: note: while referencing 'vals'
   13 |         int vals[MAX_ENTRIES];
      |             ^~~~

             reply	other threads:[~2023-03-08 21:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-08 21:04 kees at outflux dot net [this message]
2023-03-08 21:13 ` [Bug tree-optimization/109071] " pinskia at gcc dot gnu.org
2023-03-09 10:18 ` rguenth at gcc dot gnu.org
2023-03-09 16:15 ` kees at outflux dot net
2023-03-10 15:51 ` qinzhao at gcc dot gnu.org
2024-04-22 19:12 ` [Bug tree-optimization/109071] -Warray-bounds false positive warnings due to code duplication from jump threading qinzhao at gcc dot gnu.org
2024-04-22 19:21 ` kees at outflux dot net
2024-04-22 20:04 ` qinzhao at gcc dot gnu.org
2024-04-22 20:15 ` kees at outflux dot net
2024-04-22 21:19 ` qinzhao at gcc dot gnu.org
2024-05-13 14:21 ` qinzhao at gcc dot gnu.org
2024-05-14 16:19 ` qinzhao 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-109071-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).