Fix IntLit.to_int interface and uses.

Summary: `IntLit.to_int` could raise, was not documented until recently and was not named `_exn`.  Switch to option type and fix uses.

Reviewed By: jeremydubreil

Differential Revision: D8865525

fbshipit-source-id: f5ec2f221
master
Nikos Gorogiannis 6 years ago committed by Facebook Github Bot
parent 9670d3c700
commit c2416defed

@ -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 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 let to_float (_, i, _) = Int64.to_float i

@ -87,8 +87,9 @@ val shift_right : t -> t -> t
val sub : t -> t -> t val sub : t -> t -> t
val to_int : t -> int val to_int : t -> int option
(** throws exception if literal is not representable as an OCaml int *)
val to_int_exn : t -> int
val to_float : t -> float val to_float : t -> float

@ -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 match (n_lexp1, n_lexp2, n_lexp3) with
| Exp.Const (Const.Cstr str1), Exp.Const (Const.Cstr str2), Exp.Const (Const.Cint n_sil) | 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 try
let parts = Str.split (Str.regexp_string str2) str1 in let parts = Str.split (Str.regexp_string str2) str1 in
let n_part = List.nth_exn parts n in let n_part = List.nth_exn parts n in

@ -388,7 +388,7 @@ let reason_to_skip ~callee_desc : string option =
(** In case of constant string dereference, return the result immediately *) (** In case of constant string dereference, return the result immediately *)
let check_constant_string_dereference lexp = let check_constant_string_dereference lexp =
let string_lookup s n = 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) Exp.int (IntLit.of_int c)
in in
match lexp with match lexp with

@ -269,7 +269,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let rec decl_local pname ~node_hash location loc typ ~inst_num ~dimension mem = let rec decl_local pname ~node_hash location loc typ ~inst_num ~dimension mem =
match typ.Typ.desc with match typ.Typ.desc with
| Typ.Tarray {elt= typ; length; stride} -> | 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 BoUtils.Exec.decl_local_array ~decl_local pname ~node_hash location loc typ ~length
?stride ~inst_num ~dimension mem ?stride ~inst_num ~dimension mem
| Typ.Tstruct typname -> ( | Typ.Tstruct typname -> (

@ -177,7 +177,7 @@ let set_array_length array length_exp =
match array with match array with
| Exp.Lvar array_pvar, {Typ.desc= Typ.Tarray {elt; stride}} -> | Exp.Lvar array_pvar, {Typ.desc= Typ.Tarray {elt; stride}} ->
let length = Sem.eval length_exp mem |> Dom.Val.get_itv in 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 = let v =
Sem.eval_array_alloc pname ~node_hash elt ~stride ~offset:Itv.zero ~size:length Sem.eval_array_alloc pname ~node_hash elt ~stride ~offset:Itv.zero ~size:length
~inst_num:0 ~dimension:1 ~inst_num:0 ~dimension:1

@ -14,8 +14,8 @@ open! AbstractDomain.Types
open BufferOverrunDomain open BufferOverrunDomain
let eval_const : Const.t -> Val.t = function let eval_const : Const.t -> Val.t = function
| Const.Cint intlit -> ( | Const.Cint intlit ->
try Val.of_int (IntLit.to_int intlit) with _ -> Val.Itv.top ) Option.value_map ~default:Val.Itv.top ~f:Val.of_int (IntLit.to_int intlit)
| Const.Cfloat f -> | Const.Cfloat f ->
f |> int_of_float |> Val.of_int f |> int_of_float |> Val.of_int
| _ -> | _ ->
@ -60,9 +60,9 @@ let rec sizeof (typ: Typ.t) : int =
| Typ.Tstruct _ | Typ.TVar _ -> | Typ.Tstruct _ | Typ.TVar _ ->
4 (* TODO *) 4 (* TODO *)
| Typ.Tarray {length= Some length; stride= Some stride} -> | 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} -> | Typ.Tarray {elt; length= Some length; stride= None} ->
sizeof elt * IntLit.to_int length sizeof elt * IntLit.to_int_exn length
| _ -> | _ ->
4 4

@ -94,7 +94,7 @@ module Exec = struct
let i = Dom.Val.get_itv (Sem.eval dyn_length mem) in let i = Dom.Val.get_itv (Sem.eval dyn_length mem) in
Itv.plus i length ) Itv.plus i length )
in in
let stride = Option.map stride ~f:IntLit.to_int in let stride = Option.map stride ~f:IntLit.to_int_exn in
let v = let v =
Sem.eval_array_alloc pname ~node_hash typ ~stride ~offset:Itv.zero ~size:length Sem.eval_array_alloc pname ~node_hash typ ~stride ~offset:Itv.zero ~size:length
~inst_num ~dimension ~inst_num ~dimension

@ -1912,7 +1912,7 @@ let of_bool = function
let of_int : int -> astate = fun n -> NonBottom (ItvPure.of_int n) 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 = let of_int64 : Int64.t -> astate =
fun n -> Int64.to_int n |> Option.value_map ~f:of_int ~default:top fun n -> Int64.to_int n |> Option.value_map ~f:of_int ~default:top

@ -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) 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 |> collect_controls trans_state.context.procdesc |> mk_trans_result exp_typ
| Tarray {elt= field_typ; length= Some n} -> | 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 let indices = CGeneral_utils.list_range 0 (size - 1) in
List.map indices ~f:(fun i -> List.map indices ~f:(fun i ->
let idx_exp = Exp.Const (Const.Cint (IntLit.of_int i)) in let idx_exp = Exp.Const (Const.Cint (IntLit.of_int i)) in

@ -330,7 +330,9 @@ module SinkKind = struct
match HilExp.eval exp with match HilExp.eval exp with
| Some (Const.Cint i) -> | Some (Const.Cint i) ->
(* check if the data kind might be CURLOPT_URL *) (* 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 *) (* can't statically resolve data kind; taint it just in case *)
taint_after_nth 1 URL actuals ) taint_after_nth 1 URL actuals )

Loading…
Cancel
Save