Skip to content

Commit 8aeb2a0

Browse files
Roland Takacsegavrin
authored andcommitted
Fix underflow in JSON.stringify()
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs [email protected]
1 parent f625473 commit 8aeb2a0

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-json.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,8 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
10021002
ret_value);
10031003

10041004
/* 6.a */
1005-
uint32_t num_of_spaces = ecma_number_to_uint32 (array_length_num);
1006-
uint32_t space = (num_of_spaces > 10) ? 10 : num_of_spaces;
1005+
int32_t num_of_spaces = ecma_number_to_int32 (array_length_num);
1006+
int32_t space = (num_of_spaces > 10) ? 10 : num_of_spaces;
10071007

10081008
/* 6.b */
10091009
if (space < 1)
@@ -1014,12 +1014,12 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
10141014
{
10151015
MEM_DEFINE_LOCAL_ARRAY (space_buff, space, char);
10161016

1017-
for (uint32_t i = 0; i < space; i++)
1017+
for (int32_t i = 0; i < space; i++)
10181018
{
10191019
space_buff[i] = ' ';
10201020
}
10211021

1022-
context_p.gap_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) space_buff, space);
1022+
context_p.gap_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) space_buff, (lit_utf8_size_t) space);
10231023

10241024
MEM_FINALIZE_LOCAL_ARRAY (space_buff);
10251025
}

tests/jerry/json-stringify.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,14 @@ assert (JSON.stringify (object, null, " ") == '{\n "a": 2\n}');
163163
assert (JSON.stringify (object, null, "asd") == '{\nasd"a": 2\n}');
164164
assert (JSON.stringify (object, null, "asd0123456789") == '{\nasd0123456"a": 2\n}');
165165
assert (JSON.stringify (object, null, 100) == '{\n "a": 2\n}');
166+
assert (JSON.stringify (object, null, -5) == '{"a":2}');
166167

167168
array = [2];
168169
assert (JSON.stringify (array, null, " ") == '[\n 2\n]');
169170
assert (JSON.stringify (array, null, "asd") == '[\nasd2\n]');
170171
assert (JSON.stringify (array, null, "asd0123456789") == '[\nasd01234562\n]');
171172
assert (JSON.stringify (array, null, 100) == '[\n 2\n]');
173+
assert (JSON.stringify (array, null, -5) == '[2]');
172174

173175
nested_object = {"a": 2, "b": {"c": 1, "d": true}};
174176
assert (JSON.stringify (nested_object, null, 2) == '{\n "b": {\n "d": true,\n "c": 1\n },\n "a": 2\n}');

0 commit comments

Comments
 (0)