ref: 22430de3a9bd077deea6d5854751d81ca97fb424
parent: 833f82ca952952e6e584e3aaf258004b99f2b999
author: Tor Andersson <tor.andersson@artifex.com>
date: Thu May 3 06:44:18 EDT 2018
Fix creation of empty regular expressions. Handle empty regular expressions in RegExp compiler asserts. Also turn them into /(?:)/ on the JS side, so they can be printed back as valid JS syntax where // is a comment, not a regular expression.
--- a/jsregexp.c
+++ b/jsregexp.c
@@ -116,12 +116,15 @@
pattern = old->source;
flags = old->flags;
} else if (js_isundefined(J, 1)) {
- pattern = "";
+ pattern = "(?:)";
flags = 0;
} else {
pattern = js_tostring(J, 1);
flags = 0;
}
+
+ if (strlen(pattern) == 0)
+ pattern = "(?:)";
if (js_isdefined(J, 2)) {
const char *s = js_tostring(J, 2);
--- a/regexp.c
+++ b/regexp.c
@@ -831,9 +831,12 @@
g.prog = alloc(ctx, NULL, sizeof (Reprog));
if (!g.prog)
die(&g, "cannot allocate regular expression");
- g.pstart = g.pend = alloc(ctx, NULL, sizeof (Renode) * strlen(pattern) * 2);
- if (!g.pstart)
- die(&g, "cannot allocate regular expression parse list");
+ n = strlen(pattern) * 2;
+ if (n > 0) {
+ g.pstart = g.pend = alloc(ctx, NULL, sizeof (Renode) * n);
+ if (!g.pstart)
+ die(&g, "cannot allocate regular expression parse list");
+ }
g.source = pattern;
g.ncclass = 0;