Permalink
Browse files

changes an error message in case of empty file (#896)

* changes an error message in case of empty file

This PR changes an error message if bap is using with an emptyt file:
```
$ touch /tmp/foo.txt
$ bap /tmp/foo.txt
Failed to create a project: ("File format is not supported: expected executable, library or kernel module"
  "Raised at file \"src/import0.ml\" (inlined), line 234, characters 22-32\
 \nCalled from file \"src/error.ml\" (inlined), line 9, characters 14-30\
 \nCalled from file \"src/or_error.ml\", line 70, characters 17-32\
 \nCalled from file \"lib/bap/bap_project.ml\", line 264, characters 47-54\
 \nCalled from file \"src/or_error.ml\", line 62, characters 9-15\
 \n")

```

* edited message

* better output + check input file

* added strip

* refactored a little

* updated testsuite
  • Loading branch information...
gitoleg authored and ivg committed Nov 12, 2018
1 parent ded29fd commit a755ec72f6440c7ab0851b052efd0ed4f72990c6
Showing with 34 additions and 3 deletions.
  1. +18 −1 src/bap_cmdline_terms.ml
  2. +15 −1 src/bap_main.ml
  3. +1 −1 testsuite
@@ -13,9 +13,26 @@ end
let enum_processors (module T : With_factory) =
T.Factory.list () |> List.map ~f:(fun x -> x,x)
let check file =
let open FileUtil in
let exists =
"file not found", Exists in
let is_file =
"must be a regular file",
Is_file in
let not_empty =
"file is empty",
Size_not_null in
let test_fail (_,t) = not (FileUtil.test t file) in
match List.find ~f:test_fail [exists; is_file; not_empty] with
| None -> `Ok file
| Some (m,_) -> `Error (sprintf "%s: %s" file m)
let suitable_file = check, Format.pp_print_string
let filename : string Term.t =
let doc = "Input filename." in
Arg.(required & pos 0 (some non_dir_file) None &
Arg.(required & pos 0 (some suitable_file) None &
info [] ~doc ~docv:"FILE")
let logdir : string option Term.t =
@@ -340,6 +340,20 @@ let load_recipe () =
| _,`Ok (Some r) -> eval_recipe r
| _ -> Sys.argv
let nice_pp_error fmt er =
let module R = Info.Internal_repr in
let rec pp_sexp fmt = function
| Sexp.Atom x -> Format.fprintf fmt "%s\n" x
| Sexp.List xs -> List.iter ~f:(pp_sexp fmt) xs in
let rec pp fmt r =
let open R in
match r with
| With_backtrace (r, backtrace) ->
Format.fprintf fmt "%a\n" pp r;
Format.fprintf fmt "Backtrace:\n%s" @@ String.strip backtrace
| String s -> Format.fprintf fmt "%s" s
| r -> pp_sexp fmt (R.sexp_of_t r) in
Format.fprintf fmt "%a" pp (R.of_info (Error.to_info er))
let () =
let () =
@@ -360,7 +374,7 @@ let () =
| Bap_plugin_loader.Plugin_not_found name ->
error "Can't find a plugin bundle `%s'" name
| Failed_to_create_project err ->
error "Failed to create a project: %a" Error.pp err
error "Failed to create a project: %a" nice_pp_error err
| Project.Pass.Failed (Project.Pass.Unsat_dep (p,n)) ->
error "Dependency `%s' of pass `%s' is not loaded"
n (Project.Pass.name p)

0 comments on commit a755ec7

Please sign in to comment.