From 55253a2763e754ec96e7d71ec066e56df2aa5200 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Tue, 1 Jun 2021 05:00:20 -0700 Subject: [PATCH] [linereader] simplify and remove exceptions Summary: Auditing exception handling so as to remove catch-all clauses that could potentially swallow exceptions from the restart scheduler. Reviewed By: jvillard Differential Revision: D28772739 fbshipit-source-id: 99a8d516d --- infer/src/base/LineReader.ml | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/infer/src/base/LineReader.ml b/infer/src/base/LineReader.ml index 23038ba00..30ff1e541 100644 --- a/infer/src/base/LineReader.ml +++ b/infer/src/base/LineReader.ml @@ -15,33 +15,19 @@ type t = (SourceFile.t, string array) Hashtbl.t let create () = Hashtbl.create 1 let read_file fname = - let cin = In_channel.create fname in - let lines = ref [] in - try - while true do - let line_raw = In_channel.input_line_exn cin in - let line = - let len = String.length line_raw in - if len > 0 && Char.equal line_raw.[len - 1] '\r' then - String.sub line_raw ~pos:0 ~len:(len - 1) - else line_raw - in - lines := line :: !lines - done ; - assert false (* execution never reaches here *) - with End_of_file -> - In_channel.close cin ; - Array.of_list_rev !lines + try In_channel.read_lines ~fix_win_eol:true fname |> Array.of_list |> Option.some + with Sys_error _ -> None let file_data (hash : t) fname = - try Some (Hashtbl.find hash fname) - with Caml.Not_found -> ( - try - let lines_arr = read_file (SourceFile.to_abs_path fname) in + match Hashtbl.find_opt hash fname with + | None -> + let open IOption.Let_syntax in + let+ lines_arr = read_file (SourceFile.to_abs_path fname) in Hashtbl.add hash fname lines_arr ; - Some lines_arr - with exn when SymOp.exn_not_failure exn -> None ) + lines_arr + | res -> + res let from_file_linenum hash fname linenum =