Skip to content

Fix Number.prototype.toFixed if argument is outside int32 range #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,8 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);

/* 1. */
int32_t frac_digits = ecma_number_to_int32 (arg_num);

/* 2. */
if (frac_digits < 0 || frac_digits > 20)
if (arg_num <= -1 || arg_num >= 21)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
}
Expand Down Expand Up @@ -251,6 +248,9 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
int32_t num_digits = 0;
int32_t exponent = 1;

/* 1. */
int32_t frac_digits = ecma_number_to_int32 (arg_num);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number_to_int32 returns 0 for -0.9? I don't know, just asking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. I'll add a test case for this.

/* Get the parameters of the number if non-zero. */
if (!ecma_number_is_zero (this_num))
{
Expand Down
16 changes: 16 additions & 0 deletions tests/jerry/number_prototype_to_fixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,26 @@ assert((0.0).toFixed(1) === "0.0");
assert((-0.0).toFixed(0) === "-0");
assert((-0.0).toFixed(1) === "-0.0");
assert((123456789012345678901.0).toFixed(20) === "123456789012345680000.00000000000000000000");
assert((123.56).toFixed(NaN) === "124");
assert((123.56).toFixed(-0.9) === "124");

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

try {
assert(obj.toFixed(Infinity));
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}

try {
assert(obj.toFixed(-Infinity));
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}

try {
assert(obj.toFixed(-1));
assert(false);
Expand Down