[njs] Fixed RegExp() constructor with empty pattern and non-empty flags.
Dmitry Volyntsev
xeioex at nginx.com
Tue Apr 14 12:48:27 UTC 2020
details: https://hg.nginx.org/njs/rev/c6f1ae6d8fac
branches:
changeset: 1372:c6f1ae6d8fac
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Apr 14 12:42:05 2020 +0000
description:
Fixed RegExp() constructor with empty pattern and non-empty flags.
diffstat:
src/njs_regexp.c | 54 +++++++++++++++++++----------------------------
src/test/njs_unit_test.c | 9 ++++++++
2 files changed, 31 insertions(+), 32 deletions(-)
diffs (108 lines):
diff -r 777ed1eb1918 -r c6f1ae6d8fac src/njs_regexp.c
--- a/src/njs_regexp.c Tue Apr 14 12:18:25 2020 +0000
+++ b/src/njs_regexp.c Tue Apr 14 12:42:05 2020 +0000
@@ -104,29 +104,11 @@ njs_regexp_constructor(njs_vm_t *vm, njs
u_char *start;
njs_int_t ret;
njs_str_t string;
- njs_value_t source, flags_string, *pattern, *flags;
+ njs_value_t source, *pattern, *flags;
njs_regexp_flags_t re_flags;
pattern = njs_arg(args, nargs, 1);
- if (!njs_is_regexp(pattern) && !njs_is_primitive(pattern)) {
- ret = njs_value_to_string(vm, &args[1], &args[1]);
- if (ret != NJS_OK) {
- return ret;
- }
- }
-
- flags = njs_arg(args, nargs, 2);
-
- if (!njs_is_primitive(flags)) {
- ret = njs_value_to_string(vm, &args[2], &args[2]);
- if (ret != NJS_OK) {
- return ret;
- }
- }
-
- re_flags = 0;
-
if (njs_is_regexp(pattern)) {
ret = njs_regexp_prototype_source(vm, NULL, pattern, NULL, &source);
if (njs_slow_path(ret != NJS_OK)) {
@@ -138,25 +120,28 @@ njs_regexp_constructor(njs_vm_t *vm, njs
pattern = &source;
} else {
- if (njs_is_undefined(pattern)) {
+ if (njs_is_defined(pattern)) {
+ ret = njs_value_to_string(vm, pattern, pattern);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
+
+ } else {
pattern = njs_value_arg(&njs_string_empty);
}
- ret = njs_primitive_value_to_string(vm, &source, pattern);
+ re_flags = 0;
+ }
+
+ flags = njs_arg(args, nargs, 2);
+
+ if (njs_is_defined(flags)) {
+ ret = njs_value_to_string(vm, flags, flags);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
- pattern = &source;
- }
-
- if (njs_is_defined(flags)) {
- ret = njs_primitive_value_to_string(vm, &flags_string, flags);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
- }
-
- njs_string_get(&flags_string, &string);
+ njs_string_get(flags, &string);
start = string.start;
@@ -181,7 +166,12 @@ njs_regexp_create(njs_vm_t *vm, njs_valu
njs_regexp_t *regexp;
njs_regexp_pattern_t *pattern;
- if (length != 0) {
+ if (length != 0 || flags != 0) {
+ if (length == 0) {
+ start = (u_char *) "(?:)";
+ length = njs_length("(?:)");
+ }
+
pattern = njs_regexp_pattern_create(vm, start, length, flags);
if (njs_slow_path(pattern == NULL)) {
return NJS_ERROR;
diff -r 777ed1eb1918 -r c6f1ae6d8fac src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Tue Apr 14 12:18:25 2020 +0000
+++ b/src/test/njs_unit_test.c Tue Apr 14 12:42:05 2020 +0000
@@ -7391,6 +7391,15 @@ static njs_unit_test_t njs_test[] =
{ njs_str("RegExp('\\\\0').source[1]"),
njs_str("0") },
+ { njs_str("RegExp(undefined, 'g').global"),
+ njs_str("true") },
+
+ { njs_str("RegExp('', 'g').global"),
+ njs_str("true") },
+
+ { njs_str("var x; RegExp(x, 'g')"),
+ njs_str("/(?:)/g") },
+
{ njs_str("']'.match(/]/)"),
njs_str("]") },
More information about the nginx-devel
mailing list