public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/109577] New: -Wanalyzer-allocation-size mishandles __builtin_mul_overflow
@ 2023-04-20 20:33 eggert at gnu dot org
  2023-05-12 20:39 ` [Bug analyzer/109577] " eggert at cs dot ucla.edu
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: eggert at gnu dot org @ 2023-04-20 20:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109577
           Summary: -Wanalyzer-allocation-size mishandles
                    __builtin_mul_overflow
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: eggert at gnu dot org
  Target Milestone: ---

This is (GCC) 13.0.1 20230401 (Red Hat 13.0.1-0) on x86-64.

Compile the following program with 'gcc -O2 -S -fanalyzer t.c'. GCC will
incorrectly complain "warning: allocated buffer size is not a multiple of the
pointee's size [CWE-131]". But the allocated buffer size must be a multiple of
sizeof (double), due to the checked call to __builtin_mul_overflow. As the
code's comment suggests, if the code uses plain * (integer multiply) instead
the bogus warning goes away.

I ran into this problem when compiling Emacs, which is often careful about
checking integer overflow. As a result I think I'll compile Emacs with
-Wno-analyzer-allocation-size to suppress false alarms, which would be a real
shame since this warning is useful for lower-quality code.

  #include <stdlib.h>

  int
  main (int argc, char **argv)
  {
    size_t s;
    double *d;
    if (__builtin_mul_overflow (argc, sizeof *d, &s))
      return 1;
    // No warning if the above is replaced with 's = argc * sizeof *d;'.
    d = malloc (s);
    if (d && s)
      {
        d[0] = argc;
        d[argc - 1] = argc + 1;
        return d[0];
      }
    return 0;
  }

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

end of thread, other threads:[~2024-05-21  9:14 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-20 20:33 [Bug analyzer/109577] New: -Wanalyzer-allocation-size mishandles __builtin_mul_overflow eggert at gnu dot org
2023-05-12 20:39 ` [Bug analyzer/109577] " eggert at cs dot ucla.edu
2023-06-10 12:29 ` cvs-commit at gcc dot gnu.org
2023-06-10 12:29 ` cvs-commit at gcc dot gnu.org
2023-06-12 13:17 ` dmalcolm at gcc dot gnu.org
2024-01-28  8:06 ` nightstrike at gmail dot com
2024-02-15 19:57 ` [Bug analyzer/109577] [13 Regression] " dmalcolm at gcc dot gnu.org
2024-04-14  5:20 ` pinskia at gcc dot gnu.org
2024-05-09 17:10 ` cvs-commit at gcc dot gnu.org
2024-05-09 17:11 ` cvs-commit at gcc dot gnu.org
2024-05-09 17:54 ` [Bug analyzer/109577] " dmalcolm at gcc dot gnu.org
2024-05-11 16:40 ` nightstrike at gmail dot com
2024-05-11 17:50 ` segher at gcc dot gnu.org
2024-05-21  9:14 ` jakub 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).