Fix argument boundary check order during number format conversion (#4031)

Also added some tests related to the issues caused by the wrong order

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam 2020-07-24 15:55:12 +02:00 committed by GitHub
parent d39a076b2e
commit da5b058dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 7 deletions

View File

@ -493,17 +493,12 @@ ecma_builtin_number_prototype_object_to_number_convert (ecma_number_t this_num,
return to_integer;
}
/* Argument boundary checks */
if (mode != NUMBER_ROUTINE_TO_PRECISION
/* Argument boundary check for toFixed method */
if (mode == NUMBER_ROUTINE_TO_FIXED
&& (arg_num <= -1 || arg_num >= 101))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Fraction digits must be between 0 and 100."));
}
else if (mode == NUMBER_ROUTINE_TO_PRECISION
&& (arg_num < 1 || arg_num > 100))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Precision digits must be between 1 and 100."));
}
/* Handle NaN separately */
if (ecma_number_is_nan (this_num))
@ -566,6 +561,20 @@ ecma_builtin_number_prototype_object_to_number_convert (ecma_number_t this_num,
return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
}
/* Argument boundary check for toExponential and toPrecision methods */
if (mode == NUMBER_ROUTINE_TO_EXPONENTIAL
&& (arg_num <= -1 || arg_num >= 101))
{
ecma_stringbuilder_destroy (&builder);
return ecma_raise_range_error (ECMA_ERR_MSG ("Fraction digits must be between 0 and 100."));
}
else if (mode == NUMBER_ROUTINE_TO_PRECISION
&& (arg_num < 1 || arg_num > 100))
{
ecma_stringbuilder_destroy (&builder);
return ecma_raise_range_error (ECMA_ERR_MSG ("Precision digits must be between 1 and 100."));
}
num_of_digits = ecma_number_to_decimal (this_num, digits, &exponent);
/* Handle undefined argument */

View File

@ -59,3 +59,31 @@ try {
} catch (e) {
assert(e instanceof TypeError)
}
assert((+Infinity).toPrecision(1000) === "Infinity");
var n = new Number(+Infinity);
assert(n.toPrecision(1000) === "Infinity");
assert((-Infinity).toPrecision(1000) === "-Infinity");
var n = new Number(-Infinity);
assert(n.toPrecision(1000) === "-Infinity");
assert(NaN.toPrecision(undefined) === "NaN");
var calls = 0;
var p = {
valueOf: function() {
calls++;
return Infinity;
}
};
assert(NaN.toPrecision(p) === "NaN");
assert(calls === 1);
var n = new Number(NaN);
calls = 0;
assert(n.toPrecision(p) === "NaN");
assert(calls === 1);