Skip to content

Commit ea88f42

Browse files
committed
Ceval.binop: take bit width of integer type into account for shifts
Fixes: #568
1 parent ad4cb44 commit ea88f42

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cparser/Ceval.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ let is_signed env ty =
9292
| TEnum(_, _) -> is_signed_ikind enum_ikind
9393
| _ -> false
9494

95+
let int_bit_size env ty =
96+
let byte_size =
97+
match unroll env ty with
98+
| TInt(ik, _) -> sizeof_ikind ik
99+
| TEnum(_, _) -> sizeof_ikind enum_ikind
100+
| _ -> 0 in
101+
Int64.of_int (byte_size * 8)
102+
95103
let cast env ty_to v =
96104
match unroll env ty_to, v with
97105
| TInt(IBool, _), _ ->
@@ -197,13 +205,13 @@ let binop env op tyop tyres ty1 v1 ty2 v2 =
197205
end
198206
| Oshl ->
199207
begin match v1, v2 with
200-
| I n1, I n2 when n2 >= 0L && n2 < 64L ->
208+
| I n1, I n2 when n2 >= 0L && n2 < int_bit_size env tyop ->
201209
I (Int64.shift_left n1 (Int64.to_int n2))
202210
| _, _ -> raise Notconst
203211
end
204212
| Oshr ->
205213
begin match v1, v2 with
206-
| I n1, I n2 when n2 >= 0L && n2 < 64L ->
214+
| I n1, I n2 when n2 >= 0L && n2 < int_bit_size env tyop ->
207215
if is_signed env tyop
208216
then I (Int64.shift_right n1 (Int64.to_int n2))
209217
else I (Int64.shift_right_logical n1 (Int64.to_int n2))

0 commit comments

Comments
 (0)