Skip to content

Commit 2132d73

Browse files
committed
Refactor: inline pointer-arithmetic checks and simplify binop handling
1 parent 1948593 commit 2132d73

1 file changed

Lines changed: 14 additions & 23 deletions

File tree

src/analyses/memOutOfBounds.ml

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ struct
5656
try ID.add x y
5757
with IntDomain.ArithmeticOnIntegerBot _ -> ID.bot_of @@ Cilfacade.ptrdiff_ikind ()
5858

59-
let is_ptr_arith_binop = function
60-
| PlusPI | MinusPI | IndexPI -> true
61-
| _ -> false
62-
63-
let exp_is_explicit_ptr_arith = function
64-
| BinOp (binop, _, _, _) -> is_ptr_arith_binop binop
65-
| _ -> false
66-
6759
let rec exp_contains_a_ptr (exp:exp) =
6860
match exp with
6961
| Const _
@@ -312,10 +304,11 @@ struct
312304
in
313305
let get_deref_offset ptr_deref_type e offs =
314306
let lval_offs = get_lval_offset ptr_deref_type offs in
315-
match exp_is_explicit_ptr_arith e with
316-
| true -> (* Explicit pointer arithmetic is checked separately by check_binop_exp; do not add it here again. *)
307+
match e with
308+
| BinOp ((PlusPI | MinusPI | IndexPI), _, _, _) ->
309+
(* Explicit pointer arithmetic is checked separately by check_binop_exp; do not add it here again. *)
317310
lval_offs
318-
| false ->
311+
| _ ->
319312
add_offsets (get_stored_addr_offset e) lval_offs
320313
in
321314
let ptr_deref_type = get_ptr_deref_type @@ typeOf e in
@@ -351,8 +344,8 @@ struct
351344
end;
352345
begin match e with
353346
| Lval (Var v, _) as lval_exp -> check_no_binop_deref man lval_exp
354-
| BinOp (binop, e1, e2, t) when binop = PlusPI || binop = MinusPI || binop = IndexPI ->
355-
check_binop_exp man binop e1 e2 t;
347+
| BinOp ((PlusPI | MinusPI | IndexPI), e1, e2, _) as binopexp ->
348+
check_binop_exp man binopexp;
356349
check_exp_for_oob_access man ~is_implicitly_derefed e1;
357350
check_exp_for_oob_access man ~is_implicitly_derefed e2
358351
| _ -> check_exp_for_oob_access man ~is_implicitly_derefed e
@@ -420,15 +413,12 @@ struct
420413
| StartOf lval
421414
| AddrOf lval -> check_lval_for_oob_access man ~is_implicitly_derefed lval
422415

423-
and check_binop_exp man binop e1 e2 t =
424-
check_unknown_addr_deref man e1;
425-
let binopexp = BinOp (binop, e1, e2, t) in
426-
let behavior = Undefined MemoryOutOfBoundsAccess in
427-
let cwe_number = 823 in
428-
match binop with
429-
| PlusPI
430-
| IndexPI
431-
| MinusPI ->
416+
and check_binop_exp man binopexp =
417+
match binopexp with
418+
| BinOp ((PlusPI | MinusPI | IndexPI), e1, e2, _) ->
419+
check_unknown_addr_deref man e1;
420+
let behavior = Undefined MemoryOutOfBoundsAccess in
421+
let cwe_number = 823 in
432422
let ptr_size = get_size_of_ptr_target man e1 in
433423
let addr_offs = get_addr_offs man e1 in
434424
let ptr_type = typeOf e1 in
@@ -484,7 +474,8 @@ struct
484474
end
485475
| _ -> M.error "Binary expression %a doesn't have a pointer" d_exp binopexp
486476
end
487-
| _ -> ()
477+
| _ ->
478+
M.error "check_binop_exp called with non-pointer-arithmetic expression %a" d_exp binopexp
488479

489480
(* For memset() and memcpy() *)
490481
let check_count man fun_name ptr n =

0 commit comments

Comments
 (0)