aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Schmidt <github@mschmidt.me>2019-03-20 20:16:21 +0100
committerXavier Leroy <xavierleroy@users.noreply.github.com>2019-03-20 20:16:21 +0100
commitc85e957812d4581f17a534f3754c555a6a2a2243 (patch)
treeb665a02980af7e2fb64d8d8bfd9ca482d653ee2b
parent72ba1c282e2a8bfd0e826352a251fa71bfb71e05 (diff)
downloadcompcert-c85e957812d4581f17a534f3754c555a6a2a2243.tar.gz
compcert-c85e957812d4581f17a534f3754c555a6a2a2243.zip
Improve overflow check for integer literals (#157)
The previous check was incomplete for integer literals in base 10. Bug 26119
-rw-r--r--cparser/Elab.ml6
1 files changed, 4 insertions, 2 deletions
diff --git a/cparser/Elab.ml b/cparser/Elab.ml
index 7a0b05de..a3915dc4 100644
--- a/cparser/Elab.ml
+++ b/cparser/Elab.ml
@@ -295,14 +295,16 @@ let parse_int base s =
| _ -> assert false in
let v = ref 0L in
for i = 0 to String.length s - 1 do
- if !v < 0L || !v > max_val then raise Overflow;
- v := Int64.mul !v (Int64.of_int base);
let c = s.[i] in
let digit =
if c >= '0' && c <= '9' then Char.code c - 48
else if c >= 'A' && c <= 'F' then Char.code c - 55
else raise Bad_digit in
if digit >= base then raise Bad_digit;
+ if !v < 0L || !v > max_val then raise Overflow;
+ (* because (2^64 - 1) % 10 = 5, not 9 *)
+ if base = 10 && !v = max_val && digit > 5 then raise Overflow;
+ v := Int64.mul !v (Int64.of_int base);
v := Int64.add !v (Int64.of_int digit)
done;
!v