Skip to content

Commit b602433

Browse files
committed
Fix regexp flag handling in case of regexp like object
The regexp flag should be correctly referenced and released if an existing regexp like object is used for constructing a new one. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent a4659a8 commit b602433

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ ecma_builtin_regexp_dispatch_helper (const ecma_value_t *arguments_list_p, /**<
136136
return flags_value;
137137
}
138138
}
139+
else
140+
{
141+
flags_value = ecma_copy_value (flags_value);
142+
}
139143

140144
free_arguments = true;
141145
}
@@ -151,47 +155,37 @@ ecma_builtin_regexp_dispatch_helper (const ecma_value_t *arguments_list_p, /**<
151155
}
152156
#endif /* ENABLED (JERRY_ES2015) */
153157

158+
ecma_value_t ret_value = ECMA_VALUE_ERROR;
154159
ecma_object_t *new_target_obj_p = ecma_op_regexp_alloc (new_target_p);
155160

156-
if (JERRY_UNLIKELY (new_target_obj_p == NULL))
161+
if (JERRY_LIKELY (new_target_obj_p != NULL))
157162
{
158163
#if ENABLED (JERRY_ES2015)
159-
if (free_arguments)
164+
if (create_regexp_from_bc)
160165
{
161-
ecma_free_value (pattern_value);
162-
ecma_free_value (flags_value);
166+
ret_value = ecma_op_create_regexp_from_bytecode (new_target_obj_p, bc_p);
167+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (ret_value));
163168
}
169+
else
164170
#endif /* ENABLED (JERRY_ES2015) */
171+
{
172+
ret_value = ecma_op_create_regexp_from_pattern (new_target_obj_p, pattern_value, flags_value);
165173

166-
return ECMA_VALUE_ERROR;
174+
if (ECMA_IS_VALUE_ERROR (ret_value))
175+
{
176+
ecma_deref_object (new_target_obj_p);
177+
}
178+
}
167179
}
168180

169-
ecma_value_t ret_value;
170-
171181
#if ENABLED (JERRY_ES2015)
172-
if (create_regexp_from_bc)
173-
{
174-
ret_value = ecma_op_create_regexp_from_bytecode (new_target_obj_p, bc_p);
175-
}
176-
else
177-
{
178-
#endif /* ENABLED (JERRY_ES2015) */
179-
ret_value = ecma_op_create_regexp_from_pattern (new_target_obj_p, pattern_value, flags_value);
180-
#if ENABLED (JERRY_ES2015)
181-
}
182-
183182
if (free_arguments)
184183
{
185184
ecma_free_value (pattern_value);
186185
ecma_free_value (flags_value);
187186
}
188187
#endif /* ENABLED (JERRY_ES2015) */
189188

190-
if (ECMA_IS_VALUE_ERROR (ret_value))
191-
{
192-
ecma_deref_object (new_target_obj_p);
193-
}
194-
195189
return ret_value;
196190
} /* ecma_builtin_regexp_dispatch_helper */
197191

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 split = RegExp.prototype[Symbol.split];
16+
17+
try {
18+
split.call({[Symbol.match]: "g"});
19+
assert(false);
20+
} catch (ex) {
21+
assert(ex instanceof SyntaxError);
22+
}

0 commit comments

Comments
 (0)