Skip to content

Commit 7d6ebc0

Browse files
committed
Fix assertion in 're_insert_simple_iterator'
It is a followup fix after jerryscript-project#2169. It also fixes a memory leak. This fixes jerryscript-project#2198 and fixes jerryscript-project#2204 JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 3c57698 commit 7d6ebc0

File tree

4 files changed

+83
-27
lines changed

4 files changed

+83
-27
lines changed

jerry-core/parser/regexp/re-compiler.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ re_append_char_class (void *re_ctx_p, /**< RegExp compiler context */
5252

5353
/**
5454
* Insert simple atom iterator
55+
*
56+
* @return empty ecma value - if inserted successfully
57+
* error ecma value - otherwise
58+
*
59+
* Returned value must be freed with ecma_free_value
5560
*/
56-
static void
61+
static ecma_value_t
5762
re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
5863
uint32_t new_atom_start_offset) /**< atom start offset */
5964
{
@@ -63,7 +68,15 @@ re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler con
6368

6469
qmin = re_ctx_p->current_token.qmin;
6570
qmax = re_ctx_p->current_token.qmax;
66-
JERRY_ASSERT (qmin <= qmax);
71+
72+
if (qmin == 1 && qmax == 1)
73+
{
74+
return ECMA_VALUE_EMPTY;
75+
}
76+
else if (qmin > qmax)
77+
{
78+
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: qmin > qmax."));
79+
}
6780

6881
/* TODO: optimize bytecode length. Store 0 rather than INF */
6982

@@ -83,6 +96,8 @@ re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler con
8396
{
8497
re_insert_opcode (re_ctx_p->bytecode_ctx_p, offset, RE_OP_NON_GREEDY_ITERATOR);
8598
}
99+
100+
return ECMA_VALUE_EMPTY;
86101
} /* re_insert_simple_iterator */
87102

88103
/**
@@ -271,21 +286,15 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
271286
re_append_char (bc_ctx_p, re_canonicalize ((ecma_char_t) re_ctx_p->current_token.value,
272287
re_ctx_p->flags & RE_FLAG_IGNORE_CASE));
273288

274-
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
275-
{
276-
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
277-
}
289+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
278290
break;
279291
}
280292
case RE_TOK_PERIOD:
281293
{
282294
JERRY_TRACE_MSG ("Compile a period\n");
283295
re_append_opcode (bc_ctx_p, RE_OP_PERIOD);
284296

285-
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
286-
{
287-
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
288-
}
297+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
289298
break;
290299
}
291300
case RE_TOK_ALTERNATIVE:
@@ -387,21 +396,17 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
387396
: RE_OP_CHAR_CLASS);
388397
uint32_t offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
389398

390-
ECMA_TRY_CATCH (empty_value,
391-
re_parse_char_class (re_ctx_p->parser_ctx_p,
392-
re_append_char_class,
393-
re_ctx_p,
394-
&(re_ctx_p->current_token)),
395-
ret_value);
396-
re_insert_u32 (bc_ctx_p, offset, re_ctx_p->parser_ctx_p->num_of_classes);
399+
ret_value = re_parse_char_class (re_ctx_p->parser_ctx_p,
400+
re_append_char_class,
401+
re_ctx_p,
402+
&(re_ctx_p->current_token));
397403

398-
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
404+
if (!ECMA_IS_VALUE_ERROR (ret_value))
399405
{
400-
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
406+
re_insert_u32 (bc_ctx_p, offset, re_ctx_p->parser_ctx_p->num_of_classes);
407+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
401408
}
402409

403-
ECMA_FINALIZE (empty_value);
404-
405410
break;
406411
}
407412
case RE_TOK_END_GROUP:

jerry-core/parser/regexp/re-parser.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ecma-exceptions.h"
1717
#include "ecma-globals.h"
1818
#include "ecma-try-catch-macro.h"
19+
#include "jcontext.h"
1920
#include "jrt-libc-includes.h"
2021
#include "lit-char-helpers.h"
2122
#include "re-compiler.h"
@@ -242,11 +243,6 @@ re_parse_iterator (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */
242243

243244
JERRY_ASSERT (ecma_is_value_empty (ret_value));
244245

245-
if (re_token_p->qmin > re_token_p->qmax)
246-
{
247-
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: qmin > qmax."));
248-
}
249-
250246
return ret_value;
251247
} /* re_parse_iterator */
252248

@@ -893,11 +889,15 @@ re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context *
893889
const lit_utf8_byte_t *input_curr_p = parser_ctx_p->input_curr_p;
894890

895891
lit_utf8_decr (&parser_ctx_p->input_curr_p);
896-
if (ecma_is_value_empty (re_parse_iterator (parser_ctx_p, out_token_p)))
892+
ret_value = re_parse_iterator (parser_ctx_p, out_token_p);
893+
if (ecma_is_value_empty (ret_value))
897894
{
898895
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid RegExp token."));
899896
}
900897

898+
JERRY_ASSERT (ECMA_IS_VALUE_ERROR (ret_value));
899+
ecma_free_value (JERRY_CONTEXT (error_value));
900+
901901
parser_ctx_p->input_curr_p = input_curr_p;
902902

903903
out_token_p->type = RE_TOK_CHAR;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 id_0 = Object.prototype.toString ;
16+
Object.prototype.toString = function ( ) { return "SHIFTED" } ;
17+
RegExp ( '#1: __str = new String({}); typeof __str === "object". Actual: typeof __str ===');
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
try {
16+
new RegExp("\{{91406,456}");
17+
assert(false);
18+
} catch(e) {
19+
assert(e instanceof SyntaxError)
20+
}
21+
22+
try {
23+
new RegExp("\{91406,456}");
24+
assert(false);
25+
} catch(e) {
26+
assert(e instanceof SyntaxError)
27+
}
28+
29+
try {
30+
new RegExp("\({91406,456}");
31+
assert(false);
32+
} catch(e) {
33+
assert(e instanceof SyntaxError)
34+
}

0 commit comments

Comments
 (0)