C RESOURCE_LEAK false positive appears to be caused by calls to stat() or fstat()
- Dominant language
- OCaml
- Stars
- 15.7k
- Forks
- 2.1k
- Avg merge
- 19h 36m
- Merged PRs (30d)
- 13
Description
**Infer version:**
```
mherunter@IGCA180701-Linux:~$ infer --version
Infer version v0.17.0
Copyright 2009 - present Facebook. All Rights Reserved.
```
**System:**
```
mherunter@IGCA180701-Linux:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.2 LTS
Release: 18.04
Codename: bionic
```
**Infer command:**
```
mherunter@IGCA180701-Linux:~/Firmware/tests/infer$ make clean && infer run -- make
rm -f test
Capturing in make/cc mode...
gcc -Wall -O2 -o test test.c
Found 1 source file to analyze in /home/mherunter/Firmware/tests/infer/infer-out
Analysis finished in 359mss
Found 1 issue
test.c:18: error: RESOURCE_LEAK
resource of type `_IO_FILE` acquired by call to `fopen()` at line 13, column 13 is not released after line 18, column 2.
16. return 1;
17. }
18. > fp = check_file_size_and_reopen(fp);
19.
20. if (fp) {
Summary of the reports
RESOURCE_LEAK: 1
```
**Code**
```
#include
#include
#include
#define FILE_MAX_SIZE 256000
#define FILE_NAME "dump.log"
#define LAST_FILE_NAME "dump.log2"
FILE* check_file_size_and_reopen(FILE* fp);
int main(void) {
FILE *fp = fopen(FILE_NAME, "a+");
if (!fp) {
return 1;
}
fp = check_file_size_and_reopen(fp);
if (fp) {
fclose(fp);
}
return 0;
}
FILE* check_file_size_and_reopen(FILE* fp) {
struct stat info;
int fd = fileno(fp);
(void)fd;
fstat(fd, &info);
if (info.st_size <= FILE_MAX_SIZE) {
return fp;
}
fclose(fp);
rename(FILE_NAME, LAST_FILE_NAME);
return fopen(FILE_NAME,"a+");
}
```
**Notes:**
I get the same result if the call to `fstat()` is replaced with a call to `stat()` for a completely unrelated file (such as ~/.bashrc). If I remove the call to `stat()`/`fstat()` then I get no warnings.
Contributor guide
Assessment
This issue has not been assessed yet.