Skip to content

Fix assertion fails in parseInt() #527

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
243 changes: 130 additions & 113 deletions jerry-core/ecma/builtin-objects/ecma-builtin-global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,160 +236,177 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
}
}

/* 3. */
int sign = 1;

/* 4. */
ecma_char_t current = lit_utf8_iterator_read_next (&iter);
if (current == LIT_CHAR_MINUS)
if (!lit_utf8_iterator_is_eos (&iter))
{
sign = -1;
}
/* 3. */
int sign = 1;

/* 5. */
if (current == LIT_CHAR_MINUS || current == LIT_CHAR_PLUS)
{
start = lit_utf8_iterator_get_pos (&iter);
if (!lit_utf8_iterator_is_eos (&iter))
/* 4. */
ecma_char_t current = lit_utf8_iterator_read_next (&iter);
if (current == LIT_CHAR_MINUS)
{
current = lit_utf8_iterator_read_next (&iter);
sign = -1;
}

/* 5. */
if (current == LIT_CHAR_MINUS || current == LIT_CHAR_PLUS)
{
start = lit_utf8_iterator_get_pos (&iter);
if (!lit_utf8_iterator_is_eos (&iter))
{
current = lit_utf8_iterator_read_next (&iter);
}
}
}

/* 6. */
ECMA_OP_TO_NUMBER_TRY_CATCH (radix_num, radix, ret_value);
int32_t rad = ecma_number_to_int32 (radix_num);
/* 6. */
ECMA_OP_TO_NUMBER_TRY_CATCH (radix_num, radix, ret_value);
int32_t rad = ecma_number_to_int32 (radix_num);

/* 7.*/
bool strip_prefix = true;
/* 7.*/
bool strip_prefix = true;

/* 8. */
if (rad != 0)
{
/* 8.a */
if (rad < 2 || rad > 36)
/* 8. */
if (rad != 0)
{
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
/* 8.a */
if (rad < 2 || rad > 36)
{
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}
/* 8.b */
else if (rad != 16)
{
strip_prefix = false;
}
}
/* 8.b */
else if (rad != 16)
/* 9. */
else
{
strip_prefix = false;
rad = 10;
}
}
/* 9. */
else
{
rad = 10;
}

if (ecma_is_completion_value_empty (ret_value))
{
/* 10. */
if (strip_prefix)
if (ecma_is_completion_value_empty (ret_value))
{
if (end.offset - start.offset >= 2 && current == LIT_CHAR_0)
/* 10. */
if (strip_prefix)
{
ecma_char_t next = lit_utf8_iterator_peek_next (&iter);
if (next == LIT_CHAR_LOWERCASE_X || next == LIT_CHAR_UPPERCASE_X)
if (end.offset - start.offset >= 2 && current == LIT_CHAR_0)
{
/* Skip the 'x' or 'X' characters. */
lit_utf8_iterator_incr (&iter);
start = lit_utf8_iterator_get_pos (&iter);
ecma_char_t next = lit_utf8_iterator_peek_next (&iter);
if (next == LIT_CHAR_LOWERCASE_X || next == LIT_CHAR_UPPERCASE_X)
{
/* Skip the 'x' or 'X' characters. */
lit_utf8_iterator_incr (&iter);
start = lit_utf8_iterator_get_pos (&iter);

rad = 16;
rad = 16;
}
}
}
}

/* 11. Check if characters are in [0, Radix - 1]. We also convert them to number values in the process. */
lit_utf8_iterator_seek (&iter, start);
while (!lit_utf8_iterator_is_eos (&iter))
{
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
int32_t current_number;

if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
{
current_number = current_char - LIT_CHAR_LOWERCASE_A + 10;
}
else if ((current_char >= LIT_CHAR_UPPERCASE_A && current_char <= LIT_CHAR_UPPERCASE_Z))
{
current_number = current_char - LIT_CHAR_UPPERCASE_A + 10;
}
else if (lit_char_is_decimal_digit (current_char))
{
current_number = current_char - LIT_CHAR_0;
}
else
/* 11. Check if characters are in [0, Radix - 1]. We also convert them to number values in the process. */
lit_utf8_iterator_seek (&iter, start);
while (!lit_utf8_iterator_is_eos (&iter))
{
/* Not a valid number char, set value to radix so it fails to pass as a valid character. */
current_number = rad;
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
int32_t current_number;

if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
{
current_number = current_char - LIT_CHAR_LOWERCASE_A + 10;
}
else if ((current_char >= LIT_CHAR_UPPERCASE_A && current_char <= LIT_CHAR_UPPERCASE_Z))
{
current_number = current_char - LIT_CHAR_UPPERCASE_A + 10;
}
else if (lit_char_is_decimal_digit (current_char))
{
current_number = current_char - LIT_CHAR_0;
}
else
{
/* Not a valid number char, set value to radix so it fails to pass as a valid character. */
current_number = rad;
}

if (!(current_number < rad))
{
lit_utf8_iterator_decr (&iter);
end = lit_utf8_iterator_get_pos (&iter);
break;
}
}

if (!(current_number < rad))
/* 12. */
if (end.offset - start.offset == 0)
{
lit_utf8_iterator_decr (&iter);
end = lit_utf8_iterator_get_pos (&iter);
break;
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}
}

/* 12. */
if (end.offset - start.offset == 0)
{
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}
}

if (ecma_is_completion_value_empty (ret_value))
{
ecma_number_t *value_p = ecma_alloc_number ();
*value_p = 0;
ecma_number_t multiplier = 1.0f;

/* 13. and 14. */
lit_utf8_iterator_seek (&iter, end);
while (lit_utf8_iterator_get_pos (&iter).offset > start.offset)
if (ecma_is_completion_value_empty (ret_value))
{
ecma_char_t current_char = lit_utf8_iterator_read_prev (&iter);
ecma_number_t current_number;
ecma_number_t *value_p = ecma_alloc_number ();
*value_p = 0;
ecma_number_t multiplier = 1.0f;

if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
/* 13. and 14. */
if (end.offset < str_size)
{
current_number = (ecma_number_t) current_char - LIT_CHAR_LOWERCASE_A + 10;
lit_utf8_iterator_seek (&iter, end);
}
else if ((current_char >= LIT_CHAR_UPPERCASE_A && current_char <= LIT_CHAR_UPPERCASE_Z))
else
{
current_number = (ecma_number_t) current_char - LIT_CHAR_UPPERCASE_A + 10;
lit_utf8_iterator_seek_eos (&iter);
}
else if (lit_char_is_decimal_digit (current_char))
while (lit_utf8_iterator_get_pos (&iter).offset > start.offset)
{
current_number = (ecma_number_t) current_char - LIT_CHAR_0;
ecma_char_t current_char = lit_utf8_iterator_read_prev (&iter);
ecma_number_t current_number;

if ((current_char >= LIT_CHAR_LOWERCASE_A && current_char <= LIT_CHAR_LOWERCASE_Z))
{
current_number = (ecma_number_t) current_char - LIT_CHAR_LOWERCASE_A + 10;
}
else if ((current_char >= LIT_CHAR_UPPERCASE_A && current_char <= LIT_CHAR_UPPERCASE_Z))
{
current_number = (ecma_number_t) current_char - LIT_CHAR_UPPERCASE_A + 10;
}
else if (lit_char_is_decimal_digit (current_char))
{
current_number = (ecma_number_t) current_char - LIT_CHAR_0;
}
else
{
JERRY_UNREACHABLE ();
}

*value_p += current_number * multiplier;
multiplier *= (ecma_number_t) rad;
}
else

/* 15. */
if (sign < 0)
{
JERRY_UNREACHABLE ();
*value_p *= (ecma_number_t) sign;
}

*value_p += current_number * multiplier;
multiplier *= (ecma_number_t) rad;
}

/* 15. */
if (sign < 0)
{
*value_p *= (ecma_number_t) sign;
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (value_p));
}

ret_value = ecma_make_normal_completion_value (ecma_make_number_value (value_p));
ECMA_OP_TO_NUMBER_FINALIZE (radix_num);
}
else
{
ecma_number_t *ret_num_p = ecma_alloc_number ();
*ret_num_p = ecma_number_make_nan ();
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
}

ECMA_OP_TO_NUMBER_FINALIZE (radix_num);
MEM_FINALIZE_LOCAL_ARRAY (utf8_string_buff);
}
else
Expand Down
4 changes: 4 additions & 0 deletions tests/jerry/global-parseint.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ assert(parseInt("\u00a0123") === 123);
assert(parseInt("\u20291 123\u00D0") === 1);
assert(parseInt("\u00a0123", 13) === 198);
assert(parseInt("\u2029123 1\u00D0", 11) === 146);
assert(isNaN(parseInt("\u0009")));
assert(isNaN(parseInt("\u00A0")));
assert(parseInt("\u00A0\u00A0-1") === parseInt("-1"));
assert(parseInt("\u00A01") === parseInt("1"));

var bool = true;
var obj = new Object();
Expand Down