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

            Bug ID: 107851
           Summary: Issues with -Wanalyzer-allocation-size messages
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
  Target Milestone: ---

Taken from gcc.dg/analyzer/allocation-size-2.c


#include <stdlib.h>
#include <stdint.h>

void test_2 (int32_t n)
{
  int32_t *ptr = malloc (n * sizeof (int16_t));
  free (ptr);
}

With "-fanalyzer" (https://godbolt.org/z/fKcdrrh3z) we get:

<source>: In function 'test_2':
<source>:6:18: warning: allocated buffer size is not a multiple of the
pointee's size [CWE-131] [-Wanalyzer-allocation-size]
    6 |   int32_t *ptr = malloc (n * sizeof (int16_t));
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'test_2': event 1
    |
    |    6 |   int32_t *ptr = malloc (n * sizeof (int16_t));
    |      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |
    |      |                  (1) allocated '(long unsigned int)n * 2' bytes
and assigned to 'int32_t *' {aka 'int *'} here; 'sizeof (int32_t {aka int})' is
'4'
    |
<source>:6:18: warning: allocated buffer size is not a multiple of the
pointee's size [CWE-131] [-Wanalyzer-allocation-size]
  'test_2': events 1-3
    |
    |    6 |   int32_t *ptr = malloc (n * sizeof (int16_t));
    |      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |
    |      |                  (1) allocated '(long unsigned int)n * 2' bytes
here
    |      |                  (2) allocated '(long unsigned int)n * 2' bytes
here
    |      |                  (3) assigned to 'int32_t *' {aka 'int *'} here;
'sizeof (int32_t {aka int})' is '4'
    |
Compiler returned: 0

With "-fanalyzer -fanalyzer-fine-grained" ( https://godbolt.org/z/3fbvofPje )
we get:
<source>: In function 'test_2':
<source>:6:18: warning: allocated buffer size is not a multiple of the
pointee's size [CWE-131] [-Wanalyzer-allocation-size]
    6 |   int32_t *ptr = malloc (n * sizeof (int16_t));
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'test_2': events 1-3
    |
    |    6 |   int32_t *ptr = malloc (n * sizeof (int16_t));
    |      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |
    |      |                  (1) allocated '(long unsigned int)n * 2' bytes
here
    |      |                  (2) allocated '(long unsigned int)n * 2' bytes
here
    |      |                  (3) assigned to 'int32_t *' {aka 'int *'} here;
'sizeof (int32_t {aka int})' is '4'
    |
Compiler returned: 0

Issues:
* note how the "allocated '(long unsigned int)n * 2' bytes here" message is
repeated
* note how we get a duplicate diagnostic, which goes away with
-fanalyzer-fine-grained

Reply via email to