public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "contino at epigenesys dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/105906] New: fanalyzer strdup false positive leak in loop
Date: Thu, 09 Jun 2022 09:33:01 +0000	[thread overview]
Message-ID: <bug-105906-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105906
           Summary: fanalyzer strdup false positive leak in loop
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: contino at epigenesys dot com
  Target Milestone: ---

Created attachment 53109
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53109&action=edit
The warning

Hi,
compiling the code with GCC-12.1.0 on Debian I am seeing the fanalyzer warning
in the attachment, which seems to me a false positive leak related to strdup.

Code:

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#define LEN 64

char **__epystr_explode(const char *delim, char *str)
{
        char **out = NULL;
        int i;

        if (str == NULL || delim == NULL)
                return NULL;

        out = malloc(LEN * sizeof(char *));
        if (out == NULL)
                return NULL;

        for (i = 0; i < LEN; i++) {
                out[i] = strdup("bla");
                if (out[i] == NULL)
                        goto freem;
        }
        return out;

freem:
        while (--i >= 0)
                free(out[i]);
        free(out);
        return NULL;
}

If I replace strdup with malloc the warning disappears.

for (i = 0; i < LEN; i++) {
        out[i] = malloc(10);
        if (out[i] == NULL)
                goto freem;
}

The same happens if I replace the for loop with a goto loop.

i = 0;
loop:
out[i] = strdup("bla");
if (out[i] == NULL)
        goto freem;
i++
if (i < LEN)
        goto loop;

             reply	other threads:[~2022-06-09  9:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-09  9:33 contino at epigenesys dot com [this message]
2022-06-10 12:19 ` [Bug analyzer/105906] " dmalcolm at gcc dot gnu.org
2023-03-13 18:36 ` dmalcolm at gcc dot gnu.org
2023-03-13 18:37 ` dmalcolm at gcc dot gnu.org
2023-03-13 18:54 ` cvs-commit at gcc dot gnu.org

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-105906-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).