[njs] Fixed RegExp literal parsing.
Dmitry Volyntsev
xeioex at nginx.com
Tue Mar 5 16:54:51 UTC 2019
details: https://hg.nginx.org/njs/rev/acc5fc4be35b
branches:
changeset: 817:acc5fc4be35b
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Mar 05 19:54:23 2019 +0300
description:
Fixed RegExp literal parsing.
diffstat:
njs/njs_regexp.c | 11 ++++++-----
njs/test/njs_unit_test.c | 15 +++++++++++++++
2 files changed, 21 insertions(+), 5 deletions(-)
diffs (55 lines):
diff -r 109697757094 -r acc5fc4be35b njs/njs_regexp.c
--- a/njs/njs_regexp.c Tue Mar 05 08:32:54 2019 +0800
+++ b/njs/njs_regexp.c Tue Mar 05 19:54:23 2019 +0300
@@ -138,7 +138,7 @@ njs_regexp_create(njs_vm_t *vm, njs_valu
njs_token_t
njs_regexp_literal(njs_vm_t *vm, njs_parser_t *parser, njs_value_t *value)
{
- u_char *p;
+ u_char *p, c;
njs_lexer_t *lexer;
njs_regexp_flags_t flags;
njs_regexp_pattern_t *pattern;
@@ -147,12 +147,13 @@ njs_regexp_literal(njs_vm_t *vm, njs_par
for (p = lexer->start; p < lexer->end; p++) {
- if (*p == '\\') {
- p++;
- continue;
+ c = *p;
+
+ if (c == '\n' || c == '\r') {
+ break;
}
- if (*p == '/') {
+ if (c == '/' && !(p > lexer->start && p[-1] == '\\')) {
lexer->text.start = lexer->start;
lexer->text.length = p - lexer->text.start;
p++;
diff -r 109697757094 -r acc5fc4be35b njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Tue Mar 05 08:32:54 2019 +0800
+++ b/njs/test/njs_unit_test.c Tue Mar 05 19:54:23 2019 +0300
@@ -6526,6 +6526,21 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("/"),
nxt_string("SyntaxError: Unterminated RegExp \"/\" in 1") },
+ { nxt_string("/a\n/"),
+ nxt_string("SyntaxError: Unterminated RegExp \"/a\" in 1") },
+
+ { nxt_string("/a\r/"),
+ nxt_string("SyntaxError: Unterminated RegExp \"/a\" in 1") },
+
+ { nxt_string("/a\\q/"),
+ nxt_string("/a\\q/") },
+
+ { nxt_string("/a\\q/.test('a\\q')"),
+ nxt_string("true") },
+
+ { nxt_string("/(\\.(?!com|org)|\\/)/.test('ah.info')"),
+ nxt_string("true") },
+
{ nxt_string("/(/.test('')"),
nxt_string("SyntaxError: pcre_compile(\"(\") failed: missing ) in 1") },
More information about the nginx-devel
mailing list