facebook / facebook/infer

NULLPTR_DEREFERENCE false positive when we check the pointer first.

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

Description

In the case below we never dereference mysql_default when it is null so the infer warning is a false positive.

x.c:
```c
#include
#include

static char *mysql_default = 0;

#define IS_MAXSCALE()\
((mysql_default && strstr(mysql_default, "maxScale")) ||\
(getenv("srv")!=NULL && (strcmp(getenv("srv"), "maxscale") == 0 ||\
strcmp(getenv("srv"), "skysql-ha") == 0)))

#define SKIP_MAXSCALE \
if (IS_MAXSCALE()) \
{ \
return; \
}

void test()
{
SKIP_MAXSCALE;
}
```

```
$ infer -- clang -c /tmp/x.c
Capturing in make/cc mode...
Found 1 source file to analyze in /home/dan/repos/mariadb-connector-c/infer-out
3/3 [################################################################################] 100% 30.2ms

/tmp/x.c:21: error: Null Dereference(NULLPTR_DEREFERENCE)
`mysql_default` could be null (null value originating from line 21) and is dereferenced.
19. void test()
20. {
21. SKIP_MAXSCALE;
^
22. }

/tmp/x.c:21: error: Null Dereference(NULLPTR_DEREFERENCE)
null (null value originating from line 21) is dereferenced.
19. void test()
20. {
21. SKIP_MAXSCALE;
^
22. }

/tmp/x.c:21: error: Null Dereference(NULLPTR_DEREFERENCE)
`mysql_default` could be null (null value originating from line 21) and is dereferenced.
19. void test()
20. {
21. SKIP_MAXSCALE;
^
22. }

/tmp/x.c:21: error: Null Dereference(NULLPTR_DEREFERENCE)
null (null value originating from line 21) is dereferenced.
19. void test()
20. {
21. SKIP_MAXSCALE;
^
22. }

Found 4 issues
Issue Type(ISSUED_TYPE_ID): #
Null Dereference(NULLPTR_DEREFERENCE): 4
```

```
$ infer --version
Infer version v1.2.0-49969b1487
```

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.