public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/107851] New: Issues with -Wanalyzer-allocation-size messages
@ 2022-11-23 21:53 dmalcolm at gcc dot gnu.org
  2022-12-02 21:32 ` [Bug analyzer/107851] " cvs-commit at gcc dot gnu.org
  2022-12-02 22:05 ` dmalcolm at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-11-23 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2022-12-02 22:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-23 21:53 [Bug analyzer/107851] New: Issues with -Wanalyzer-allocation-size messages dmalcolm at gcc dot gnu.org
2022-12-02 21:32 ` [Bug analyzer/107851] " cvs-commit at gcc dot gnu.org
2022-12-02 22:05 ` dmalcolm 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).