facebook / facebook/infer

False positive: memory pointer stored in structures incorrectly marked as leaking

Open
#743 1 comment 0 reactions 0 assignees View on GitHub
false-positive
Dominant language
OCaml
Stars
15.7k
Forks
2.1k
Avg merge
19h 36m
Merged PRs (30d)
13

Description

A function allocates memory and stores the pointer in structure, which is then returned from the function. The memory allocated is incorrectly inferred to be unreachable after function exit.

This issue may be connected to that reported in [#704]

Minimal example below. Infer run from a vanilla docker image created as instructions on http://fbinfer.com/docs/getting-started.html

```C
#include

struct retval {
int i;
float * f;
};

struct retval make_struct(size_t n){
float * f = calloc(n, sizeof(float));
struct retval ret = {0, f};

return ret;
}

int main(void){
struct retval ret = make_struct(5);
free(ret.f);
return EXIT_SUCCESS;
}
```

```
# infer --version
Infer version v0.12.1
Copyright 2009 - present Facebook. All Rights Reserved.

# infer -- clang -c structure_leak.c
Capturing in make/cc mode...
Found 1 source file to analyze in /opt/android-sdk-linux/scrappie/infer-out
Starting analysis...

legend:
"F" analyzing a file
"." analyzing a procedure

F..

Found 1 issue

structure_leak.c:12: error: MEMORY_LEAK
memory dynamically allocated to `ret.f` by call to `calloc()` at line 9, column 17 is not reachable after line 12, column 5
10. struct retval ret = {0, f};
11.
12. > return ret;
13. }
14.

Summary of the reports

MEMORY_LEAK: 1
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.