llvm / llvm/llvm-project

[clang][analyzer] False positve in core.NullDereference checker

Open
#219,497 3 comments 0 reactions 0 assignees View on GitHub
clang:static analyzer false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Description:**
Analyzer does not preserve the value written by `memset(c, 0, sizeof(*c))` when `c` is a pointer parameter. As a result, it considers `c->head != 0` feasible after the entire object was zeroed, producing a false `core.NullDereference` warning.

**Minimal reproducer:**
```c
#include

void clang_analyzer_eval(int);

struct container { int *head; };

void test(struct container *c)
{
int *tail = 0;

memset(c, 0, sizeof(*c));

clang_analyzer_eval(c->head == 0);
// Expected: TRUE
// Actual: TRUE and FALSE

if (c->head != 0)
*tail = 1; // false positive
}
```

Replacing `memset(c, 0, sizeof(*c))` with `c->head = 0` suppresses the warning. Compiler explorer reproducer can be found [here](https://compiler-explorer.com/z/KGvsGhbqc).

Contributor guide

Open the contributing guide

Research direction

Start with the minimal C reproducer in the issue and trace the core.NullDereference checker’s handling of memset(c, 0, sizeof(*c)) for a pointer parameter. Run the analyzer on the reproducer; done means clang_analyzer_eval reports only TRUE and no warning is produced for the guarded dereference.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.