Skip to content

False positive: Potential leak of memory [unix.malloc] #55019

@josephcsible

Description

@josephcsible

I'm running version 14.0.0 obtained from https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz

Consider this C program:

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

struct mystruct {
    void *ptr;
    char arr[4];
};

int main(void) {
    struct mystruct x;
    x.ptr = malloc(1);
    strcpy(x.arr, "hi\n");
    free(x.ptr);
}

When I run clang --analyze /tmp/bug.c, I get this:

/tmp/bug.c:13:5: warning: Potential leak of memory pointed to by 'x.ptr' [unix.Malloc]
    free(x.ptr);
    ^~~~~~~~~~~
1 warning generated.

But it's clear that there's not actually a potential leak of memory here.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions