Skip to content

Commit 6b8332e

Browse files
committed
Fix 'Segmentation fault on String.Replace'
Related issue: #747 JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent d420be3 commit 6b8332e

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
440440
if (str_curr_p <= re_ctx_p->input_start_p)
441441
{
442442
JERRY_DDLOG ("match\n");
443-
break;
443+
break; /* tail merge */
444444
}
445445

446446
if (!(re_ctx_p->flags & RE_FLAG_MULTILINE))
@@ -452,7 +452,7 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
452452
if (lit_char_is_line_terminator (lit_utf8_peek_prev (str_curr_p)))
453453
{
454454
JERRY_DDLOG ("match\n");
455-
break;
455+
break; /* tail merge */
456456
}
457457

458458
JERRY_DDLOG ("fail\n");
@@ -1266,20 +1266,23 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
12661266

12671267
MEM_DEFINE_LOCAL_ARRAY (input_buffer_p, input_string_size, lit_utf8_byte_t);
12681268

1269+
re_matcher_ctx_t re_ctx;
1270+
lit_utf8_byte_t *input_curr_p = NULL;
12691271
ssize_t sz = ecma_string_to_utf8_string (input_string_p, input_buffer_p, (ssize_t) input_string_size);
12701272
JERRY_ASSERT (sz >= 0);
12711273

1272-
lit_utf8_byte_t *input_curr_p = input_buffer_p;
1273-
1274-
if (!input_string_size)
1274+
if (input_string_size == 0u)
12751275
{
12761276
input_curr_p = (lit_utf8_byte_t *) lit_get_magic_string_utf8 (LIT_MAGIC_STRING__EMPTY);
12771277
}
1278-
lit_utf8_byte_t *input_end_p = input_buffer_p + input_string_size;
1278+
else
1279+
{
1280+
input_curr_p = input_buffer_p;
1281+
}
12791282

1280-
re_matcher_ctx_t re_ctx;
1281-
re_ctx.input_start_p = input_buffer_p;
1282-
re_ctx.input_end_p = input_buffer_p + input_string_size;
1283+
re_ctx.input_start_p = input_curr_p;
1284+
const lit_utf8_byte_t *input_end_p = re_ctx.input_start_p + input_string_size;
1285+
re_ctx.input_end_p = input_end_p;
12831286

12841287
/* 1. Read bytecode header and init regexp matcher context. */
12851288
re_ctx.flags = bc_p->flags;
@@ -1390,7 +1393,8 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
13901393
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL);
13911394
ecma_number_t *lastindex_num_p = ecma_alloc_number ();
13921395

1393-
if (sub_str_p)
1396+
if (sub_str_p != NULL
1397+
&& input_buffer_p != NULL)
13941398
{
13951399
*lastindex_num_p = lit_utf8_string_length (input_buffer_p,
13961400
(lit_utf8_size_t) (sub_str_p - input_buffer_p));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
assert (''.replace(/$/g,"a") === "a");
17+
assert (''.replace(/^/g,"a") === "a");

0 commit comments

Comments
 (0)