From bda2f9346bc021e812008b1ad4f97b7500d81d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Sat, 30 May 2020 12:21:00 +0200 Subject: [PATCH] Fix error handling in SerializeJSONProperty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3813. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- .../ecma/builtin-objects/ecma-builtin-json.c | 32 +++++++++---------- tests/jerry/regression-test-issue-3813.js | 23 +++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 tests/jerry/regression-test-issue-3813.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c index ea6c2f613c..15350baefc 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c @@ -1186,36 +1186,36 @@ ecma_builtin_json_serialize_property (ecma_json_stringify_context_t *context_p, ecma_object_t *obj_p = ecma_get_object_from_value (value); lit_magic_string_id_t class_name = ecma_object_get_class_name (obj_p); - ecma_value_t result = ECMA_VALUE_EMPTY; - /* 5.a */ if (class_name == LIT_MAGIC_STRING_NUMBER_UL) { - result = ecma_op_to_number (value); + value = ecma_op_to_number (value); + ecma_deref_object (obj_p); + + if (ECMA_IS_VALUE_ERROR (value)) + { + return value; + } } /* 5.b */ else if (class_name == LIT_MAGIC_STRING_STRING_UL) { ecma_string_t *str_p = ecma_op_to_string (value); - result = ecma_make_string_value (str_p); + ecma_deref_object (obj_p); + + if (JERRY_UNLIKELY (str_p == NULL)) + { + return ECMA_VALUE_ERROR; + } + + value = ecma_make_string_value (str_p); } /* 5.c */ else if (class_name == LIT_MAGIC_STRING_BOOLEAN_UL) { ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; - result = ext_object_p->u.class_prop.u.value; - } - - if (!ecma_is_value_empty (result)) - { + value = ext_object_p->u.class_prop.u.value; ecma_deref_object (obj_p); - - if (ECMA_IS_VALUE_ERROR (result)) - { - return result; - } - - value = result; } } diff --git a/tests/jerry/regression-test-issue-3813.js b/tests/jerry/regression-test-issue-3813.js new file mode 100644 index 0000000000..6bebfcda24 --- /dev/null +++ b/tests/jerry/regression-test-issue-3813.js @@ -0,0 +1,23 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var o = Object("str"); +o.toString = Symbol; + +try { + JSON.stringify(o); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +}