Skip to content

Commit 0b78d4a

Browse files
committed
Fix assertion in 're_insert_simple_iterator'
It is a followup fix after #2169. Fixes #2204. It also fixes a memory leak. JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent d7991ae commit 0b78d4a

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ re_append_char_class (void *re_ctx_p, /**< RegExp compiler context */
5353
/**
5454
* Insert simple atom iterator
5555
*/
56-
static void
56+
static ecma_value_t
5757
re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
5858
uint32_t new_atom_start_offset) /**< atom start offset */
5959
{
@@ -63,7 +63,14 @@ re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler con
6363

6464
qmin = re_ctx_p->current_token.qmin;
6565
qmax = re_ctx_p->current_token.qmax;
66-
JERRY_ASSERT (qmin <= qmax);
66+
67+
if (qmin == 1 && qmax == 1) {
68+
return ECMA_VALUE_EMPTY;
69+
}
70+
else if (qmin > qmax)
71+
{
72+
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: qmin > qmax."));
73+
}
6774

6875
/* TODO: optimize bytecode length. Store 0 rather than INF */
6976

@@ -83,6 +90,8 @@ re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler con
8390
{
8491
re_insert_opcode (re_ctx_p->bytecode_ctx_p, offset, RE_OP_NON_GREEDY_ITERATOR);
8592
}
93+
94+
return ECMA_VALUE_EMPTY;
8695
} /* re_insert_simple_iterator */
8796

8897
/**
@@ -271,21 +280,15 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
271280
re_append_char (bc_ctx_p, re_canonicalize ((ecma_char_t) re_ctx_p->current_token.value,
272281
re_ctx_p->flags & RE_FLAG_IGNORE_CASE));
273282

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-
}
283+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
278284
break;
279285
}
280286
case RE_TOK_PERIOD:
281287
{
282288
JERRY_TRACE_MSG ("Compile a period\n");
283289
re_append_opcode (bc_ctx_p, RE_OP_PERIOD);
284290

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-
}
291+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
289292
break;
290293
}
291294
case RE_TOK_ALTERNATIVE:
@@ -387,21 +390,16 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
387390
: RE_OP_CHAR_CLASS);
388391
uint32_t offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
389392

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);
393+
ret_value = re_parse_char_class (re_ctx_p->parser_ctx_p,
394+
re_append_char_class,
395+
re_ctx_p,
396+
&(re_ctx_p->current_token));
397397

398-
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
399-
{
400-
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
398+
if (!ECMA_IS_VALUE_ERROR(ret_value)) {
399+
re_insert_u32 (bc_ctx_p, offset, re_ctx_p->parser_ctx_p->num_of_classes);
400+
ret_value = re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
401401
}
402402

403-
ECMA_FINALIZE (empty_value);
404-
405403
break;
406404
}
407405
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: 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)