public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local
@ 2022-12-24 10:53 jhaberman at gmail dot com
  2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jhaberman at gmail dot com @ 2022-12-24 10:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108217
           Summary: bogus -Warray-bounds with pointer to constant local
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jhaberman at gmail dot com
  Target Milestone: ---

Repro:

void ExternFunc1();
void ExternFunc2(const int*);

char mem[32];

static void StaticFunc(const int* i) {
  void* ptr = (void*)0;
  switch (*i) {
    case 0:
      ExternFunc2(i);
      return;
    case 1:
      __builtin_memcpy(mem, &ptr, sizeof(ptr));
      return;
    case 2: {
      __builtin_memcpy(mem, &ptr, 32);
      return;
    }
  }
}

void Bad() {
  const int i = 1;
  ExternFunc1();
  StaticFunc(&i);
}

This reproduces on trunk according to Godbolt: https://godbolt.org/z/vYGo1z6bG

Godbolt also indicates a missed optimization, which is probably related to the
bogus warning.  Clang correctly performs constant propagation of the local `i`,
whereas GCC seems to think that all cases of the switch() are reachable.

It is true that &i escapes, but mutating `i` is UB because it is const, so it
should be legal to perform constant propagation here.

Additionally, even if ExternFunc2() mutated `i`, it would be too late to change
its value in time to affect the switch().

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

end of thread, other threads:[~2022-12-24 20:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
2022-12-24 20:14 ` pinskia at gcc dot gnu.org
2022-12-24 20:35 ` jhaberman at gmail dot com
2022-12-24 20:42 ` pinskia at gcc dot gnu.org
2022-12-24 20:46 ` 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).