Skip to content
Snippets Groups Projects
Commit 0e2c64ef authored by Virgile Prevosto's avatar Virgile Prevosto Committed by Andre Maroneze
Browse files

[crowbar] less UB in generated expressions

parent 7ba8259e
No related branches found
No related tags found
No related merge requests found
...@@ -19,9 +19,6 @@ let gen_type = ...@@ -19,9 +19,6 @@ let gen_type =
let mk_exp expr_node = { expr_loc = loc; expr_node } 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 let needs_int_unary = function
| NOT | BNOT -> true | NOT | BNOT -> true
| _ -> false | _ -> false
...@@ -75,22 +72,26 @@ let gen_constant = ...@@ -75,22 +72,26 @@ let gen_constant =
mk_exp (CONSTANT (CONST_FLOAT (string_of_float f)))) 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 = let protected_cast t e =
match t with let max = mk_exp (CONSTANT(CONST_INT("255"))) in
| Tunsigned -> let min =
mk_exp ( match t with
QUESTION ( | Tunsigned -> mk_exp(CONSTANT(CONST_INT("0")))
mk_exp(BINARY(GE,e,mk_exp (CONSTANT(CONST_INT("0"))))), | _ -> mk_exp (UNARY(MINUS,max))
e, in
mk_exp(CONSTANT(CONST_INT("0"))))) let maxr = mk_cast t max in
| _ -> let minr = mk_cast t min in
let max = mk_exp (CONSTANT(CONST_INT("255"))) in mk_exp(
let min = mk_exp (UNARY(MINUS,max)) in QUESTION(
mk_exp( mk_exp(BINARY(GE,e,min)),
QUESTION( mk_exp(QUESTION(mk_exp(BINARY(LE,e,max)),e,maxr)),
mk_exp(BINARY(GE,e,min)), minr))
mk_exp(QUESTION(mk_exp(BINARY(LE,e,max)),e,max)),
min)) let force_int typ e =
let e = protected_cast typ e in
mk_exp (CAST (([SpecType typ],JUSTBASE), SINGLE_INIT e))
let gen_expr = let gen_expr =
fix fix
...@@ -109,10 +110,8 @@ let gen_expr = ...@@ -109,10 +110,8 @@ let gen_expr =
else e1,e2 else e1,e2
in in
mk_exp (BINARY (b,e1,e2))); mk_exp (BINARY (b,e1,e2)));
map [ gen_int_type; gen_expr; gen_expr; gen_expr ] map [ gen_expr; gen_expr; gen_expr ]
(fun t c et ef -> (fun c et ef -> mk_exp (QUESTION (c,et,ef)));
let c = force_int t c in
mk_exp (QUESTION (c,et,ef)));
map [ gen_type; gen_expr ] map [ gen_type; gen_expr ]
(fun t e -> (fun t e ->
let e = protected_cast t e in let e = protected_cast t e in
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment