Skip to content

Commit d06c3a7

Browse files
authored
Fix error handling in SerializeJSONProperty (#3816)
Fixes #3813. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 3b4c259 commit d06c3a7

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,36 +1186,36 @@ ecma_builtin_json_serialize_property (ecma_json_stringify_context_t *context_p,
11861186
ecma_object_t *obj_p = ecma_get_object_from_value (value);
11871187
lit_magic_string_id_t class_name = ecma_object_get_class_name (obj_p);
11881188

1189-
ecma_value_t result = ECMA_VALUE_EMPTY;
1190-
11911189
/* 5.a */
11921190
if (class_name == LIT_MAGIC_STRING_NUMBER_UL)
11931191
{
1194-
result = ecma_op_to_number (value);
1192+
value = ecma_op_to_number (value);
1193+
ecma_deref_object (obj_p);
1194+
1195+
if (ECMA_IS_VALUE_ERROR (value))
1196+
{
1197+
return value;
1198+
}
11951199
}
11961200
/* 5.b */
11971201
else if (class_name == LIT_MAGIC_STRING_STRING_UL)
11981202
{
11991203
ecma_string_t *str_p = ecma_op_to_string (value);
1200-
result = ecma_make_string_value (str_p);
1204+
ecma_deref_object (obj_p);
1205+
1206+
if (JERRY_UNLIKELY (str_p == NULL))
1207+
{
1208+
return ECMA_VALUE_ERROR;
1209+
}
1210+
1211+
value = ecma_make_string_value (str_p);
12011212
}
12021213
/* 5.c */
12031214
else if (class_name == LIT_MAGIC_STRING_BOOLEAN_UL)
12041215
{
12051216
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
1206-
result = ext_object_p->u.class_prop.u.value;
1207-
}
1208-
1209-
if (!ecma_is_value_empty (result))
1210-
{
1217+
value = ext_object_p->u.class_prop.u.value;
12111218
ecma_deref_object (obj_p);
1212-
1213-
if (ECMA_IS_VALUE_ERROR (result))
1214-
{
1215-
return result;
1216-
}
1217-
1218-
value = result;
12191219
}
12201220
}
12211221

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 o = Object("str");
16+
o.toString = function() { throw "toString error" };
17+
18+
try {
19+
JSON.stringify(o);
20+
assert(false);
21+
} catch (e) {
22+
assert(e === "toString error");
23+
}

0 commit comments

Comments
 (0)