Skip to content

Commit 9582c1c

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 [email protected]
1 parent 801e8e3 commit 9582c1c

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,11 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
11171117
{
11181118
ecma_length_t string_len = ecma_string_get_length (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_len, lit_utf8_byte_t);
11211121

11221122
size_t string_buf_size = (size_t) (string_len) * sizeof (lit_utf8_byte_t);
11231123
ssize_t bytes_copied = ecma_string_to_utf8_string (space_str_p,
1124-
zt_string_buff,
1124+
string_buff,
11251125
(ssize_t) string_buf_size);
11261126
JERRY_ASSERT (bytes_copied > 0);
11271127

@@ -1130,13 +1130,13 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
11301130

11311131
for (uint32_t i = 0; i < 10; i++)
11321132
{
1133-
space_buff[i] = zt_string_buff[i];
1133+
space_buff[i] = string_buff[i];
11341134
}
11351135

11361136
context_p.gap_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) space_buff, 10);
11371137

11381138
MEM_FINALIZE_LOCAL_ARRAY (space_buff);
1139-
MEM_FINALIZE_LOCAL_ARRAY (zt_string_buff);
1139+
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
11401140
}
11411141
}
11421142
/* 8. */
@@ -1198,23 +1198,24 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
11981198
ecma_string_t *product_str_p = ecma_copy_or_ref_ecma_string (quote_str_p);
11991199
ecma_string_t *tmp_str_p;
12001200

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

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

1205-
size_t string_buf_size = (size_t) (string_len) * sizeof (lit_utf8_byte_t);
12061205
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);
1206+
string_buff,
1207+
(ssize_t) string_size);
12101208

1211-
/* 2. */
1212-
for (ecma_length_t i = 0; i < string_len; i++)
1209+
JERRY_ASSERT (bytes_copied > 0 || !string_size);
1210+
1211+
lit_utf8_iterator_t iter = lit_utf8_iterator_create (string_buff, string_size);
1212+
1213+
while (!lit_utf8_iterator_is_eos (&iter))
12131214
{
1214-
lit_utf8_byte_t c = zt_string_buff[i];
1215+
ecma_char_t current_char = lit_utf8_iterator_read_next (&iter);
12151216

12161217
/* 2.a */
1217-
if (c == LIT_CHAR_BACKSLASH || c == LIT_CHAR_DOUBLE_QUOTE)
1218+
if (current_char == LIT_CHAR_BACKSLASH || current_char == LIT_CHAR_DOUBLE_QUOTE)
12181219
{
12191220
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12201221

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

12271228
/* 2.a.ii */
1228-
ecma_string_t *c_str_p = ecma_new_ecma_string_from_utf8 (&c, 1);
1229+
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
12291230

1230-
tmp_str_p = ecma_concat_ecma_strings (product_str_p, c_str_p);
1231+
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
12311232
ecma_deref_ecma_string (product_str_p);
1232-
ecma_deref_ecma_string (c_str_p);
1233+
ecma_deref_ecma_string (current_char_str_p);
12331234
product_str_p = tmp_str_p;
12341235
}
12351236
/* 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)
1237+
else if (current_char == LIT_CHAR_BS
1238+
|| current_char == LIT_CHAR_FF
1239+
|| current_char == LIT_CHAR_LF
1240+
|| current_char == LIT_CHAR_CR
1241+
|| current_char == LIT_CHAR_TAB)
12381242
{
12391243
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12401244

@@ -1247,7 +1251,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12471251
/* 2.b.ii */
12481252
lit_utf8_byte_t abbrev = LIT_CHAR_SP;
12491253

1250-
switch (c)
1254+
switch (current_char)
12511255
{
12521256
case LIT_CHAR_BS:
12531257
{
@@ -1285,7 +1289,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12851289
product_str_p = tmp_str_p;
12861290
}
12871291
/* 2.c */
1288-
else if (c < LIT_CHAR_SP)
1292+
else if (current_char < LIT_CHAR_SP)
12891293
{
12901294
ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12911295

@@ -1305,7 +1309,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
13051309
product_str_p = tmp_str_p;
13061310

13071311
/* 2.c.iii */
1308-
ecma_string_t *hex_str_p = ecma_builtin_helper_json_create_hex_digit_ecma_string (c);
1312+
ecma_string_t *hex_str_p = ecma_builtin_helper_json_create_hex_digit_ecma_string ( (uint8_t) current_char);
13091313

13101314
/* 2.c.iv */
13111315
tmp_str_p = ecma_concat_ecma_strings (product_str_p, hex_str_p);
@@ -1316,16 +1320,16 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
13161320
/* 2.d */
13171321
else
13181322
{
1319-
ecma_string_t *c_str_p = ecma_new_ecma_string_from_utf8 (&c, 1);
1323+
ecma_string_t *current_char_str_p = ecma_new_ecma_string_from_code_unit (current_char);
13201324

1321-
tmp_str_p = ecma_concat_ecma_strings (product_str_p, c_str_p);
1325+
tmp_str_p = ecma_concat_ecma_strings (product_str_p, current_char_str_p);
13221326
ecma_deref_ecma_string (product_str_p);
1323-
ecma_deref_ecma_string (c_str_p);
1327+
ecma_deref_ecma_string (current_char_str_p);
13241328
product_str_p = tmp_str_p;
13251329
}
13261330
}
13271331

1328-
MEM_FINALIZE_LOCAL_ARRAY (zt_string_buff);
1332+
MEM_FINALIZE_LOCAL_ARRAY (string_buff);
13291333

13301334
/* 3. */
13311335
tmp_str_p = ecma_concat_ecma_strings (product_str_p, quote_str_p);

tests/jerry/json-stringify.js

Lines changed: 3 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');

0 commit comments

Comments
 (0)