[sledge] Protect Trace.parse from invalid user input

Summary:
Trace.parse is exposed across a module boundary, so handle its own
exceptions and return an `error`.

Reviewed By: mbouaziz

Differential Revision: D14081602

fbshipit-source-id: 4087c5d5b
master
Josh Berdine 6 years ago committed by Facebook Github Bot
parent 5a3718a098
commit 65c446f0fb

@ -9,8 +9,9 @@
let trace_conv = let trace_conv =
let parse s = let parse s =
try Ok (Trace.parse s).trace_mods_funs with _ -> match Trace.parse s with
Error (`Msg ("Invalid trace spec: " ^ s)) | Ok c -> Ok c.trace_mods_funs
| Error err -> Error err
in in
let print fs c = let print fs c =
let pf fmt = Format.fprintf fs fmt in let pf fmt = Format.fprintf fs fmt in

@ -24,6 +24,7 @@ let all = {none with trace_all= true}
let config : config ref = ref none let config : config ref = ref none
let parse s = let parse s =
try
let default = Map.empty (module String) in let default = Map.empty (module String) in
let index_from s i = let index_from s i =
Option.merge ~f:min Option.merge ~f:min
@ -64,13 +65,14 @@ let parse s =
Map.set m ~key:mod_name Map.set m ~key:mod_name
~data: ~data:
{ trace_mod { trace_mod
; trace_funs= Map.set trace_funs ~key:fun_name ~data:enabled ; trace_funs=
} Map.set trace_funs ~key:fun_name ~data:enabled }
| mod_name, None, enabled -> | mod_name, None, enabled ->
Map.set m ~key:mod_name Map.set m ~key:mod_name
~data:{trace_mod= Some enabled; trace_funs= default} ) ~data:{trace_mod= Some enabled; trace_funs= default} )
in in
{none with trace_mods_funs} Ok {none with trace_mods_funs}
with Assert_failure _ -> Error (`Msg ("Invalid trace spec: " ^ s))
let init ?(margin = 300) ~config:c () = let init ?(margin = 300) ~config:c () =
Format.set_margin margin ; Format.set_margin margin ;

@ -23,7 +23,7 @@ type config =
val none : config val none : config
val all : config val all : config
val parse : string -> config val parse : string -> (config, [> `Msg of string]) result
val init : ?margin:int -> config:config -> unit -> unit val init : ?margin:int -> config:config -> unit -> unit
(** Initialize the configuration of debug tracing. *) (** Initialize the configuration of debug tracing. *)

Loading…
Cancel
Save