Skip to content

Commit

Permalink
[fix] handle more exceptions when parsing conf
Browse files Browse the repository at this point in the history
it is currently crashing the server when their is an error in the conf
files
  • Loading branch information
gautierhattenberger committed Jul 31, 2020
1 parent 2ae834f commit ce71d15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
25 changes: 14 additions & 11 deletions sw/ground_segment/tmtc/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,24 @@ let send_aircrafts_msg = fun _asker _values ->

let expand_aicraft x =
let ac_name = ExtXml.attrib x "name" in
let handle_error_message = fun error_type msg ->
prerr_endline ("A failure occurred while processing aircraft '"^ac_name^"'");
prerr_endline (" - "^error_type^" : "^msg);
prerr_endline (" - '"^ac_name^"' will be ignored by the server");
prerr_endline " - Please remove it from 'conf.xml' or fix its parameter(s)";
flush stderr;
Xml.Element ("ignoring_aircraft",["name", ac_name],[])
in
try
let ac = Aircraft.parse_aircraft ~parse_all:true "" x in
if List.length ac.Aircraft.xml > 0 then Xml.Element (Xml.tag x, Xml.attribs x, ac.Aircraft.xml)
else failwith "Nothing to parse"
with Failure msg ->
begin
prerr_endline ("A failure occurred while processing aircraft '"^ac_name^"'");
prerr_endline (" - Fail with : "^msg);
prerr_endline (" - '"^ac_name^"' will be ignored by the server");
prerr_endline " - Please remove it from 'conf.xml' or fix its parameter(s)";
flush stderr;
(*failwith msg*)
Xml.Element ("ignoring_aircraft",["name", ac_name],[])
end
with
| Failure msg -> handle_error_message "Fail with" msg
| Xml.File_not_found file -> handle_error_message "File not found" file
| Module.Module_not_found m -> handle_error_message "Module not found" m
| Dtd.Prove_error err -> handle_error_message "Dtd error" (Dtd.prove_error err)
| Not_found -> handle_error_message "Not found" "sorry, something went wrong somewhere"

let make_element = fun t a c -> Xml.Element (t,a,c)

Expand Down Expand Up @@ -901,7 +905,6 @@ let () =
"Usage: ";

Srtm.add_path srtm_path;

Ivy.init "Paparazzi server" "READY" (fun _ _ -> ());
Ivy.start !ivy_bus;

Expand Down
24 changes: 17 additions & 7 deletions sw/lib/ocaml/aircraft.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let rec target_conf_add_module = fun conf target firmware name mtype load_type -
(* sort element of an airframe type by target *)
let sort_airframe_by_target = fun config_by_target airframe ->
match airframe with
| None -> ()
| None -> []
| Some a ->
(* build a list of pairs (target, firmware) *)
let l = List.fold_left (fun lf f ->
Expand Down Expand Up @@ -150,7 +150,9 @@ let sort_airframe_by_target = fun config_by_target airframe ->
) c m.Airframe.OldModules.modules
) conf a.Airframe.modules in
Hashtbl.add config_by_target name conf
) l
) l;
(* return list of targets *)
fst (List.split l)

(** Extract a configuration element from aircraft config,
* returns a tuple with absolute file path and element object
Expand Down Expand Up @@ -201,7 +203,7 @@ let get_all_modules = fun config_by_target ->

let parse_aircraft = fun ?(parse_af=false) ?(parse_ap=false) ?(parse_fp=false) ?(parse_rc=false) ?(parse_tl=false) ?(parse_set=false) ?(parse_all=false) ?(verbose=false) target aircraft_xml ->

let name = Xml.attrib aircraft_xml "name" in
let name = ExtXml.attrib aircraft_xml "name" in
let conf_aircraft = [] in (* accumulate aircraft XML config *)
let config_by_target = Hashtbl.create 5 in

Expand All @@ -214,7 +216,7 @@ let parse_aircraft = fun ?(parse_af=false) ?(parse_ap=false) ?(parse_fp=false) ?
let conf_aircraft = conf_aircraft @ (match airframe with None -> [] | Some x -> [x.Airframe.xml]) in
if verbose then
Printf.printf ", sorting by target%!";
sort_airframe_by_target config_by_target airframe;
let target_list = sort_airframe_by_target config_by_target airframe in
if verbose then
Printf.printf ", extracting and parsing autopilot...%!";
let autopilots = if parse_ap || parse_all then
Expand Down Expand Up @@ -253,9 +255,17 @@ let parse_aircraft = fun ?(parse_af=false) ?(parse_ap=false) ?(parse_fp=false) ?
) config_by_target;
(af_ap.Airframe.Autopilot.freq, ap)
) autopilots in
let c = Hashtbl.find config_by_target target in
Hashtbl.replace config_by_target target { c with autopilot = true };
Some autopilots
try
let c = Hashtbl.find config_by_target target in
Hashtbl.replace config_by_target target { c with autopilot = true };
Some autopilots
with Not_found ->
(* target not found or not defined, add in all targets instead *)
List.iter (fun t ->
let c = Hashtbl.find config_by_target target in
Hashtbl.replace config_by_target target { c with autopilot = true }
) target_list;
Some autopilots
end
else None in
let conf_aircraft = conf_aircraft @ (match autopilots with None -> [] | Some lx -> List.map (fun (_, x) -> x.Autopilot.xml) lx) in
Expand Down

0 comments on commit ce71d15

Please sign in to comment.