From d6daad93a4b2cf2575c334d09fcf83edc557a53c Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 30 Dec 2025 09:37:12 +0100 Subject: [PATCH 1/3] Ceval.binop: add missing casts to `tyop` The arguments must always be converted to `tyop` before the comparison is done. Fixes: #565 --- cparser/Ceval.ml | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cparser/Ceval.ml b/cparser/Ceval.ml index 0800e25ba2..7fdc400632 100644 --- a/cparser/Ceval.ml +++ b/cparser/Ceval.ml @@ -122,9 +122,10 @@ let unop env op tyres ty v = in cast env ty res let comparison env direction ptraction tyop v1 v2 = - (* tyop = type at which the comparison is done *) + (* tyop = type at which the comparison is done. + v1, v2 have been converted to tyop already. *) let b = - match cast env tyop v1, cast env tyop v2 with + match v1, v2 with | I n1, I n2 -> if is_signed env tyop then direction (compare n1 n2) 0 @@ -140,29 +141,31 @@ let comparison env direction ptraction tyop v1 v2 = let binop env op tyop tyres ty1 v1 ty2 v2 = (* tyop = type at which the computation is done tyres = expected result type *) + let v1 = cast env tyop v1 + and v2 = cast env tyop v2 in let res = match op with | Oadd -> if is_arith_type env ty1 && is_arith_type env ty2 then begin - match cast env tyop v1, cast env tyop v2 with + match v1, v2 with | I n1, I n2 -> I (Int64.add n1 n2) | _, _ -> raise Notconst end else raise Notconst | Osub -> if is_arith_type env ty1 && is_arith_type env ty2 then begin - match cast env tyop v1, cast env tyop v2 with + match v1, v2 with | I n1, I n2 -> I (Int64.sub n1 n2) | _, _ -> raise Notconst end else raise Notconst | Omul -> - begin match cast env tyop v1, cast env tyop v2 with + begin match v1, v2 with | I n1, I n2 -> I (Int64.mul n1 n2) | _, _ -> raise Notconst end | Odiv -> - begin match cast env tyop v1, cast env tyop v2 with + begin match v1, v2 with | I n1, I n2 -> if n2 = 0L then raise Notconst else if is_signed env tyop then I (Int64.div n1 n2) From 1e449f2d42de1b58aa52ac65c2fda513ca6b2dc8 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 30 Dec 2025 09:39:36 +0100 Subject: [PATCH 2/3] Ceval.unop: wrong cast of result value Fixes: #565 --- cparser/Ceval.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cparser/Ceval.ml b/cparser/Ceval.ml index 7fdc400632..052fbdeba2 100644 --- a/cparser/Ceval.ml +++ b/cparser/Ceval.ml @@ -119,7 +119,7 @@ let unop env op tyres ty v = | Olognot, _, _ -> if boolean_value v then I 0L else I 1L | Onot, _, I n -> I (Int64.lognot n) | _ -> raise Notconst - in cast env ty res + in cast env tyres res let comparison env direction ptraction tyop v1 v2 = (* tyop = type at which the comparison is done. From 23b1f34de2cf74eed70a3191cfe471fd1e6e5acd Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 30 Dec 2025 10:32:58 +0100 Subject: [PATCH 3/3] Update small test suite --- test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test b/test index ddb2ea33a7..081175df37 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit ddb2ea33a776bb576edb79a4476b06280087fb84 +Subproject commit 081175df374260304a5cd78dd3dfaded93dceb41