public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/105348] New: Overly aggressive -Warray-bounds after conditional
@ 2022-04-22 17:33 thiago at kde dot org
  2022-04-22 17:37 ` [Bug middle-end/105348] " thiago at kde dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: thiago at kde dot org @ 2022-04-22 17:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105348
           Summary: Overly aggressive -Warray-bounds after conditional
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thiago at kde dot org
  Target Milestone: ---

Testcase:

#include <string.h>
char empty;

void sink(int);
bool cond(size_t);
void f(const char *s, size_t l)
{
    int n;
    if (cond(l)) {
        memcpy(&n, s, sizeof(n));
        sink(n);
    }
}

void g()
{
    f(&empty, 1);
}

#ifdef EXPAND
bool cond(size_t l)
{
    return l >= sizeof(int);
}
#endif

$ gcc -DEXPAND -O3 -c -Wall -Wextra -Werror test.cpp && echo $?
0
$ gcc -O3 -c -Wall -Wextra -Werror test.cpp && echo $?         
In function ‘void f(const char*, size_t)’,
    inlined from ‘void f(const char*, size_t)’ at test.cpp:6:6,
    inlined from ‘void g()’ at test.cpp:17:6:
test.cpp:10:15: error: array subscript ‘unsigned int[0]’ is partly outside
array bounds of ‘char [1]’ [-Werror=array-bounds]
   10 |         memcpy(&n, s, sizeof(n));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~
test.cpp: In function ‘void g()’:
test.cpp:2:6: note: object ‘empty’ of size 1
    2 | char empty;
      |      ^~~~~
cc1plus: all warnings being treated as errors

I've noticed this even when the other function was present and available for
inlining. Unfortunately, for reasons outside of my direct control, GCC decided
not to inline that function, which meant it considers the condition bad.

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

end of thread, other threads:[~2022-05-31 16:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-22 17:33 [Bug middle-end/105348] New: Overly aggressive -Warray-bounds after conditional thiago at kde dot org
2022-04-22 17:37 ` [Bug middle-end/105348] " thiago at kde dot org
2022-04-25  6:46 ` rguenth at gcc dot gnu.org
2022-04-25 14:55 ` thiago at kde dot org
2022-05-31 16:25 ` thiago at kde dot 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).