diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 2015-07-24 09:36:03 +0000 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 2015-07-24 09:36:03 +0000 |
commit | a1bafbbb6723ae937e2553af43df5a04e281c8d8 (patch) | |
tree | 33beacc493031c45516694e706df01dda730d738 /byterun/ints.c | |
parent | 844b1af40cd8f8b5db6f8c02d9441ba6f2c44552 (diff) | |
download | ocaml-a1bafbbb6723ae937e2553af43df5a04e281c8d8.tar.gz |
PR#6316: Scanf.scanf failure on %u formats when reading big integers.
The approach implemented is the second one suggested by BenoƮt Vaugon in the PR:
- The int_of_string functions accept a "0u" prefix meaning "decimal unsigned".
- The '%u' format of the scanf functions adds this "0u" prefix before conversion.
This is consistent with the current handling of unsigned hexa, octal, and binary numbers.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16241 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'byterun/ints.c')
-rw-r--r-- | byterun/ints.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/byterun/ints.c b/byterun/ints.c index ee1f05fa45..fa7aaa3271 100644 --- a/byterun/ints.c +++ b/byterun/ints.c @@ -23,6 +23,7 @@ static char * parse_sign_and_base(char * p, /*out*/ int * base, + /*out*/ int * signedness, /*out*/ int * sign) { *sign = 1; @@ -30,15 +31,17 @@ static char * parse_sign_and_base(char * p, *sign = -1; p++; } - *base = 10; + *base = 10; *signedness = 1; if (*p == '0') { switch (p[1]) { case 'x': case 'X': - *base = 16; p += 2; break; + *base = 16; *signedness = 0; p += 2; break; case 'o': case 'O': - *base = 8; p += 2; break; + *base = 8; *signedness = 0; p += 2; break; case 'b': case 'B': - *base = 2; p += 2; break; + *base = 2; *signedness = 0; p += 2; break; + case 'u': case 'U': + *signedness = 0; p += 2; break; } } return p; @@ -65,9 +68,9 @@ static intnat parse_intnat(value s, int nbits, const char *errmsg) { char * p; uintnat res, threshold; - int sign, base, d; + int sign, base, signedness, d; - p = parse_sign_and_base(String_val(s), &base, &sign); + p = parse_sign_and_base(String_val(s), &base, &signedness, &sign); threshold = ((uintnat) -1) / base; d = parse_digit(*p); if (d < 0 || d >= base) caml_failwith(errmsg); @@ -85,7 +88,7 @@ static intnat parse_intnat(value s, int nbits, const char *errmsg) if (p != String_val(s) + caml_string_length(s)){ caml_failwith(errmsg); } - if (base == 10) { + if (signedness) { /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */ if (sign >= 0) { if (res >= (uintnat)1 << (nbits - 1)) caml_failwith(errmsg); @@ -530,9 +533,9 @@ CAMLprim value caml_int64_of_string(value s) { char * p; uint64_t res, threshold; - int sign, base, d; + int sign, base, signedness, d; - p = parse_sign_and_base(String_val(s), &base, &sign); + p = parse_sign_and_base(String_val(s), &base, &signedness, &sign); threshold = ((uint64_t) -1) / base; d = parse_digit(*p); if (d < 0 || d >= base) caml_failwith(INT64_ERRMSG); @@ -551,7 +554,7 @@ CAMLprim value caml_int64_of_string(value s) if (p != String_val(s) + caml_string_length(s)){ caml_failwith(INT64_ERRMSG); } - if (base == 10) { + if (signedness) { /* Signed representation expected, allow -2^63 to 2^63 - 1 only */ if (sign >= 0) { if (res >= (uint64_t)1 << 63) caml_failwith(INT64_ERRMSG); |