https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128979

            Bug ID: 128979
           Summary: analyzer: NULL + n is folded to NULL, so a branch
                    guarded by p < p + n is pruned; no diagnostic for the
                    null pointer arithmetic
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: tianxinghe at smail dot nju.edu.cn
  Target Milestone: ---

Summary
-fanalyzer silently prunes a branch guarded by a comparison involving a
NULL-based POINTER_PLUS_EXPR, without diagnosing either the null pointer
arithmetic or the null dereference in the branch.

Bug details
- Component: gcc / analyzer
- Version: GCC 17.0.0
- Flags: -O0 -fanalyzer
- Keywords: false-negative

Minimal reproducer

#include <stddef.h>

void f(size_t n) {
  unsigned char *sp = 0;
  unsigned char *ep = sp + n;      /* null pointer arithmetic: undefined
behavior */
  if (sp < ep)
    *(volatile unsigned char *)sp = 1;   /* store to null */
}

int main(int argc, char **argv) {
  f((size_t)argc);
  return 0;
}

Compile:
gcc -O0 -fanalyzer -c min.c -o /dev/null
echo $?
The command exits successfully and emits no analyzer diagnostics.

Observed behavior
GCC emits no diagnostic for either:
1. the null-based pointer arithmetic at sp + n; or
2. the null store in the body of the if.

The question is whether the current modeling and branch-pruning behavior is
intentional, and whether the lack of a diagnostic for this operation is an
expected limitation.

Runtime observation
On the tested build, compiling with UBSan:
gcc -fsanitize=undefined -fno-sanitize-recover=undefined -O0 min.c -o min
./min
produces:
min.c:7:5: runtime error: store to null pointer of type 'volatile unsigned
char'

Evidence that the branch is pruned
An independent null dereference was added inside the branch:
if (sp < ep) {
  *(volatile unsigned char *)sp = 1;
  {
    volatile int *q = 0;
    *q = 1;                       /* independent probe */
  }
}
The independent probe is reported when sp is not set to NULL. After setting sp
to NULL, the probe is not reported either.

This suggests that the branch body is not analyzed, rather than the analyzer
entering the branch and merely losing track of sp.
Comparison experiments
The following variants were tested with the same general setup. “Reported”
means that a diagnostic for a dereference placed inside the body was emitted.
Condition / setup       Diagnostic inside body?
if (sp < ep)    No
if (sp != ep)   No
if (sp == ep)   Yes
if (ep != 0)    No
if (ep == 0)    Yes
if (ep == 1) (also tested with 2, 8, and 255)   Yes
if ((size_t)sp < (size_t)ep)    Yes
if (n)  Yes
ep = row + n with a non-NULL base row, while sp is NULL Yes; the null
dereference of sp is reported

These controls suggest that the behavior is specific to the interaction between
a NULL base, pointer arithmetic, and pointer comparison. In particular,
replacing the pointer comparison with an integer comparison, or using a
non-NULL base for the pointer arithmetic, does not produce the same branch
pruning.
What the analyzer appears to do
The observed behavior is consistent with the analyzer treating:
ep = (unsigned char *)NULL + n;
as equivalent to the NULL base for purposes of the subsequent comparison. It
then appears to resolve:
sp < ep
as equivalent to:
NULL < NULL
and conclude that the condition is false.

Command tested:
gcc -O0 -fanalyzer -Wanalyzer-too-complex \
  --param analyzer-max-enodes-per-program-point=4000 \
  -c min.c -o /dev/null

Questions
Is the current treatment of a NULL-based POINTER_PLUS_EXPR during pointer
comparisons intentional? The source comment describing this heuristic refers to
offsets from a non-NULL pointer, but the observed behavior suggests that it
also applies when the base pointer is NULL.

If this behavior is intentional because the pointer arithmetic already invokes
undefined behavior, is the resulting definite branch pruning without any
diagnostic expected? Alternatively, should the analyzer treat this comparison
as unknown or diagnose the invalid pointer arithmetic?

Related PRs (not duplicates)
- PR analyzer/125304 — pointer-comparison rewrite, fixed by
r17-609-g573b66baa6cb8d (2026-05-19). That change compares region offsets
rather than region identity. This report appears to involve a NULL constant and
a binop_svalue representing POINTER_PLUS_EXPR, rather than two region values.
- PR analyzer/109736 — pointer-arithmetic comparison precision; the reported
direction is the opposite (unknown where a known Boolean result may be
possible).
- PR analyzer/105891 — request for a warning about pointer arithmetic on
non-arrays. This report may be related to the absence of a diagnostic for null
pointer arithmetic, but the branch-pruning behavior is a separate question.
Versions tested
GCC build       Result
17.0.0 20261026 (x86_64-pc-linux-gnu, built from source)        No analyzer
diagnostics
17.0.0 20261115 (x86_64-pc-linux-gnu, built from source)        No analyzer
diagnostics


Both builds reproduce with:
gcc -O0 -fanalyzer -c min.c -o /dev/null
The result is unchanged with -Wanalyzer-too-complex and --param
analyzer-max-enodes-per-program-point=4000.

Additional comparison
Clang Static Analyzer 24.0.0git (f44c6ac743d5) reports the null pointer
arithmetic in the minimal reproducer:
min.c:4:26: warning: Addition of a null pointer (from variable 'sp') and a
nonzero integer value
(from variable 'n') results in undefined behavior [core.NullPointerArithm]
  • [Bug analyzer/128979] New:... tianxinghe at smail dot nju.edu.cn via Gcc-bugs

Reply via email to