Skip to content

Commit e6664f6

Browse files
rerobikaLaszloLango
authored andcommitted
Fix heap buffer overflow in re_parse_char_class (#2352)
This patch fixes #2230 and fixes #2237. Test cases are added for both issues and also adds new cases which caused the same error. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent ac9fce1 commit e6664f6

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */
409409
}
410410
}
411411
}
412-
else if (ch == LIT_CHAR_LOWERCASE_X)
412+
else if (ch == LIT_CHAR_LOWERCASE_X && re_hex_lookup (parser_ctx_p, 2))
413413
{
414414
ecma_char_t code_unit;
415415

@@ -419,15 +419,17 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */
419419
}
420420

421421
parser_ctx_p->input_curr_p += 2;
422-
if (is_range == false && lit_utf8_peek_next (parser_ctx_p->input_curr_p) == LIT_CHAR_MINUS)
422+
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p
423+
&& is_range == false
424+
&& lit_utf8_peek_next (parser_ctx_p->input_curr_p) == LIT_CHAR_MINUS)
423425
{
424426
start = code_unit;
425427
continue;
426428
}
427429

428430
ch = code_unit;
429431
}
430-
else if (ch == LIT_CHAR_LOWERCASE_U)
432+
else if (ch == LIT_CHAR_LOWERCASE_U && re_hex_lookup (parser_ctx_p, 4))
431433
{
432434
ecma_char_t code_unit;
433435

@@ -437,7 +439,9 @@ re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */
437439
}
438440

439441
parser_ctx_p->input_curr_p += 4;
440-
if (is_range == false && lit_utf8_peek_next (parser_ctx_p->input_curr_p) == LIT_CHAR_MINUS)
442+
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p
443+
&& is_range == false
444+
&& lit_utf8_peek_next (parser_ctx_p->input_curr_p) == LIT_CHAR_MINUS)
441445
{
442446
start = code_unit;
443447
continue;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
16+
try {
17+
((new RegExp("[\\u0")).exec("u"));
18+
assert (false);
19+
} catch (e) {
20+
assert (e instanceof SyntaxError);
21+
}
22+
23+
try {
24+
((new RegExp("[\\x0")).exec("x"));
25+
assert (false);
26+
} catch (e) {
27+
assert (e instanceof SyntaxError);
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
16+
try {
17+
(new RegExp("[\\u0020")).exec("u");
18+
assert (false);
19+
} catch (e) {
20+
assert (e instanceof SyntaxError);
21+
}
22+
23+
try {
24+
(new RegExp("[\\x20")).exec("x");
25+
assert (false);
26+
} catch (e) {
27+
assert (e instanceof SyntaxError);
28+
}

0 commit comments

Comments
 (0)