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

            Bug ID: 125116
           Summary: -Wanalyzer-allocation-size false positive with Emacs
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: eggert at cs dot ucla.edu
  Target Milestone: ---

When building the latest Emacs with gcc (GCC) 16.0.1 20260321 (Red Hat
16.0.1-0), I got a false positive with -Wanalyzer-allocation-size. "False" in
the sense that the warning is useless and even counterproductive if an unwary
developer pays attention to it, although the warning is technically correct.

Here's a small program illustrating the issue:

#include <stdlib.h>

unsigned long
f ()
{
  int *f = malloc (10 * sizeof (int) + 3);
  int *p = f;
  char *q = (char *) (p + 10);
  *p = 100;
  *q = '\0';
  unsigned long r = (unsigned long) f; // Artificially use f.
  free (f);
  return r;
}

Compile with "gcc -O2 -S -fanalyzer" and I see:

t.c: In function ‘f’:
t.c:6:12: warning: allocated buffer size is not a multiple of the pointee's
size [CWE-131] [-Wanalyzer-allocation-size]
    6 |   int *f = malloc (10 * sizeof (int) + 3);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ‘f’: events 1-2
    6 |   int *f = malloc (10 * sizeof (int) + 3);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |            |
      |            (1) allocated 43 bytes here
      |            (2) ⚠️  assigned to ‘int *’ here; ‘sizeof (int)’ is ‘4’

There's nothing wrong with the code, and I'm not sure I see what the point of
the -fanalyzer check is. It would indeed be an error if the alignment of p was
stricter than anything that could fit into the requested size, but it's not an
error simply because p's size does not evenly divide the requested allocation
size.

I plan to work around the problem by using -Wno-analyzer-allocation-size with
Emacs as I don't see how the warning would be useful in finding bugs in Emacs.
I suggest either making -Wno-analyzer-allocation-size the default, or
rethinking how the warning is implemented.

Reply via email to