summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@inria.fr>2003-06-23 11:27:05 +0000
committerXavier Leroy <xavier.leroy@inria.fr>2003-06-23 11:27:05 +0000
commitaf6f4447aeb7fc6af0fc8a59f7dfade2d8adf359 (patch)
tree2418952c7b9ebeb03fccd0e81654245cc3a57f82
parentd60708263ea903fa8728ba352bd245c8a94670af (diff)
int_of_string: proteger contre les caracteres nuls
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@5611 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r--byterun/ints.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/byterun/ints.c b/byterun/ints.c
index fe397c0ee..adaa983bb 100644
--- a/byterun/ints.c
+++ b/byterun/ints.c
@@ -58,12 +58,13 @@ static int parse_digit(char c)
return -1;
}
-static long parse_long(char * p)
+static long parse_long(value s)
{
+ char * p;
unsigned long res;
int sign, base, d;
- p = parse_sign_and_base(p, &base, &sign);
+ p = parse_sign_and_base(String_val(s), &base, &sign);
d = parse_digit(*p);
if (d < 0 || d >= base) failwith("int_of_string");
for (p++, res = d; /*nothing*/; p++) {
@@ -73,7 +74,7 @@ static long parse_long(char * p)
if (d < 0 || d >= base) break;
res = base * res + d;
}
- if (*p != 0) failwith("int_of_string");
+ if (p != String_val(s) + string_length(s)) failwith("int_of_string");
return sign < 0 ? -((long) res) : (long) res;
}
@@ -105,7 +106,7 @@ CAMLprim value int_compare(value v1, value v2)
CAMLprim value int_of_string(value s)
{
- return Val_long(parse_long(String_val(s)));
+ return Val_long(parse_long(s));
}
#define FORMAT_BUFFER_SIZE 32
@@ -304,7 +305,7 @@ CAMLprim value int32_format(value fmt, value arg)
CAMLprim value int32_of_string(value s)
{
- return copy_int32(parse_long(String_val(s)));
+ return copy_int32(parse_long(s));
}
/* 64-bit integers */
@@ -500,7 +501,7 @@ CAMLprim value int64_of_string(value s)
if (d < 0 || d >= base) break;
res = I64_add(I64_mul(I64_of_int32(base), res), I64_of_int32(d));
}
- if (*p != 0) failwith("int_of_string");
+ if (p != String_val(s) + string_length(s)) failwith("int_of_string");
if (sign < 0) res = I64_neg(res);
return copy_int64(res);
}
@@ -683,7 +684,7 @@ CAMLprim value nativeint_format(value fmt, value arg)
CAMLprim value nativeint_of_string(value s)
{
- return copy_nativeint(parse_long(String_val(s)));
+ return copy_nativeint(parse_long(s));
}