From 0e2c64ef4f9230507d59c9854bf4f7da69b7462b Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Tue, 3 Mar 2020 19:27:30 +0100
Subject: [PATCH] [crowbar] less UB in generated expressions

---
 tests/crowbar/constfold.ml | 43 +++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/tests/crowbar/constfold.ml b/tests/crowbar/constfold.ml
index 6adcf9fd6b3..b98c69153c1 100644
--- a/tests/crowbar/constfold.ml
+++ b/tests/crowbar/constfold.ml
@@ -19,9 +19,6 @@ let gen_type =
 
 let mk_exp expr_node = { expr_loc = loc; expr_node }
 
-let force_int typ e =
-  mk_exp (CAST (([SpecType typ],JUSTBASE), SINGLE_INIT e))
-
 let needs_int_unary = function
   | NOT | BNOT -> true
   | _ -> false
@@ -75,22 +72,26 @@ let gen_constant =
          mk_exp (CONSTANT (CONST_FLOAT (string_of_float f))))
   ]
 
+let mk_cast t e = mk_exp (CAST (([SpecType t],JUSTBASE), SINGLE_INIT e))
+
 let protected_cast t e =
-  match t with
-  | Tunsigned ->
-    mk_exp (
-      QUESTION (
-        mk_exp(BINARY(GE,e,mk_exp (CONSTANT(CONST_INT("0"))))),
-        e,
-        mk_exp(CONSTANT(CONST_INT("0")))))
-  | _ ->
-    let max = mk_exp (CONSTANT(CONST_INT("255"))) in
-    let min = mk_exp (UNARY(MINUS,max)) in
-    mk_exp(
-      QUESTION(
-        mk_exp(BINARY(GE,e,min)),
-        mk_exp(QUESTION(mk_exp(BINARY(LE,e,max)),e,max)),
-        min))
+  let max = mk_exp (CONSTANT(CONST_INT("255"))) in
+  let min =
+    match t with
+    | Tunsigned -> mk_exp(CONSTANT(CONST_INT("0")))
+    | _ ->  mk_exp (UNARY(MINUS,max))
+  in
+  let maxr = mk_cast t max in
+  let minr = mk_cast t min in
+  mk_exp(
+    QUESTION(
+      mk_exp(BINARY(GE,e,min)),
+      mk_exp(QUESTION(mk_exp(BINARY(LE,e,max)),e,maxr)),
+      minr))
+
+let force_int typ e =
+  let e = protected_cast typ e in
+  mk_exp (CAST (([SpecType typ],JUSTBASE), SINGLE_INIT e))
 
 let gen_expr =
   fix
@@ -109,10 +110,8 @@ let gen_expr =
                 else e1,e2
               in
               mk_exp (BINARY (b,e1,e2)));
-         map [ gen_int_type; gen_expr; gen_expr; gen_expr ]
-           (fun t c et ef ->
-              let c = force_int t c in
-              mk_exp (QUESTION (c,et,ef)));
+         map [ gen_expr; gen_expr; gen_expr ]
+           (fun c et ef -> mk_exp (QUESTION (c,et,ef)));
          map [ gen_type; gen_expr ]
            (fun t e ->
               let e = protected_cast t e in
-- 
GitLab