Skip to content

Commit 0f37828

Browse files
committed
Fix memory leak in RegExp builtin.
Related issue: #787 JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 4a5a8cb commit 0f37828

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

jerry-core/ecma/operations/ecma-regexp-object.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,9 +1412,10 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
14121412
re_set_result_array_properties (result_array_obj_p, input_str_p, re_ctx.num_of_captures / 2, index);
14131413
ecma_deref_ecma_string (input_str_p);
14141414

1415-
for (uint32_t i = 0; i < re_ctx.num_of_captures; i += 2)
1415+
for (uint32_t i = 0; ecma_is_completion_value_empty (ret_value) && i < re_ctx.num_of_captures; i += 2)
14161416
{
14171417
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i / 2);
1418+
ecma_value_t capture_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
14181419

14191420
if (((re_ctx.saved_p[i] && re_ctx.saved_p[i + 1])
14201421
&& re_ctx.saved_p[i + 1] >= re_ctx.saved_p[i]))
@@ -1431,19 +1432,30 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
14311432
{
14321433
capture_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
14331434
}
1434-
ecma_op_object_put (result_array_obj_p, index_str_p, ecma_make_string_value (capture_str_p), true);
1435-
ecma_deref_ecma_string (capture_str_p);
1436-
}
1437-
else
1438-
{
1439-
ecma_op_object_put (result_array_obj_p,
1440-
index_str_p,
1441-
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
1442-
true);
1435+
1436+
capture_value = ecma_make_string_value (capture_str_p);
14431437
}
1438+
1439+
ECMA_TRY_CATCH (put_value,
1440+
ecma_op_object_put (result_array_obj_p,
1441+
index_str_p,
1442+
capture_value,
1443+
true),
1444+
ret_value);
1445+
ECMA_FINALIZE (put_value);
1446+
1447+
ecma_free_value (capture_value, true);
14441448
ecma_deref_ecma_string (index_str_p);
14451449
}
1446-
ret_value = result_array;
1450+
1451+
if (ecma_is_completion_value_empty (ret_value))
1452+
{
1453+
ret_value = result_array;
1454+
}
1455+
else
1456+
{
1457+
ecma_deref_object (result_array_obj_p);
1458+
}
14471459
}
14481460
else
14491461
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 Samsung Electronics Co., Ltd.
2+
// Copyright 2016 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
Array.prototype.push(Math.sin);
17+
Object.freeze(Array.prototype);
18+
19+
try
20+
{
21+
String.prototype.match(String.prototype);
22+
assert (false);
23+
}
24+
catch (e)
25+
{
26+
assert (e instanceof TypeError);
27+
}

0 commit comments

Comments
 (0)