diff --git a/CodeHawk/CHC/cchanalyze/cCHExpTranslator.ml b/CodeHawk/CHC/cchanalyze/cCHExpTranslator.ml index 3a834754..4f1adc54 100644 --- a/CodeHawk/CHC/cchanalyze/cCHExpTranslator.ml +++ b/CodeHawk/CHC/cchanalyze/cCHExpTranslator.ml @@ -1179,7 +1179,20 @@ object (self) ~ok:(fun ftype -> match x with | Const c -> self#translate_const_expr c - | CastE (_, xx) -> self#translate_expr xx + | CastE (_, xx) -> + (* An integer conversion that may change the value (C11 + 6.3.1.3p2-3; issue 21) is applied to a constant operand and + makes any other operand unknown. Previously every cast was + the identity: (uint8_t)x > 100 with x == 300 folded to TRUE + and made the else branch unreachable (issue 25). *) + let inner = self#translate_expr xx in + (match CCHIntegerSemantics.get_correction env context x with + | None -> inner + | Some c -> + match inner with + | XConst (IntConst n) -> + num_constant_expr (CCHIntegerSemantics.fold_constant c n) + | _ -> random_constant_expr) | Lval lval -> self#translate_variable_expr lval ftype | AddrOf lval | StartOf lval -> self#translate_address lval ftype diff --git a/CodeHawk/CHC/cchanalyze/cCHPOChecker.ml b/CodeHawk/CHC/cchanalyze/cCHPOChecker.ml index 950dfd5a..1edd2a54 100644 --- a/CodeHawk/CHC/cchanalyze/cCHPOChecker.ml +++ b/CodeHawk/CHC/cchanalyze/cCHPOChecker.ml @@ -5,7 +5,7 @@ The MIT License (MIT) Copyright (c) 2005-2019 Kestrel Technology LLC - Copyright (c) 2020-2023 Henny B. Sipma + Copyright (c) 2020-2025 Henny B. Sipma Copyright (c) 2026 Aarno Labs LLC Permission is hereby granted, free of charge, to any person obtaining a copy @@ -202,10 +202,10 @@ object end -let is_unreachable facts = - List.fold_left (fun acc f -> - match acc with Some _ -> acc | _ -> f#is_unreachable) - None facts +(* domains that report the location unreachable *) +let unreachable_domains facts = + List.sort_uniq Stdlib.compare + (List.filter_map (fun f -> f#is_unreachable) facts) let _check_file_assumptions _fenv _file_assumptions _p = false @@ -261,22 +261,42 @@ let check_proof_obligations | CCHFailure s -> po_check_failure (msg s) | Failure s -> po_check_failure (msg (STR s)) in - match is_unreachable + match unreachable_domains (invIO#get_location_invariant p#get_context#project_on_cfg)#get_invariants with - | Some domain -> + | (domain :: _) as domains -> if system_settings#use_unreachability then ignore ((make_unreachable p domain)) else + (* Unreachability is rare in real code and usually + points to an error or omission in the analysis; it must not + silently discharge the proof obligation. The other domains' + facts at this location are not used either: they are vacuous + if the location is unreachable (a violation would be a false + alarm, an api delegation a spurious precondition) and + suspect otherwise. Only the statement-local check, which does + not use invariants, is performed; if the proof obligation + remains open it is marked with an explicit unreachable + diagnostic that is reported separately. *) begin chlog#add "unreachable" (LBLOCK [ location_to_pretty p#get_location; - STR " (domain: "; - STR domain; + STR " (domains: "; + STR (String.concat ", " domains); STR ")"]); - default () + (try + check_ppo_validity env#get_functionname env#get_fdecls p + with + | CCHFailure s -> po_check_failure (msg s) + | Failure s -> po_check_failure (msg (STR s))); + if p#is_closed then + () + else + List.iter (fun d -> + p#add_diagnostic_key_msg + "unreachable" ("proven by domain " ^ d)) domains end - | _ -> + | [] -> default ()) proofObligations diff --git a/CodeHawk/CHC/cchpre/cCHProofObligation.ml b/CodeHawk/CHC/cchpre/cCHProofObligation.ml index 46e46268..3e3f4c31 100644 --- a/CodeHawk/CHC/cchpre/cCHProofObligation.ml +++ b/CodeHawk/CHC/cchpre/cCHProofObligation.ml @@ -6,7 +6,7 @@ Copyright (c) 2005-2019 Kestrel Technology LLC Copyright (c) 2020 Henny B. Sipma - Copyright (c) 2021-2025 Aarno Labs LLC + Copyright (c) 2021-2026 Aarno Labs LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -208,6 +208,8 @@ object (self) method clear = begin H.clear invarianttable; + H.clear argmessages; + H.clear keymessages; messages#removeList messages#toList end @@ -267,6 +269,7 @@ object (self) method is_empty = (H.length invarianttable) = 0 && (H.length argmessages = 0) + && (H.length keymessages = 0) && messages#isEmpty method private write_xml_situated_msg