summaryrefslogtreecommitdiff
path: root/byterun/ints.c
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@inria.fr>2009-07-15 14:50:31 +0000
committerXavier Leroy <xavier.leroy@inria.fr>2009-07-15 14:50:31 +0000
commita5aa0b7e3772645aa586b2b4db0eb9cc7f3e4e32 (patch)
tree76581d7ab0b1e37872b11280ef21816db97aaf47 /byterun/ints.c
parent11217e8f704644e11a22c944fbf9dcee0e767547 (diff)
downloadocaml-a5aa0b7e3772645aa586b2b4db0eb9cc7f3e4e32.tar.gz
PR#4210, PR#4245: tightened bound checking in string->integer conversion functions, without changing what the lexer accepts
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@9317 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'byterun/ints.c')
-rw-r--r--byterun/ints.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/byterun/ints.c b/byterun/ints.c
index 5fc15c6264..7b8a136751 100644
--- a/byterun/ints.c
+++ b/byterun/ints.c
@@ -83,9 +83,12 @@ static intnat parse_intnat(value s, int nbits)
caml_failwith("int_of_string");
}
if (base == 10) {
- /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits - 1) */
- if (res > (uintnat)1 << (nbits - 1))
- caml_failwith("int_of_string");
+ /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
+ if (sign >= 0) {
+ if (res >= (uintnat)1 << (nbits - 1)) caml_failwith("int_of_string");
+ } else {
+ if (res > (uintnat)1 << (nbits - 1)) caml_failwith("int_of_string");
+ }
} else {
/* Unsigned representation expected, allow 0 to 2^nbits - 1
and tolerate -(2^nbits - 1) to 0 */
@@ -540,7 +543,8 @@ CAMLprim value caml_int64_of_string(value s)
{
char * p;
uint64 max_uint64 = I64_literal(0xFFFFFFFF, 0xFFFFFFFF);
- uint64 max_int64 = I64_literal(0x80000000, 0x00000000);
+ uint64 max_int64_pos = I64_literal(0x7FFFFFFF, 0xFFFFFFFF);
+ uint64 max_int64_neg = I64_literal(0x80000000, 0x00000000);
uint64 res, threshold;
int sign, base, d;
@@ -563,7 +567,10 @@ CAMLprim value caml_int64_of_string(value s)
if (p != String_val(s) + caml_string_length(s)){
caml_failwith("int_of_string");
}
- if (base == 10 && I64_ult(max_int64, res)) caml_failwith("int_of_string");
+ if (base == 10) {
+ if (I64_ult((sign >= 0 ? max_int64_pos : max_int64_neg), res))
+ caml_failwith("int_of_string");
+ }
if (sign < 0) res = I64_neg(res);
return caml_copy_int64(res);
}