From c85e957812d4581f17a534f3754c555a6a2a2243 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 20 Mar 2019 20:16:21 +0100 Subject: Improve overflow check for integer literals (#157) The previous check was incomplete for integer literals in base 10. Bug 26119 --- cparser/Elab.ml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'cparser/Elab.ml') 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 -- cgit