Skip to content

Commit 87b1d1e

Browse files
authored
Fix regexp flag handling in case of regexp like object (#3766)
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 0822299 commit 87b1d1e

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
@@ -138,6 +138,10 @@ ecma_builtin_regexp_dispatch_helper (const ecma_value_t *arguments_list_p, /**<
138138
return flags_value;
139139
}
140140
}
141+
else
142+
{
143+
flags_value = ecma_copy_value (flags_value);
144+
}
141145

142146
free_arguments = true;
143147
}
@@ -153,47 +157,37 @@ ecma_builtin_regexp_dispatch_helper (const ecma_value_t *arguments_list_p, /**<
153157
}
154158
#endif /* ENABLED (JERRY_ES2015) */
155159

160+
ecma_value_t ret_value = ECMA_VALUE_ERROR;
156161
ecma_object_t *new_target_obj_p = ecma_op_regexp_alloc (new_target_p);
157162

158-
if (JERRY_UNLIKELY (new_target_obj_p == NULL))
163+
if (JERRY_LIKELY (new_target_obj_p != NULL))
159164
{
160165
#if ENABLED (JERRY_ES2015)
161-
if (free_arguments)
166+
if (create_regexp_from_bc)
162167
{
163-
ecma_free_value (pattern_value);
164-
ecma_free_value (flags_value);
168+
ret_value = ecma_op_create_regexp_from_bytecode (new_target_obj_p, bc_p);
169+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (ret_value));
165170
}
171+
else
166172
#endif /* ENABLED (JERRY_ES2015) */
173+
{
174+
ret_value = ecma_op_create_regexp_from_pattern (new_target_obj_p, pattern_value, flags_value);
167175

168-
return ECMA_VALUE_ERROR;
176+
if (ECMA_IS_VALUE_ERROR (ret_value))
177+
{
178+
ecma_deref_object (new_target_obj_p);
179+
}
180+
}
169181
}
170182

171-
ecma_value_t ret_value;
172-
173183
#if ENABLED (JERRY_ES2015)
174-
if (create_regexp_from_bc)
175-
{
176-
ret_value = ecma_op_create_regexp_from_bytecode (new_target_obj_p, bc_p);
177-
}
178-
else
179-
{
180-
#endif /* ENABLED (JERRY_ES2015) */
181-
ret_value = ecma_op_create_regexp_from_pattern (new_target_obj_p, pattern_value, flags_value);
182-
#if ENABLED (JERRY_ES2015)
183-
}
184-
185184
if (free_arguments)
186185
{
187186
ecma_free_value (pattern_value);
188187
ecma_free_value (flags_value);
189188
}
190189
#endif /* ENABLED (JERRY_ES2015) */
191190

192-
if (ECMA_IS_VALUE_ERROR (ret_value))
193-
{
194-
ecma_deref_object (new_target_obj_p);
195-
}
196-
197191
return ret_value;
198192
} /* ecma_builtin_regexp_dispatch_helper */
199193

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)