Skip to content

C RESOURCE_LEAK false positive appears to be caused by calls to stat() or fstat() #1351

Description

@matt-herunter

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 <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions