From ccb2d23c5b007a5dc7f606b42c73ee3f6c52e52d Mon Sep 17 00:00:00 2001 From: David Pichardie Date: Fri, 17 Apr 2020 09:34:30 -0700 Subject: [PATCH] Make source file baby parser fails silently Summary: The java source file parser should refuse to run on non .java file. Also, as we expect some autogenerated source files to break Java official syntax, we catch parsing error silently and cancel location recording for them. Reviewed By: jvillard Differential Revision: D21089587 fbshipit-source-id: 35f1a1e28 --- infer/src/java/jSourceFileInfo.mll | 55 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/infer/src/java/jSourceFileInfo.mll b/infer/src/java/jSourceFileInfo.mll index 16f1146e6..9a7924966 100644 --- a/infer/src/java/jSourceFileInfo.mll +++ b/infer/src/java/jSourceFileInfo.mll @@ -372,30 +372,33 @@ and skip_comments action = parse (** We scan source file [file] and record location of each class declaration *) let collect_class_location (program:JClasspath.program) (file:SourceFile.t) = - let cin = In_channel.create (SourceFile.to_abs_path file) in - let stack = [] in - let record_location ~classname ~col ~line = - let loc : Location.t = { line; col; file } in - let cn : JBasics.class_name = JBasics.make_cn classname in - Logging.debug Capture Verbose "set_java_location %s with location %a@." - (JBasics.cn_name cn) Location.pp_file_pos loc; - JClasspath.set_java_location program cn loc in - try ( - class_scan { record_location; stack; } (from_channel cin) ; - In_channel.close cin ) - with - | Failure s -> - raise - (Failure - (Printf.sprintf "Error parsing source file %s\n%s" (SourceFile.to_abs_path file) s)) - | Missing_opening_bracket -> - raise - (Failure (Printf.sprintf "Missing opening bracket error while parsing source file %s\n" - (SourceFile.to_abs_path file))) - | Missing_opening_parenthesis -> - raise - (Failure - (Printf.sprintf "Missing opening parenthesis error while parsing source file %s\n" - (SourceFile.to_abs_path file))) - + let path = SourceFile.to_abs_path file in + if String.is_suffix path ~suffix:".java" then ( + let cin = In_channel.create path in + let stack = [] in + let record_location ~classname ~col ~line = + let loc : Location.t = { line; col; file } in + let cn : JBasics.class_name = JBasics.make_cn classname in + Logging.debug Capture Verbose "set_java_location %s with location %a@." + (JBasics.cn_name cn) Location.pp_file_pos loc; + JClasspath.set_java_location program cn loc in + try ( + class_scan { record_location; stack; } (from_channel cin) ; + In_channel.close cin ) + with + | Failure s -> + Logging.debug Capture Verbose "Error parsing source file %s\n%s" + (SourceFile.to_abs_path file) s; + In_channel.close cin + | Missing_opening_bracket -> + Logging.debug Capture Verbose + "Missing opening bracket error while parsing source file %s\n" + (SourceFile.to_abs_path file); + In_channel.close cin + | Missing_opening_parenthesis -> + Logging.debug Capture Verbose + "Missing opening parenthesis error while parsing source file %s\n" + (SourceFile.to_abs_path file); + In_channel.close cin + ) }