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;
  • [Bug analyzer/105906] New: fana... contino at epigenesys dot com via Gcc-bugs

Reply via email to