public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1
@ 2023-10-09  1:12 xavier.cooney03 at gmail dot com
  2023-10-09  1:36 ` [Bug c/111730] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: xavier.cooney03 at gmail dot com @ 2023-10-09  1:12 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111730
           Summary: erroneous alloc-size-larger-than warning with -O1
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xavier.cooney03 at gmail dot com
  Target Milestone: ---

Created attachment 56076
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56076&action=edit
bug.c

Hi, in some circumstances gcc incorrectly emits the alloc-size-larger-than
warning:

$ gcc -O1 -Wall -c bug.c
bug.c: In function ‘foo’:
bug.c:9:15: warning: argument 1 range [18446744071562067968,
18446744073709551615] exceeds maximum object size 9223372036854775807
[-Walloc-size-larger-than=]
    9 |     char *a = malloc(x);
      |               ^~~~~~~~~
bug.c:4:14: note: in a call to allocation function ‘malloc’ declared here
    4 | extern void *malloc (size_t size) __attribute__ ((__malloc__));
      |              ^~~~~~


This only seems to occur when using -O1, other optimisation levels (-O0, -O2,
-O3, -Os) don't result in the warning.

The 'useless' loops are necessary to reproduce, removing the first or last loop
causes the warning to disappear.

18446744071562067968 = 0xffffffff80000000
18446744073709551615 = 0xffffffffffffffff

It seems as if gcc is incorrectly deducing that `x` must be negative.

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,objc,obj-c++ --enable-bootstrap
--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--with-build-config=bootstrap-lto --with-linker-hash-style=gnu
--with-system-zlib --enable-__cxa_atexit --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.1 20230801 (GCC)

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

* [Bug c/111730] erroneous alloc-size-larger-than warning with -O1
  2023-10-09  1:12 [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1 xavier.cooney03 at gmail dot com
@ 2023-10-09  1:36 ` pinskia at gcc dot gnu.org
  2023-10-09  2:08 ` [Bug tree-optimization/111730] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-09  1:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You might want to add a check that x is not negative and I suspect that will
fix the warning.

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

* [Bug tree-optimization/111730] erroneous alloc-size-larger-than warning with -O1
  2023-10-09  1:12 [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1 xavier.cooney03 at gmail dot com
  2023-10-09  1:36 ` [Bug c/111730] " pinskia at gcc dot gnu.org
@ 2023-10-09  2:08 ` pinskia at gcc dot gnu.org
  2023-10-09  2:15 ` pinskia at gcc dot gnu.org
  2023-10-09  2:38 ` xavier.cooney03 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-09  2:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The difference between -O1 and -O2 is -O2 removes the empty loops.

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

* [Bug tree-optimization/111730] erroneous alloc-size-larger-than warning with -O1
  2023-10-09  1:12 [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1 xavier.cooney03 at gmail dot com
  2023-10-09  1:36 ` [Bug c/111730] " pinskia at gcc dot gnu.org
  2023-10-09  2:08 ` [Bug tree-optimization/111730] " pinskia at gcc dot gnu.org
@ 2023-10-09  2:15 ` pinskia at gcc dot gnu.org
  2023-10-09  2:38 ` xavier.cooney03 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-09  2:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note you can reproduce the same warning with ( -O2 -fno-code-hoisting
-fno-tree-loop-im -fno-tree-pre):
```
// #include <stdlib.h>

typedef long unsigned int size_t;
extern void *malloc (size_t size) __attribute__ ((__malloc__));
int *t;
void foo(int x) {
    // if (x < 1) return;
    for (int i = 0; i < x; i++) {*t = i;}

    char *a = malloc(x);

    for (int i = 0; i < x; i++)
        a[i] = 0;

    while (a[x - 1]) {*t++;}
}
```

I still think you should add a check for x being negative to fix the
code/warning. Unless you have a unreduced testcase which has the test before
and still able to produce the warning, this will most likely be closed as
invalid.

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

* [Bug tree-optimization/111730] erroneous alloc-size-larger-than warning with -O1
  2023-10-09  1:12 [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1 xavier.cooney03 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-10-09  2:15 ` pinskia at gcc dot gnu.org
@ 2023-10-09  2:38 ` xavier.cooney03 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: xavier.cooney03 at gmail dot com @ 2023-10-09  2:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Xavier Cooney <xavier.cooney03 at gmail dot com> ---
I see, thanks for looking into this.

The unreduced test case (which was from a student confused about the error
message) was still passing a value to `malloc` which from the context which gcc
could see wasn't /necessarily/ non-negative.

But the code
```
void foo2(int x) {
    char *a = malloc(x);
    (void) a;
}
```
doesn't trigger the warning, even though `x` could also be negative.

I'm not sure why the extra loops are necessary for the warning to be emitted if
the compiler is trying to warn about any time a potentially negative value
might be passed to `malloc`.

Also the error message reads to me as saying that argument 1 must be in the
range [18446744071562067968, 18446744073709551615] (in which case it would be
incorrect), rather than saying the argument could be in the range (in which
case it would be correct).

Thanks again for look into this :)

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

end of thread, other threads:[~2023-10-09  2:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-09  1:12 [Bug c/111730] New: erroneous alloc-size-larger-than warning with -O1 xavier.cooney03 at gmail dot com
2023-10-09  1:36 ` [Bug c/111730] " pinskia at gcc dot gnu.org
2023-10-09  2:08 ` [Bug tree-optimization/111730] " pinskia at gcc dot gnu.org
2023-10-09  2:15 ` pinskia at gcc dot gnu.org
2023-10-09  2:38 ` xavier.cooney03 at gmail dot com

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