Skip to content

Commit 2f8616c

Browse files
past-dueFabrice Bellard
authored andcommitted
fixed buffer overflow in js_bigint_from_string()
Cherry-pick of bellard/quickjs@e1c18be Co-authored-by: Fabrice Bellard <fabrice@bellard.org>
1 parent 5fdc969 commit 2f8616c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

quickjs.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11699,6 +11699,7 @@ static JSBigInt *js_bigint_from_string(JSContext *ctx,
1169911699
const char *str, int radix)
1170011700
{
1170111701
const char *p = str;
11702+
size_t n_digits1;
1170211703
int is_neg, n_digits, n_limbs, len, log2_radix, n_bits, i;
1170311704
JSBigInt *r;
1170411705
js_limb_t v, c, h;
@@ -11710,10 +11711,16 @@ static JSBigInt *js_bigint_from_string(JSContext *ctx,
1171011711
}
1171111712
while (*p == '0')
1171211713
p++;
11713-
n_digits = strlen(p);
11714+
n_digits1 = strlen(p);
11715+
/* the real check for overflox is done js_bigint_new(). Here
11716+
we just avoid integer overflow */
11717+
if (n_digits1 > JS_BIGINT_MAX_SIZE * JS_LIMB_BITS) {
11718+
JS_ThrowRangeError(ctx, "BigInt is too large to allocate");
11719+
return NULL;
11720+
}
11721+
n_digits = n_digits1;
1171411722
log2_radix = 32 - clz32(radix - 1); /* ceil(log2(radix)) */
1171511723
/* compute the maximum number of limbs */
11716-
/* XXX: overflow */
1171711724
if (radix == 10) {
1171811725
n_bits = (n_digits * 27 + 7) / 8; /* >= ceil(n_digits * log2(10)) */
1171911726
} else {

0 commit comments

Comments
 (0)