public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "eblake at redhat dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/107100] New: -fanalyzer false positive about leak in function with attribute((malloc)) obtained from another function with attribute((malloc(free,1)))
Date: Fri, 30 Sep 2022 15:19:34 +0000	[thread overview]
Message-ID: <bug-107100-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 107100
           Summary: -fanalyzer false positive about leak in function with
                    attribute((malloc)) obtained from another function
                    with attribute((malloc(free,1)))
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eblake at redhat dot com
  Target Milestone: ---

I'm at a loss for why disabling -O2 or removing __attribute__((malloc(free,1)))
from function f silences a false-positive warning about a memory leak in
function g with __attribute((malloc)) specifically documenting that g() wants
to return a just-allocated pointer:

$ cat foo.c
#include <stdlib.h>
#include <string.h>

char *
__attribute__((malloc))
#ifndef HACK
__attribute__((malloc(free,1)))
#endif
f (const char *i)
{
  return strdup (i);
}

char *
__attribute__((malloc))
__attribute__((malloc(free,1)))
bar (void)
{
  char *x = f ("test");
  return x;
}
$ gcc -O2 -Wsuggest-attribute=malloc -fanalyzer -o foo.o -c foo.c
$ gcc -DHACK -Wsuggest-attribute=malloc -fanalyzer -o foo.o -c foo.c
$ gcc -Wsuggest-attribute=malloc -fanalyzer -o foo.o -c foo.c
foo.c: In function ‘f’:
foo.c:11:10: warning: leak of ‘x’ [CWE-401] [-Wanalyzer-malloc-leak]
   11 |   return strdup (i);
      |          ^~~~~~~~~~
  ‘bar’: events 1-3
    |
    |   17 | bar (void)
    |      | ^~~
    |      | |
    |      | (1) entry to ‘bar’
    |   18 | {
    |   19 |   char *x = f ("test");
    |      |             ~~~~~~~~~~
    |      |             |
    |      |             (2) allocated here
    |      |             (3) calling ‘f’ from ‘bar’
    |
    +--> ‘f’: events 4-5
           |
           |    9 | f (const char *i)
           |      | ^
           |      | |
           |      | (4) entry to ‘f’
           |   10 | {
           |   11 |   return strdup (i);
           |      |          ~~~~~~~~~~
           |      |          |
           |      |          (5) ‘x’ leaks here; was allocated at (2)
           |

             reply	other threads:[~2022-09-30 15:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30 15:19 eblake at redhat dot com [this message]
2022-09-30 16:04 ` [Bug c/107100] " eblake at redhat dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107100-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).