Skip to content

Commit 0822299

Browse files
authored
Fix parseFloat for non-infinity values (#3769)
The parseFloat method performed the "Infinity" string check incorrectly. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent e196062 commit 0822299

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -934,19 +934,18 @@ ecma_number_parse_float (const lit_utf8_byte_t *string_buff, /**< routine's firs
934934
}
935935
}
936936

937+
/* Check if string is equal to "Infinity". */
937938
const lit_utf8_byte_t *infinity_str_p = lit_get_magic_string_utf8 (LIT_MAGIC_STRING_INFINITY_UL);
938-
lit_utf8_byte_t *infinity_str_curr_p = (lit_utf8_byte_t *) infinity_str_p;
939-
lit_utf8_byte_t *infinity_str_end_p = infinity_str_curr_p + sizeof (*infinity_str_p);
939+
const lit_utf8_size_t infinity_length = lit_get_magic_string_size (LIT_MAGIC_STRING_INFINITY_UL);
940940

941-
/* Check if string is equal to "Infinity". */
942-
while (str_curr_p < str_end_p
943-
&& *str_curr_p++ == *infinity_str_curr_p++)
941+
/* The input string should be at least the length of "Infinity" to be correctly processed as
942+
* the infinity value.
943+
*/
944+
if (string_buff_size >= infinity_length
945+
&& memcmp (infinity_str_p, str_curr_p, infinity_length) == 0)
944946
{
945-
if (infinity_str_curr_p == infinity_str_end_p)
946-
{
947-
/* String matched Infinity. */
948-
return ecma_make_number_value (ecma_number_make_infinity (sign));
949-
}
947+
/* String matched Infinity. */
948+
return ecma_make_number_value (ecma_number_make_infinity (sign));
950949
}
951950

952951
/* Reset to starting position. */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
var result = parseFloat ("IE");
16+
assert (isNaN (result));

0 commit comments

Comments
 (0)