Skip to content

[pulse] report null arguments dereferenced after a comparison to null - #2169

Open
VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pulse-compared-to-null-precondition
Open

VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pulse-compared-to-null-precondition

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Since e1f88f2 (August 2024), callers passing null to a function that checks p == NULL and
then dereferences p anyway get no NULLPTR_DEREFERENCE: the access fails on
ComparedToNullInThisProcedure before MustBeValid is abduced. That commit deleted the caller
tests showing this (call_test_after_dereference_bad, no_early_return_no_zero_manifest_bad).
Java is hit hardest: bytecode spells every null check as == null.

#include <stddef.h>
struct node { int data; };
void log_message(const char* msg);

int get_data(struct node* n) {
  if (n == NULL) {
    log_message("null node");
  }
  return n->data;
}

int caller(void) {
  return get_data(NULL); // Infer missed: null dereference
}

When an access fails on ComparedToNullInThisProcedure or ConstantDereference and the
pointer's history shows it was read from a pre-condition cell holding it, abduce MustBeValid as
successful accesses do. All null values of a path share one canonical value: the check keeps a
null local, or the result of an unknown call given a null argument, from blaming the caller.

Limitations: with the opt-in COMPARED_TO_NULL_AND_DEREFERENCED, one bug yields two reports (callee
and caller).

Test plan

New c/pulse/compared_to_null.c, with callers not blamed for a null local or unknown-call result
(direct or through a wrapper), and an FP_ test for an unknown noreturn function in the null
branch. Callers restored in cpp/pulse/nullptr.cpp and objc/pulse/null_deref/return_npe_test.m,
added in java/pulse/NullPointerExceptions.java. All suites pass; the ObjC line comes from that
file alone.

@meta-cla meta-cla Bot added the CLA Signed label Oct 2, 2026
@VladimirMakaev
VladimirMakaev force-pushed the pulse-compared-to-null-precondition branch 4 times, most recently from 8fbc190 to 10cc4a7 Compare October 5, 2026 09:04
@VladimirMakaev VladimirMakaev changed the title [DRAFT] [pulse] report null arguments dereferenced after a comparison to null [pulse] report null arguments dereferenced after a comparison to null Oct 5, 2026
@VladimirMakaev
VladimirMakaev marked this pull request as ready for review October 5, 2026 15:27
VladimirMakaev added a commit to VladimirMakaev/infer that referenced this pull request Oct 5, 2026
Since e1f88f2 (August 2024), callers passing null to a function that checks `p == NULL` and
then dereferences `p` anyway get no NULLPTR_DEREFERENCE: the access fails on
`ComparedToNullInThisProcedure` before `MustBeValid` is abduced. That commit deleted the caller
tests showing this (`call_test_after_dereference_bad`, `no_early_return_no_zero_manifest_bad`).
Java is hit hardest: bytecode spells every null check as `== null`.

Upstream-PR: meta#2169
PR-Head: 10cc4a7
PR-Base: 9cc2641
@VladimirMakaev
VladimirMakaev force-pushed the pulse-compared-to-null-precondition branch from 10cc4a7 to d3aa036 Compare October 6, 2026 15:41
@dulmarod

dulmarod commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@jvillard is best placed to look at this one.

@jvillard

jvillard commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

I think it makes sense to also report these errors as null pointer dereference if we do see functions that do this being called with NULL, so this looks nice! This can possibly be simplified as per my inline comments.

Since e1f88f2 (August 2024), callers passing null to a function that checks `p == NULL` and
then dereferences `p` anyway get no NULLPTR_DEREFERENCE: the access fails on
`ComparedToNullInThisProcedure` before `MustBeValid` is abduced. That commit deleted the caller
tests showing this (`call_test_after_dereference_bad`, `no_early_return_no_zero_manifest_bad`).
Java is hit hardest: bytecode spells every null check as `== null`.

```c
#include <stddef.h>
struct node { int data; };
void log_message(const char* msg);

int get_data(struct node* n) {
  if (n == NULL) {
    log_message("null node");
  }
  return n->data;
}

int caller(void) {
  return get_data(NULL); // Infer missed: null dereference
}
```

When an access fails on `ComparedToNullInThisProcedure` or `ConstantDereference` and the
pointer's history shows it was read from a pre-condition cell holding it, abduce `MustBeValid` as
successful accesses do. All null values of a path share one canonical value: the check keeps a
null local, or the result of an unknown call given a null argument, from blaming the caller.

Limitations: with the opt-in COMPARED_TO_NULL_AND_DEREFERENCED, one bug yields two reports (callee
and caller).

## Test plan

New `c/pulse/compared_to_null.c`, with callers not blamed for a null local or unknown-call result
(direct or through a wrapper), and an `FP_` test for an unknown noreturn function in the null
branch. Callers restored in `cpp/pulse/nullptr.cpp` and `objc/pulse/null_deref/return_npe_test.m`,
added in `java/pulse/NullPointerExceptions.java`. All suites pass; the ObjC line comes from that
file alone.
@VladimirMakaev
VladimirMakaev force-pushed the pulse-compared-to-null-precondition branch from 931539b to 0794fbd Compare November 17, 2026 16:32

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants