llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

false positive: Potential leak connected with memcpy

Open llvmbot opened this issue 11 years ago • 3 comments

Bugzilla Link 22954
Resolution FIXED
Resolved on Oct 29, 2015 06:32
Version 3.6
OS Linux
Reporter LLVM Bugzilla Contributor
CC @goussepi

Extended Description

Hello,

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

struct aa { char *s; char data[123]; };

int main() { struct aa a = {0}; a.s = strdup("hello"); char input[] = {'a', 'b', 'c', 'd'}; memcpy(a.data, input, 4);

free(a.s);
return 0;

}

This code snippet doesn't have memleak (tested with valgrind) but scan-build complains about it. Tested with gcc and clang and on both the output is the same: scan-build clang a.c scan-build: Using '/home/dirdival/clang-3.6/bin/clang' for static analysis a.c:15:5: warning: Potential leak of memory pointed to by 'a.s' free(a.s); ^~~~~~~~~

When I remove 2 lines everything works: $ cat b.c #include <string.h> #include <stdlib.h>

struct aa { char *s; char data[123]; };

int main() { struct aa a = {0}; a.s = strdup("hello");

free(a.s);
return 0;

} $ scan-build clang b.c scan-build: Using '/home/dirdival/clang-3.6/bin/clang' for static analysis scan-build: Removing directory '/tmp/scan-build-2015-03-19-105501-3689-1' because it contains no reports. scan-build: No bugs found.

Additional information: $ clang --version clang version 3.6.0 (tags/RELEASE_360/final) Target: x86_64-unknown-linux-gnu Thread model: posix

Regards, Krzysztof

llvmbot avatar Mar 19 '15 11:03 llvmbot

This was fixed in r248516.

goussepi avatar Oct 29 '15 13:10 goussepi

Thanks Krzysztof for reporting the issue, I have a potential patch for it under review at http://reviews.llvm.org/D11832.

Regards, Pierre

goussepi avatar Aug 07 '15 16:08 goussepi

assigned to @tkremenek

llvmbot avatar Mar 19 '15 11:03 llvmbot