Skip to content

Commit 1db5c15

Browse files
committed
Fix for issue #1993
This patch fixes this bug which caused corrupted stack by preventing unnecessary double to ascii conversion even if the convertible number of digits is higher than allowed. In addition, improved ecma_double_to_binary_floating_point function by removing a needless buffer. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 8ae6592 commit 1db5c15

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,10 @@ ecma_number_of_digits (double val) /**< ecma number */
981981
inline static void __attr_always_inline___
982982
ecma_double_to_ascii (double val, /**< ecma number */
983983
lit_utf8_byte_t *buffer_p, /**< buffer to generate digits into */
984+
int32_t num_of_digits, /**< number of digits */
984985
int32_t *exp_p) /**< [out] exponent */
985986
{
986987
int32_t char_cnt = 0;
987-
int32_t num_of_digits = ecma_number_of_digits (val);
988988

989989
double divider = 10.0;
990990
double prev_residual;
@@ -1017,35 +1017,38 @@ ecma_double_to_binary_floating_point (double val, /**< ecma number */
10171017
lit_utf8_byte_t *buffer_p, /**< buffer to generate digits into */
10181018
int32_t *exp_p) /**< [out] exponent */
10191019
{
1020-
int32_t i, char_cnt = 0;
1020+
int32_t char_cnt = 0;
10211021
double integer_part, fraction_part;
10221022

10231023
fraction_part = fmod (val, 1.0);
10241024
integer_part = floor (val);
1025-
1026-
lit_utf8_byte_t integer_part_buffer[ecma_number_of_digits (integer_part) + 1];
1025+
int32_t num_of_digits = ecma_number_of_digits (integer_part);
10271026

10281027
if (fabs (integer_part) < EPSILON)
10291028
{
10301029
buffer_p[0] = '0';
10311030
char_cnt++;
10321031
}
1033-
else if (integer_part < 10e16) /* Ensure that integer_part is not rounded */
1032+
else if (num_of_digits <= 16) /* Ensure that integer_part is not rounded */
10341033
{
10351034
while (integer_part > 0.0)
10361035
{
1037-
integer_part_buffer[char_cnt++] = (lit_utf8_byte_t) ((int) fmod (integer_part, 10.0) + '0');
1036+
buffer_p[num_of_digits - 1 - char_cnt++] = (lit_utf8_byte_t) ((int) fmod (integer_part, 10.0) + '0');
10381037
integer_part = floor (integer_part / 10.0);
10391038
}
1040-
1041-
for (i = 0; i < char_cnt; i++)
1042-
{
1043-
buffer_p[i] = integer_part_buffer[char_cnt - i - 1];
1044-
}
1039+
}
1040+
else if (num_of_digits <= 21)
1041+
{
1042+
ecma_double_to_ascii (integer_part, buffer_p, num_of_digits, &char_cnt);
10451043
}
10461044
else
10471045
{
1048-
ecma_double_to_ascii (val, buffer_p, &char_cnt);
1046+
/* According to ECMA-262 v5, 15.7.4.5 7th step: if x >= 10^21, then execution will continue by
1047+
* ToString(x) so in this case no further conversations are required. Number 21 in the else if condition
1048+
* above must be kept in sync with the number 21 in ecma_builtin_number_prototype_object_to_fixed
1049+
* method 7th step. */
1050+
*exp_p = num_of_digits;
1051+
return 0;
10491052
}
10501053

10511054
*exp_p = char_cnt;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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(parseFloat(Number.MAX_VALUE).toFixed(5) == parseFloat(Number.MAX_VALUE).toString());

0 commit comments

Comments
 (0)