Skip to content

Commit 2841188

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 3782449 commit 2841188

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

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

Lines changed: 18 additions & 21 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,40 @@ 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;
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);
163167
}
168+
else
164169
#endif /* ENABLED (JERRY_ES2015) */
170+
{
171+
ret_value = ecma_op_create_regexp_from_pattern (new_target_obj_p, pattern_value, flags_value);
172+
}
165173

166-
return ECMA_VALUE_ERROR;
167-
}
168-
169-
ecma_value_t ret_value;
170-
171-
#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);
174+
if (ECMA_IS_VALUE_ERROR (ret_value))
175+
{
176+
ecma_deref_object (new_target_obj_p);
177+
}
175178
}
176179
else
177180
{
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+
ret_value = ECMA_VALUE_ERROR;
181182
}
182183

184+
#if ENABLED (JERRY_ES2015)
183185
if (free_arguments)
184186
{
185187
ecma_free_value (pattern_value);
186188
ecma_free_value (flags_value);
187189
}
188190
#endif /* ENABLED (JERRY_ES2015) */
189191

190-
if (ECMA_IS_VALUE_ERROR (ret_value))
191-
{
192-
ecma_deref_object (new_target_obj_p);
193-
}
194-
195192
return ret_value;
196193
} /* ecma_builtin_regexp_dispatch_helper */
197194

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)