From bfca80bb8b6b13ddbfab29c151c5783aa1b94a28 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Thu, 15 Dec 2022 23:22:15 +0100 Subject: [PATCH] Avoid crashes that can occur `when` using when with a statement that may crash if the `when` condition doesn't hold For instance ``` node main () returns (o: int); var i: int; let i = 0; o = (1 / i) when false; tel ``` used to crash with for instance: ``` Floating point exception 136 ``` now returns `0` but in fact this value wouldn't be used in theory as the `when` condition doesn't hold. --- src/ast_to_c.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ast_to_c.ml b/src/ast_to_c.ml index 52b439c..ff4e5f7 100644 --- a/src/ast_to_c.ml +++ b/src/ast_to_c.ml @@ -99,8 +99,8 @@ let pp_expression node_name = match expression with | EWhen (_, e1, e2) -> begin - (* as don't use a variable assigned when the condition holds, can define it even if the condition doesn't hold *) - Format.fprintf fmt "%a" + Format.fprintf fmt "%a ? %a : 0" + pp_expression_aux e2 pp_expression_aux e1 end | EReset (_, e1, e2) ->