clang-analyzer-unix.Malloc memory released false positive with defer
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
In this C program, the clang analyzer believes memory is released at the use of `defer`. This is similar to issue #175639. However, the specific warning, and use of `defer`, is different so I opened a new issue.
Clang compiler and tidy version.
```txt
clang version 22.1.0-rc3 (https://github.com/llvm/llvm-project 8530a2cf135c6091438b92b3778d1f23bf77d905)
Target: x86_64-unknown-linux-gnu
Thread model: posix
```
Program compiled with the `-fdefer-ts` flag.
```c
#include
#include
int
main(void) {
int *num = malloc(sizeof(int));
defer free(num);
*num = 1;
return 0;
}
```
Clang tidy output.
```txt
:8:10: warning: Use of memory after it is released [clang-analyzer-unix.Malloc]
8 | *num = 1;
| ~~~~ ^
:6:16: note: Memory is allocated
6 | int *num = malloc(sizeof(int));
| ^~~~~~~~~~~~~~~~~~~
:7:11: note: Memory is released
7 | defer free(num);
| ^~~~~~~~~
:8:10: note: Use of memory after it is released
8 | *num = 1;
| ~~~~ ^
```
See https://godbolt.org/z/Y8xE9xqY7
Contributor guide
Research direction
Start by reproducing the warning with the provided C program, clang-tidy, and the -fdefer-ts flag. Inspect handling of clang-analyzer-unix.Malloc around defer and compare with issue #175639; done means the valid use after defer is no longer reported as use-after-release.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100