Skip to content

NULLPTR_DEREFERENCE false positive when we check the pointer first. #1936

@grooverdan

Description

@grooverdan

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

x.c:

#include <stdlib.h>
#include <string.h>

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions