https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128979
--- Comment #1 from 何天行 <tianxinghe at smail dot nju.edu.cn> ---
I dug into where this happens and I think it is the binop_svalue branch of
region_model::eval_condition (gcc/analyzer/region-model.cc:5051 in my 17.0.0
snapshot):
c
else if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ())
{
/* Treat offsets from a non-NULL pointer as being non-NULL. This
isn't strictly true, in that eventually ptr++ will wrap
around and be NULL, but it won't occur in practise and thus
can be used to suppress effectively false positives that we
shouldn't warn for. */
if (binop->get_op () == POINTER_PLUS_EXPR)
{
tristate lhs_ts = eval_condition (binop->get_arg0 (), op, rhs);
if (lhs_ts.is_known ())
return lhs_ts;
}
}
The comment says "offsets from a non-NULL pointer", but the code does not check
that binop->get_arg0 () is non-NULL. In the reproducer:
- sp < ep has the constant on the LHS, so eval_condition first swaps the
operands (constant to the RHS) and the comparison becomes ep > sp;
- the binop branch then evaluates eval_condition (NULL, GT, NULL), which is
false, and the branch is pruned.
The sp == ep variant becomes eval_condition (NULL, EQ, NULL) → true, which
matches the observed behaviour (that variant does report the dereference).