Skip to content

Commit 47cd42e

Browse files
committed
Fix Number.prototype.toFixed if argument is outside int32 range
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 4e5e7bb commit 47cd42e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,8 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
200200
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
201201
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
202202

203-
/* 1. */
204-
int32_t frac_digits = ecma_number_to_int32 (arg_num);
205-
206203
/* 2. */
207-
if (frac_digits < 0 || frac_digits > 20)
204+
if (arg_num <= -1 || arg_num >= 21)
208205
{
209206
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
210207
}
@@ -251,6 +248,9 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
251248
int32_t num_digits = 0;
252249
int32_t exponent = 1;
253250

251+
/* 1. */
252+
int32_t frac_digits = ecma_number_to_int32 (arg_num);
253+
254254
/* Get the parameters of the number if non-zero. */
255255
if (!ecma_number_is_zero (this_num))
256256
{

tests/jerry/number_prototype_to_fixed.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,26 @@ assert((0.0).toFixed(1) === "0.0");
3535
assert((-0.0).toFixed(0) === "-0");
3636
assert((-0.0).toFixed(1) === "-0.0");
3737
assert((123456789012345678901.0).toFixed(20) === "123456789012345680000.00000000000000000000");
38+
assert((123.56).toFixed(NaN) === "124");
39+
assert((123.56).toFixed(-0.9) === "124");
3840

3941
var obj = { toFixed : Number.prototype.toFixed };
4042
assert(obj.toFixed(0) === "NaN");
4143

44+
try {
45+
assert(obj.toFixed(Infinity));
46+
assert(false);
47+
} catch (e) {
48+
assert(e instanceof RangeError);
49+
}
50+
51+
try {
52+
assert(obj.toFixed(-Infinity));
53+
assert(false);
54+
} catch (e) {
55+
assert(e instanceof RangeError);
56+
}
57+
4258
try {
4359
assert(obj.toFixed(-1));
4460
assert(false);

0 commit comments

Comments
 (0)