added missing bignum error tests (github issue #159)

This commit is contained in:
Fabrice Bellard 2023-12-05 17:25:03 +01:00
parent f87cab0fc6
commit 321dbfa770

View file

@ -10596,6 +10596,10 @@ static __maybe_unused JSValue JS_ToIntegerFree(JSContext *ctx, JSValue val)
BOOL is_nan; BOOL is_nan;
a = JS_ToBigFloat(ctx, &a_s, val); a = JS_ToBigFloat(ctx, &a_s, val);
if (!a) {
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
if (!bf_is_finite(a)) { if (!bf_is_finite(a)) {
is_nan = bf_is_nan(a); is_nan = bf_is_nan(a);
if (is_nan) if (is_nan)
@ -11185,6 +11189,10 @@ static JSValue js_ftoa(JSContext *ctx, JSValueConst val1, int radix,
if (JS_IsException(val)) if (JS_IsException(val))
return val; return val;
a = JS_ToBigFloat(ctx, &a_s, val); a = JS_ToBigFloat(ctx, &a_s, val);
if (!a) {
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
saved_sign = a->sign; saved_sign = a->sign;
if (a->expn == BF_EXP_ZERO) if (a->expn == BF_EXP_ZERO)
a->sign = 0; a->sign = 0;
@ -11241,6 +11249,8 @@ static JSValue js_bigdecimal_to_string1(JSContext *ctx, JSValueConst val,
int saved_sign; int saved_sign;
a = JS_ToBigDecimal(ctx, val); a = JS_ToBigDecimal(ctx, val);
if (!a)
return JS_EXCEPTION;
saved_sign = a->sign; saved_sign = a->sign;
if (a->expn == BF_EXP_ZERO) if (a->expn == BF_EXP_ZERO)
a->sign = 0; a->sign = 0;
@ -12698,6 +12708,11 @@ static int js_unary_arith_bigfloat(JSContext *ctx,
} }
r = JS_GetBigFloat(res); r = JS_GetBigFloat(res);
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1);
if (!a) {
JS_FreeValue(ctx, res);
JS_FreeValue(ctx, op1);
return -1;
}
ret = 0; ret = 0;
switch(op) { switch(op) {
case OP_inc: case OP_inc:
@ -12747,6 +12762,11 @@ static int js_unary_arith_bigdecimal(JSContext *ctx,
} }
r = JS_GetBigDecimal(res); r = JS_GetBigDecimal(res);
a = JS_ToBigDecimal(ctx, op1); a = JS_ToBigDecimal(ctx, op1);
if (!a) {
JS_FreeValue(ctx, res);
JS_FreeValue(ctx, op1);
return -1;
}
ret = 0; ret = 0;
switch(op) { switch(op) {
case OP_inc: case OP_inc:
@ -12795,6 +12815,11 @@ static int js_unary_arith_bigint(JSContext *ctx,
} }
r = JS_GetBigInt(res); r = JS_GetBigInt(res);
a = JS_ToBigInt(ctx, &a_s, op1); a = JS_ToBigInt(ctx, &a_s, op1);
if (!a) {
JS_FreeValue(ctx, res);
JS_FreeValue(ctx, op1);
return -1;
}
ret = 0; ret = 0;
switch(op) { switch(op) {
case OP_inc: case OP_inc:
@ -13153,14 +13178,21 @@ static int js_binary_arith_bigfloat(JSContext *ctx, OPCodeEnum op,
JSValue res; JSValue res;
res = JS_NewBigFloat(ctx); res = JS_NewBigFloat(ctx);
if (JS_IsException(res)) { if (JS_IsException(res))
JS_FreeValue(ctx, op1); goto fail;
JS_FreeValue(ctx, op2);
return -1;
}
r = JS_GetBigFloat(res); r = JS_GetBigFloat(res);
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1);
if (!a) {
JS_FreeValue(ctx, res);
goto fail;
}
b = JS_ToBigFloat(ctx, &b_s, op2); b = JS_ToBigFloat(ctx, &b_s, op2);
if (!b) {
if (a == &a_s)
bf_delete(a);
JS_FreeValue(ctx, res);
goto fail;
}
bf_init(ctx->bf_ctx, r); bf_init(ctx->bf_ctx, r);
switch(op) { switch(op) {
case OP_add: case OP_add:
@ -13204,6 +13236,10 @@ static int js_binary_arith_bigfloat(JSContext *ctx, OPCodeEnum op,
} }
*pres = res; *pres = res;
return 0; return 0;
fail:
JS_FreeValue(ctx, op1);
JS_FreeValue(ctx, op2);
return -1;
} }
/* b must be a positive integer */ /* b must be a positive integer */
@ -13750,8 +13786,8 @@ static int js_compare_bigdecimal(JSContext *ctx, OPCodeEnum op,
JS_FreeValue(ctx, op1); JS_FreeValue(ctx, op1);
return -1; return -1;
} }
a = JS_ToBigDecimal(ctx, op1); a = JS_ToBigDecimal(ctx, op1); /* cannot fail */
b = JS_ToBigDecimal(ctx, op2); b = JS_ToBigDecimal(ctx, op2); /* cannot fail */
switch(op) { switch(op) {
case OP_lt: case OP_lt:
@ -14189,8 +14225,10 @@ static no_inline int js_mul_pow10(JSContext *ctx, JSValue *sp)
op1 = sp[-2]; op1 = sp[-2];
op2 = sp[-1]; op2 = sp[-1];
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1);
if (!a) if (!a) {
JS_FreeValue(ctx, res);
return -1; return -1;
}
if (JS_IsBigInt(ctx, op2)) { if (JS_IsBigInt(ctx, op2)) {
ret = JS_ToBigInt64(ctx, &e, op2); ret = JS_ToBigInt64(ctx, &e, op2);
} else { } else {
@ -14312,8 +14350,8 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
res = FALSE; res = FALSE;
break; break;
} }
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1); /* cannot fail */
b = JS_ToBigFloat(ctx, &b_s, op2); b = JS_ToBigFloat(ctx, &b_s, op2); /* cannot fail */
res = bf_cmp_eq(a, b); res = bf_cmp_eq(a, b);
if (a == &a_s) if (a == &a_s)
bf_delete(a); bf_delete(a);
@ -48838,6 +48876,10 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
bf_t *a, a_s; bf_t *a, a_s;
a = JS_ToBigFloat(ctx, &a_s, val); a = JS_ToBigFloat(ctx, &a_s, val);
if (!a) {
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
if (!bf_is_finite(a)) { if (!bf_is_finite(a)) {
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to bigint"); val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to bigint");
@ -49639,6 +49681,10 @@ static JSValue js_bigfloat_fop(JSContext *ctx, JSValueConst this_val,
if (JS_IsException(op1)) if (JS_IsException(op1))
return op1; return op1;
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1);
if (!a) {
JS_FreeValue(ctx, op1);
return JS_EXCEPTION;
}
fe = &ctx->fp_env; fe = &ctx->fp_env;
if (argc > 1) { if (argc > 1) {
fe = JS_GetOpaque2(ctx, argv[1], JS_CLASS_FLOAT_ENV); fe = JS_GetOpaque2(ctx, argv[1], JS_CLASS_FLOAT_ENV);
@ -49737,7 +49783,11 @@ static JSValue js_bigfloat_fop2(JSContext *ctx, JSValueConst this_val,
return op2; return op2;
} }
a = JS_ToBigFloat(ctx, &a_s, op1); a = JS_ToBigFloat(ctx, &a_s, op1);
if (!a)
goto fail1;
b = JS_ToBigFloat(ctx, &b_s, op2); b = JS_ToBigFloat(ctx, &b_s, op2);
if (!b)
goto fail2;
fe = &ctx->fp_env; fe = &ctx->fp_env;
if (argc > 2) { if (argc > 2) {
fe = JS_GetOpaque2(ctx, argv[2], JS_CLASS_FLOAT_ENV); fe = JS_GetOpaque2(ctx, argv[2], JS_CLASS_FLOAT_ENV);
@ -49747,10 +49797,12 @@ static JSValue js_bigfloat_fop2(JSContext *ctx, JSValueConst this_val,
res = JS_NewBigFloat(ctx); res = JS_NewBigFloat(ctx);
if (JS_IsException(res)) { if (JS_IsException(res)) {
fail: fail:
if (a == &a_s)
bf_delete(a);
if (b == &b_s) if (b == &b_s)
bf_delete(b); bf_delete(b);
fail2:
if (a == &a_s)
bf_delete(a);
fail1:
JS_FreeValue(ctx, op1); JS_FreeValue(ctx, op1);
JS_FreeValue(ctx, op2); JS_FreeValue(ctx, op2);
return JS_EXCEPTION; return JS_EXCEPTION;