From 9a6bdef228f47aaec6f72cbaa06507e001f8d451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Lang=C3=B3?= Date: Wed, 10 Feb 2016 08:31:23 +0100 Subject: [PATCH] Fix assertion in RegExp compile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related issue: #641 JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com --- .../ecma-builtin-regexp-prototype.cpp | 19 +++++++++++---- tests/jerry/regression-test-issue-641.js | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 tests/jerry/regression-test-issue-641.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.cpp index 9e63c9d27..d95021f88 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.cpp @@ -62,12 +62,11 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument ecma_value_t flags_arg) /**< flags */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); - ecma_object_t *this_obj_p = ecma_get_object_from_value (this_arg); - if (ecma_object_get_class_name (this_obj_p) != LIT_MAGIC_STRING_REGEXP_UL) + if (!ecma_is_value_object (this_arg) + || ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_REGEXP_UL) { - /* Compile can only be called on RegExp objects. */ - ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + ret_value = ecma_raise_type_error ("Incomplete RegExp type"); } else { @@ -121,6 +120,9 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument ecma_deref_ecma_string (magic_string_p); + ECMA_TRY_CATCH (obj_this, ecma_op_to_object (this_arg), ret_value); + ecma_object_t *this_obj_p = ecma_get_object_from_value (obj_this); + /* Get bytecode property. */ ecma_property_t *bc_prop_p = ecma_get_internal_property (this_obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE); @@ -146,6 +148,8 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument re_initialize_props (this_obj_p, pattern_string_p, flags); ret_value = ecma_make_normal_completion_value (ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED)); + + ECMA_FINALIZE (obj_this); } } else @@ -189,6 +193,9 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument if (ecma_is_completion_value_empty (ret_value)) { + ECMA_TRY_CATCH (obj_this, ecma_op_to_object (this_arg), ret_value); + ecma_object_t *this_obj_p = ecma_get_object_from_value (obj_this); + ecma_property_t *bc_prop_p = ecma_get_internal_property (this_obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE); /* Try to compile bytecode from new source. */ @@ -211,6 +218,8 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument ret_value = ecma_make_normal_completion_value (ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED)); ECMA_FINALIZE (bc_dummy); + + ECMA_FINALIZE (obj_this); } if (pattern_string_p != NULL) @@ -254,7 +263,7 @@ ecma_builtin_regexp_prototype_exec (ecma_value_t this_arg, /**< this argument */ ret_value); ecma_property_t *bytecode_prop_p; - ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this); + ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); bytecode_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE); void *bytecode_p = ECMA_GET_POINTER (void, bytecode_prop_p->u.internal_property.value); diff --git a/tests/jerry/regression-test-issue-641.js b/tests/jerry/regression-test-issue-641.js new file mode 100644 index 000000000..3bea75cfe --- /dev/null +++ b/tests/jerry/regression-test-issue-641.js @@ -0,0 +1,24 @@ +// Copyright 2016 Samsung Electronics Co., Ltd. +// Copyright 2016 University of Szeged. +// +// 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. + +try +{ + Object.freeze(RegExp.prototype.compile)(); + assert(false); +} +catch (e) +{ + assert(e instanceof TypeError); +}