public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/101713] New: -Wanalyzer-malloc-leak false positive with GNU coreutils hash table code
@ 2021-08-01  7:20 eggert at cs dot ucla.edu
  2021-11-17 14:55 ` [Bug analyzer/101713] " dmalcolm at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: eggert at cs dot ucla.edu @ 2021-08-01  7:20 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101713
           Summary: -Wanalyzer-malloc-leak false positive with GNU
                    coreutils hash table code
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: eggert at cs dot ucla.edu
  Target Milestone: ---

Because of GCC bug 94458 we've been disabling -Wanalyzer-malloc-leak when
compiling Gnulib-based code such as GNU coreutils. Since GCC bug 94458 is fixed
I thought I'd try enabling that warning. I simplified the first false positive
I ran into (in gnulib/lib/exclude.c) to the following:

void free (void *);
char *xstrdup (char const *)
  __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (free, 1)))
  __attribute__ ((__returns_nonnull__));
void *hash_insert (void const *entry);

void
addpat (char *pattern)
{
  char *str = xstrdup (pattern);
  hash_insert (str);
}

For this example, the command 'gcc -fanalyzer -Wanalyzer-too-complex -O2 -S
t1.i' outputs the following diagnostic, which is a false alarm because 'str'
has been put into a hash table and has not leaked. Omitting the 'const' from
the declaration of the 'entry' formal parameter makes the false alarm go away,
but we shouldn't have to omit the 'const'. For now, I think we'll continue to
disable -Wanalyzer-too-complex in Gnulib-derived code.

t1.i: In function ‘addpat’:
t1.i:12:1: warning: leak of ‘str’ [CWE-401] [-Wanalyzer-malloc-leak]
   12 | }
      | ^
  ‘addpat’: events 1-2
    |
    |   10 |   char *str = xstrdup (pattern);
    |      |               ^~~~~~~~~~~~~~~~~
    |      |               |
    |      |               (1) allocated here
    |   11 |   hash_insert (str);
    |   12 | }
    |      | ~
    |      | |
    |      | (2) ‘str’ leaks here; was allocated at (1)
    |

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

end of thread, other threads:[~2024-04-02  4:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-01  7:20 [Bug analyzer/101713] New: -Wanalyzer-malloc-leak false positive with GNU coreutils hash table code eggert at cs dot ucla.edu
2021-11-17 14:55 ` [Bug analyzer/101713] " dmalcolm at gcc dot gnu.org
2021-11-17 20:28 ` eggert at cs dot ucla.edu
2024-04-02  4:53 ` egallager 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).