Skip to content

Commit fe67b75

Browse files
author
Roland Takacs
committed
Fix assertion 'bytes_copied > 0 || !string_len' in JSON.stringify()
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
1 parent 55ffb0c commit fe67b75

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

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

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,28 +1115,21 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
11151115
}
11161116
else
11171117
{
1118-
ecma_length_t string_len = ecma_string_get_length (space_str_p);
1118+
ecma_length_t string_size = ecma_string_get_size (space_str_p);
11191119

1120-
MEM_DEFINE_LOCAL_ARRAY (zt_string_buff, string_len, lit_utf8_byte_t);
1120+
MEM_DEFINE_LOCAL_ARRAY (string_buff, string_size, lit_utf8_byte_t);
11211121

1122-
size_t string_buf_size = (size_t) (string_len) * sizeof (lit_utf8_byte_t);
11231122
ssize_t bytes_copied = ecma_string_to_utf8_string (space_str_p,
1124-
zt_string_buff,
1125-
(ssize_t) string_buf_size);
1123+
string_buff,
1124+
(ssize_t) string_size);
11261125
JERRY_ASSERT (bytes_copied > 0);
11271126

1128-
/* Buffer for the first 10 characters. */
1129-
MEM_DEFINE_LOCAL_ARRAY (space_buff, 10, lit_utf8_byte_t);
1127+
lit_utf8_iterator_t iter = lit_utf8_iterator_create (string_buff, string_size);
1128+
lit_utf8_iterator_advance (&iter, 10);
11301129

1131-
for (uint32_t i = 0; i < 10; i++)
1132-
{
1133-
space_buff[i] = zt_string_buff[i];
1134-
}
1130+
context_p.gap_str_p = ecma_new_ecma_string_from_utf8 (string_buff, iter.buf_pos.offset);
11351131

1136-
context_p.gap_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) space_buff, 10);
1137-
1138-
MEM_FINALIZE_LOCAL_ARRAY (space_buff);
1139-
MEM_FINALIZE_LOCAL_ARRAY (zt_string_buff);
1132+
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
11401133
}
11411134
}
11421135
/* 8. */
@@ -1198,23 +1191,24 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
11981191
ecma_string_t *product_str_p = ecma_copy_or_ref_ecma_string (quote_str_p);
11991192
ecma_string_t *tmp_str_p;
12001193

1201-
ecma_length_t string_len = ecma_string_get_length (string_p);
1194+
ecma_length_t string_size = ecma_string_get_size (string_p);
12021195

1203-
MEM_DEFINE_LOCAL_ARRAY (zt_string_buff, string_len, lit_utf8_byte_t);
1196+
MEM_DEFINE_LOCAL_ARRAY (string_buff, string_size, lit_utf8_byte_t);
12041197

1205-
size_t string_buf_size = (size_t) (string_len) * sizeof (lit_utf8_byte_t);
12061198
ssize_t bytes_copied = ecma_string_to_utf8_string (string_p,
1207-
zt_string_buff,
1208-
(ssize_t) string_buf_size);
1209-
JERRY_ASSERT (bytes_copied > 0 || !string_len);
1199+
string_buff,
1200+
(ssize_t) string_size);
12101201

1211-
/* 2. */
1212-
for (ecma_length_t i = 0; i < string_len; i++)
1202+
JERRY_ASSERT (bytes_copied > 0 || !string_size);
1203+
1204+
lit_utf8_iterator_t iter = lit_utf8_iterator_create (string_buff, string_size);
1205+
1206+
while (!lit_utf8_iterator_is_eos (&iter))
12131207
{
1214-
lit_utf8_byte_t c = zt_string_buff[i];
1208+
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
12151209

12161210
/* 2.a */
1217-
if (c == LIT_CHAR_BACKSLASH || c == LIT_CHAR_DOUBLE_QUOTE)
1211+
if (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE)
12181212
{
12191213
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12201214

@@ -1225,16 +1219,19 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12251219
product_str_p = tmp_str_p;
12261220

12271221
/* 2.a.ii */
1228-
ecma_string_t *c_str_p = ecma_new_ecma_string_from_utf8 (&c, 1);
1222+
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
12291223

1230-
tmp_str_p = ecma_concat_ecma_strings (product_str_p, c_str_p);
1224+
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
12311225
ecma_deref_ecma_string (product_str_p);
1232-
ecma_deref_ecma_string (c_str_p);
1226+
ecma_deref_ecma_string (current_char_str_p);
12331227
product_str_p = tmp_str_p;
12341228
}
12351229
/* 2.b */
1236-
else if (c == LIT_CHAR_BS || c == LIT_CHAR_FF || c == LIT_CHAR_LF
1237-
|| c == LIT_CHAR_CR || c == LIT_CHAR_TAB)
1230+
else if (current_char == LIT_CHAR_BS
1231+
|| current_char == LIT_CHAR_FF
1232+
|| current_char == LIT_CHAR_LF
1233+
|| current_char == LIT_CHAR_CR
1234+
|| current_char == LIT_CHAR_TAB)
12381235
{
12391236
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12401237

@@ -1247,7 +1244,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12471244
/* 2.b.ii */
12481245
lit_utf8_byte_t abbrev = LIT_CHAR_SP;
12491246

1250-
switch (c)
1247+
switch (current_char)
12511248
{
12521249
case LIT_CHAR_BS:
12531250
{
@@ -1285,7 +1282,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12851282
product_str_p = tmp_str_p;
12861283
}
12871284
/* 2.c */
1288-
else if (c < LIT_CHAR_SP)
1285+
else if (current_char < LIT_CHAR_SP)
12891286
{
12901287
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12911288

@@ -1305,7 +1302,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
13051302
product_str_p = tmp_str_p;
13061303

13071304
/* 2.c.iii */
1308-
ecma_string_t *hex_str_p = ecma_builtin_helper_json_create_hex_digit_ecma_string (c);
1305+
ecma_string_t *hex_str_p = ecma_builtin_helper_json_create_hex_digit_ecma_string ((uint8_t) current_char);
13091306

13101307
/* 2.c.iv */
13111308
tmp_str_p = ecma_concat_ecma_strings (product_str_p, hex_str_p);
@@ -1316,16 +1313,16 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
13161313
/* 2.d */
13171314
else
13181315
{
1319-
ecma_string_t *c_str_p = ecma_new_ecma_string_from_utf8 (&c, 1);
1316+
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
13201317

1321-
tmp_str_p = ecma_concat_ecma_strings (product_str_p, c_str_p);
1318+
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
13221319
ecma_deref_ecma_string (product_str_p);
1323-
ecma_deref_ecma_string (c_str_p);
1320+
ecma_deref_ecma_string (current_char_str_p);
13241321
product_str_p = tmp_str_p;
13251322
}
13261323
}
13271324

1328-
MEM_FINALIZE_LOCAL_ARRAY (zt_string_buff);
1325+
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
13291326

13301327
/* 3. */
13311328
tmp_str_p = ecma_concat_ecma_strings (product_str_p, quote_str_p);

tests/jerry/json-stringify.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ assert (JSON.stringify (ctl_string) == '"asd\\u001fasd"');
2828
escpad_string = "\"asda\sd";
2929
assert (JSON.stringify (escpad_string) == '"\\"asdasd"');
3030

31+
assert (JSON.stringify('\u2040') == '"⁀"');
32+
assert (JSON.stringify('abc\u2040\u2030cba') == '"abc⁀‰cba"');
33+
3134
// Checking primitive types
3235
assert (JSON.stringify (1) === '1');
3336
assert (JSON.stringify (true) === 'true');
@@ -165,13 +168,15 @@ object = {"a": 2};
165168
assert (JSON.stringify (object, null, " ") == '{\n "a": 2\n}');
166169
assert (JSON.stringify (object, null, "asd") == '{\nasd"a": 2\n}');
167170
assert (JSON.stringify (object, null, "asd0123456789") == '{\nasd0123456"a": 2\n}');
171+
assert (JSON.stringify (object, null, "asd\u20400123456789") == '{\nasd⁀012345"a": 2\n}');
168172
assert (JSON.stringify (object, null, 100) == '{\n "a": 2\n}');
169173
assert (JSON.stringify (object, null, -5) == '{"a":2}');
170174

171175
array = [2];
172176
assert (JSON.stringify (array, null, " ") == '[\n 2\n]');
173177
assert (JSON.stringify (array, null, "asd") == '[\nasd2\n]');
174178
assert (JSON.stringify (array, null, "asd0123456789") == '[\nasd01234562\n]');
179+
assert (JSON.stringify (array, null, "asd\u20400123456789") == '[\nasd⁀0123452\n]');
175180
assert (JSON.stringify (array, null, 100) == '[\n 2\n]');
176181
assert (JSON.stringify (array, null, -5) == '[2]');
177182

0 commit comments

Comments
 (0)