Skip to content

Commit abb23aa

Browse files
committed
Allow pointer arithmethic on UnknownPtr to give NullPtr as well
1 parent 00a009a commit abb23aa

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/analyses/base.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,12 @@ struct
312312
| `Top -> AD.top_ptr
313313
end
314314
| UnknownPtr
315-
| StrPtr _ -> AD.unknown_ptr
315+
| StrPtr _ ->
316+
begin match ID.equal_to Z.zero n with
317+
| `Eq -> AD.singleton addr (* remains unchanged *)
318+
| `Neq
319+
| `Top -> AD.top_ptr
320+
end
316321
in
317322
let ad_concat_map f a = AD.fold (fun a acc -> AD.join (f a) acc) a (AD.empty ()) in
318323
let addToAddrOp p (n:ID.t):value =

tests/regression/01-cpa/79-null-ptr-arith.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ int main(void) {
33
int i;
44
int *q = p + i;
55
*q = 42; // WARN (may NULL dereference)
6+
7+
if (p) {
8+
q = p + i;
9+
*q = 42; // WARN (may NULL dereference)
10+
11+
if (i == 0) {
12+
q = p + i;
13+
*q = 42; // NOWARN (no NULL dereference)
14+
}
15+
else {
16+
q = p + i;
17+
*q = 42; // WARN (may NULL dereference)
18+
}
19+
}
620
}

0 commit comments

Comments
 (0)