diff --git a/infer/src/IR/IntLit.ml b/infer/src/IR/IntLit.ml index f293de05e..c143cbc1d 100644 --- a/infer/src/IR/IntLit.ml +++ b/infer/src/IR/IntLit.ml @@ -59,7 +59,9 @@ let of_int32 i = of_int64 (Int64.of_int32 i) let of_int i = of_int64 (Int64.of_int i) -let to_int (_, i, _) = Int64.to_int_exn i +let to_int (_, i, _) = Int64.to_int i + +let to_int_exn (_, i, _) = Int64.to_int_exn i let to_float (_, i, _) = Int64.to_float i diff --git a/infer/src/IR/IntLit.mli b/infer/src/IR/IntLit.mli index b4c7bd056..d186e981d 100644 --- a/infer/src/IR/IntLit.mli +++ b/infer/src/IR/IntLit.mli @@ -87,8 +87,9 @@ val shift_right : t -> t -> t val sub : t -> t -> t -val to_int : t -> int -(** throws exception if literal is not representable as an OCaml int *) +val to_int : t -> int option + +val to_int_exn : t -> int val to_float : t -> float diff --git a/infer/src/biabduction/BuiltinDefn.ml b/infer/src/biabduction/BuiltinDefn.ml index 94893be7d..43644fb1a 100644 --- a/infer/src/biabduction/BuiltinDefn.ml +++ b/infer/src/biabduction/BuiltinDefn.ml @@ -717,7 +717,7 @@ let execute___split_get_nth {Builtin.tenv; pdesc; prop_; path; ret_id_typ; args} match (n_lexp1, n_lexp2, n_lexp3) with | Exp.Const (Const.Cstr str1), Exp.Const (Const.Cstr str2), Exp.Const (Const.Cint n_sil) -> ( - let n = IntLit.to_int n_sil in + let n = IntLit.to_int_exn n_sil in try let parts = Str.split (Str.regexp_string str2) str1 in let n_part = List.nth_exn parts n in diff --git a/infer/src/biabduction/SymExec.ml b/infer/src/biabduction/SymExec.ml index 1e161bbc6..83d60ff23 100644 --- a/infer/src/biabduction/SymExec.ml +++ b/infer/src/biabduction/SymExec.ml @@ -388,7 +388,7 @@ let reason_to_skip ~callee_desc : string option = (** In case of constant string dereference, return the result immediately *) let check_constant_string_dereference lexp = let string_lookup s n = - let c = try Char.to_int s.[IntLit.to_int n] with Invalid_argument _ -> 0 in + let c = try Char.to_int s.[IntLit.to_int_exn n] with Invalid_argument _ -> 0 in Exp.int (IntLit.of_int c) in match lexp with diff --git a/infer/src/bufferoverrun/bufferOverrunChecker.ml b/infer/src/bufferoverrun/bufferOverrunChecker.ml index b58716146..56d2a3b01 100644 --- a/infer/src/bufferoverrun/bufferOverrunChecker.ml +++ b/infer/src/bufferoverrun/bufferOverrunChecker.ml @@ -269,7 +269,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct let rec decl_local pname ~node_hash location loc typ ~inst_num ~dimension mem = match typ.Typ.desc with | Typ.Tarray {elt= typ; length; stride} -> - let stride = Option.map ~f:IntLit.to_int stride in + let stride = Option.map ~f:IntLit.to_int_exn stride in BoUtils.Exec.decl_local_array ~decl_local pname ~node_hash location loc typ ~length ?stride ~inst_num ~dimension mem | Typ.Tstruct typname -> ( diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index 1c82979e9..e407a6acc 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -177,7 +177,7 @@ let set_array_length array length_exp = match array with | Exp.Lvar array_pvar, {Typ.desc= Typ.Tarray {elt; stride}} -> let length = Sem.eval length_exp mem |> Dom.Val.get_itv in - let stride = Option.map ~f:IntLit.to_int stride in + let stride = Option.map ~f:IntLit.to_int_exn stride in let v = Sem.eval_array_alloc pname ~node_hash elt ~stride ~offset:Itv.zero ~size:length ~inst_num:0 ~dimension:1 diff --git a/infer/src/bufferoverrun/bufferOverrunSemantics.ml b/infer/src/bufferoverrun/bufferOverrunSemantics.ml index 5f4d47bb2..9699146fe 100644 --- a/infer/src/bufferoverrun/bufferOverrunSemantics.ml +++ b/infer/src/bufferoverrun/bufferOverrunSemantics.ml @@ -14,8 +14,8 @@ open! AbstractDomain.Types open BufferOverrunDomain let eval_const : Const.t -> Val.t = function - | Const.Cint intlit -> ( - try Val.of_int (IntLit.to_int intlit) with _ -> Val.Itv.top ) + | Const.Cint intlit -> + Option.value_map ~default:Val.Itv.top ~f:Val.of_int (IntLit.to_int intlit) | Const.Cfloat f -> f |> int_of_float |> Val.of_int | _ -> @@ -60,9 +60,9 @@ let rec sizeof (typ: Typ.t) : int = | Typ.Tstruct _ | Typ.TVar _ -> 4 (* TODO *) | Typ.Tarray {length= Some length; stride= Some stride} -> - IntLit.to_int stride * IntLit.to_int length + IntLit.to_int_exn stride * IntLit.to_int_exn length | Typ.Tarray {elt; length= Some length; stride= None} -> - sizeof elt * IntLit.to_int length + sizeof elt * IntLit.to_int_exn length | _ -> 4 diff --git a/infer/src/bufferoverrun/bufferOverrunUtils.ml b/infer/src/bufferoverrun/bufferOverrunUtils.ml index 79e259958..310bd2393 100644 --- a/infer/src/bufferoverrun/bufferOverrunUtils.ml +++ b/infer/src/bufferoverrun/bufferOverrunUtils.ml @@ -94,7 +94,7 @@ module Exec = struct let i = Dom.Val.get_itv (Sem.eval dyn_length mem) in Itv.plus i length ) in - let stride = Option.map stride ~f:IntLit.to_int in + let stride = Option.map stride ~f:IntLit.to_int_exn in let v = Sem.eval_array_alloc pname ~node_hash typ ~stride ~offset:Itv.zero ~size:length ~inst_num ~dimension diff --git a/infer/src/bufferoverrun/itv.ml b/infer/src/bufferoverrun/itv.ml index f587d5733..b383000a7 100644 --- a/infer/src/bufferoverrun/itv.ml +++ b/infer/src/bufferoverrun/itv.ml @@ -1912,7 +1912,7 @@ let of_bool = function let of_int : int -> astate = fun n -> NonBottom (ItvPure.of_int n) -let of_int_lit n = try of_int (IntLit.to_int n) with _ -> top +let of_int_lit n = Option.value_map ~default:top ~f:of_int (IntLit.to_int n) let of_int64 : Int64.t -> astate = fun n -> Int64.to_int n |> Option.value_map ~f:of_int ~default:top diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index eb0b59c7e..31d0d2bf9 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -411,7 +411,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s List.map field_exp_typs ~f:(fun exp_typ -> (fill_typ_with_zero exp_typ).control) |> collect_controls trans_state.context.procdesc |> mk_trans_result exp_typ | Tarray {elt= field_typ; length= Some n} -> - let size = IntLit.to_int n in + let size = IntLit.to_int_exn n in let indices = CGeneral_utils.list_range 0 (size - 1) in List.map indices ~f:(fun i -> let idx_exp = Exp.Const (Const.Cint (IntLit.of_int i)) in diff --git a/infer/src/quandary/ClangTrace.ml b/infer/src/quandary/ClangTrace.ml index 0b071a6fd..960a01817 100644 --- a/infer/src/quandary/ClangTrace.ml +++ b/infer/src/quandary/ClangTrace.ml @@ -330,7 +330,9 @@ module SinkKind = struct match HilExp.eval exp with | Some (Const.Cint i) -> (* check if the data kind might be CURLOPT_URL *) - if controls_request (IntLit.to_int i) then taint_after_nth 1 URL actuals else None + IntLit.to_int i + |> Option.bind ~f:(fun n -> + if controls_request n then taint_after_nth 1 URL actuals else None ) | _ -> (* can't statically resolve data kind; taint it just in case *) taint_after_nth 1 URL actuals )