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

* [Bug middle-end/105348] Overly aggressive -Warray-bounds after conditional
  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 ` thiago at kde dot org
  2022-04-25  6:46 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: thiago at kde dot org @ 2022-04-22 17:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Thiago Macieira <thiago at kde dot org> ---
Qt workaround: https://codereview.qt-project.org/c/qt/qtbase/+/407217

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

* [Bug middle-end/105348] Overly aggressive -Warray-bounds after conditional
  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
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-25  6:46 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Blocks|                            |56456

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We are generally diagnosing conditional code.  Consider changing your example
to put the if (cond(l)) conditional in g(), guarding the call to f().  Then the
code in f() would look "unconditional" but of course in reality little code
as reachable from program entry is "unconditional", so artificially treating
some conditionals as "more conditional" than others is not the solution.

So the bottom line is that it's hard to avoid diagnosing such cases and
it's not even desirable.

The solution is to avoid putting too much abstraction into the guarding
conditional.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

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

* [Bug middle-end/105348] Overly aggressive -Warray-bounds after conditional
  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
  3 siblings, 0 replies; 5+ messages in thread
From: thiago at kde dot org @ 2022-04-25 14:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Thiago Macieira <thiago at kde dot org> ---
I understand. I'm just trying to avoid having to add code for a corner-case.
People don't usually parse empty buffers, so it's usually fine to allow it to
proceed and discover an EOF condition.

Anyway, worked around. Feel free to close if this is too hard to fix.

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

* [Bug middle-end/105348] Overly aggressive -Warray-bounds after conditional
  2022-04-22 17:33 [Bug middle-end/105348] New: Overly aggressive -Warray-bounds after conditional thiago at kde dot org
                   ` (2 preceding siblings ...)
  2022-04-25 14:55 ` thiago at kde dot org
@ 2022-05-31 16:25 ` thiago at kde dot org
  3 siblings, 0 replies; 5+ messages in thread
From: thiago at kde dot org @ 2022-05-31 16:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Thiago Macieira <thiago at kde dot org> ---
One more Qt workaround, for the record:
https://codereview.qt-project.org/c/qt/qtbase/+/413730

^ 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).