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

            Bug ID: 101983
           Summary: analyzer leak false positives building singly linked
                    list
           Product: gcc
           Version: 12.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: ---

-fanalyzer reports various "leak" false positives on the following code:

*******************************************************************

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

struct list {
        struct list* next;
        void *a;
};

void func(struct list **res)
{
        struct list *cur = NULL;
        do {
                struct list *n = malloc(sizeof(struct list));
                void *a = malloc(1);
                if (n == NULL || a == NULL) {
                        if (n != NULL) free(n);
                        if (a != NULL) free(a);
                        break;
                }

                if (cur == NULL) {
                        *res = cur = n;
                } else {
                        cur->next = n;
                        cur = n;
                }
                n->a = a;
        } while (true);
}

int main()
{
        struct list *res;
        func(&res);
}

*******************************************************************

trunk shows 5 "leak" false positives: https://godbolt.org/z/W4T88579q
gcc 11.2 shows 1 "leak" false positive: https://godbolt.org/z/YEa94PcaK

Reported downstream as https://bugzilla.redhat.com/show_bug.cgi?id=1995208

Reply via email to