Skip to content

Commit 55acdf2

Browse files
author
Robert Fancsik
authored
Fix buffer overflow in string radix conversion (#4850)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 18dd9aa commit 55acdf2

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

jerry-core/ecma/base/ecma-helpers-conversion.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,6 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */
368368

369369
bool sign = false;
370370

371-
if (*str_p == LIT_CHAR_PLUS)
372-
{
373-
str_p++;
374-
}
375-
else if (*str_p == LIT_CHAR_MINUS)
376-
{
377-
sign = true;
378-
str_p++;
379-
}
380-
381371
if (str_p + 2 < end_p && str_p[0] == LIT_CHAR_0)
382372
{
383373
uint8_t radix = lit_char_to_radix (str_p[1]);
@@ -388,6 +378,16 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */
388378
}
389379
}
390380

381+
if (*str_p == LIT_CHAR_PLUS)
382+
{
383+
str_p++;
384+
}
385+
else if (*str_p == LIT_CHAR_MINUS)
386+
{
387+
sign = true;
388+
str_p++;
389+
}
390+
391391
/* Check if string is equal to "Infinity". */
392392
const lit_utf8_byte_t *infinity_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
393393
const lit_utf8_size_t infinity_length = lit_get_magic_string_size (LIT_MAGIC_STRING_INFINITY_UL);

jerry-core/ecma/base/ecma-helpers-number.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ ecma_number_parse_float (const lit_utf8_byte_t *str_p, /**< routine's first argu
659659
}
660660

661661
/* 5. */
662-
ecma_number_t ret_num = ecma_utf8_string_to_number (num_start_p, (lit_utf8_size_t) (num_end_p - num_start_p), 0);
662+
ecma_number_t ret_num = ecma_utf8_string_to_number (num_start_p, num_size, 0);
663663

664664
if (sign)
665665
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
assert(Number('0x10') === 16);
16+
assert(isNaN(Number('+0x10')));
17+
assert(isNaN(Number('-0x10')));
18+
assert(parseFloat('0x10') === 0);
19+
assert(parseFloat('+0x10') === 0);
20+
assert(parseFloat('-0x10') === 0);
21+
assert(0x10 === 16);
22+
assert(+0x10 === 16);
23+
assert(-0x10 === -16);

0 commit comments

Comments
 (0)