shithub: libmujs

Download patch

ref: b4484ab18a8bca9feed3555c2b6f5d029ca2c052
parent: 8c868344b207fbcaee4622fb6c0b97d1bd5c79a9
author: Tor Andersson <[email protected]>
date: Thu Jan 2 07:31:46 EST 2020

Issue 118: Add REG_ to limit defines, and use REG_MAXSUB in header.

--- a/regexp.c
+++ b/regexp.c
@@ -14,21 +14,18 @@
 #define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
 
 #define REPINF 255
-#ifndef MAXSUB
-#define MAXSUB REG_MAXSUB
+#ifndef REG_MAXPROG
+#define REG_MAXPROG (32 << 10)
 #endif
-#ifndef MAXPROG
-#define MAXPROG (32 << 10)
+#ifndef REG_MAXREC
+#define REG_MAXREC 1024
 #endif
-#ifndef MAXREC
-#define MAXREC 1024
+#ifndef REG_MAXSPAN
+#define REG_MAXSPAN 64
 #endif
-#ifndef MAXSPAN
-#define MAXSPAN 64
+#ifndef REG_MAXCLASS
+#define REG_MAXCLASS 16
 #endif
-#ifndef MAXCLASS
-#define MAXCLASS 16
-#endif
 
 typedef struct Reclass Reclass;
 typedef struct Renode Renode;
@@ -37,7 +34,7 @@
 
 struct Reclass {
 	Rune *end;
-	Rune spans[MAXSPAN];
+	Rune spans[REG_MAXSPAN];
 };
 
 struct Reprog {
@@ -44,7 +41,7 @@
 	Reinst *start, *end;
 	int flags;
 	int nsub;
-	Reclass cclass[MAXCLASS];
+	Reclass cclass[REG_MAXCLASS];
 };
 
 struct cstate {
@@ -54,7 +51,7 @@
 	const char *source;
 	int ncclass;
 	int nsub;
-	Renode *sub[MAXSUB];
+	Renode *sub[REG_MAXSUB];
 
 	int lookahead;
 	Rune yychar;
@@ -505,7 +502,7 @@
 		return newnode(g, P_ANY);
 	if (accept(g, '(')) {
 		atom = newnode(g, P_PAR);
-		if (g->nsub == MAXSUB)
+		if (g->nsub == REG_MAXSUB)
 			die(g, "too many captures");
 		atom->n = g->nsub++;
 		atom->x = parsealt(g);
@@ -625,7 +622,7 @@
 		if (min == max) n = count(g, node->x) * min;
 		else if (max < REPINF) n = count(g, node->x) * max + (max - min);
 		else n = count(g, node->x) * (min + 1) + 2;
-		if (n < 0 || n > MAXPROG) die(g, "program too large");
+		if (n < 0 || n > REG_MAXPROG) die(g, "program too large");
 		return n;
 	case P_PAR: return count(g, node->x) + 2;
 	case P_PLA: return count(g, node->x) + 2;
@@ -849,7 +846,7 @@
 	if (!g.prog)
 		die(&g, "cannot allocate regular expression");
 	n = strlen(pattern) * 2;
-	if (n > MAXPROG)
+	if (n > REG_MAXPROG)
 		die(&g, "program too large");
 	if (n > 0) {
 		g.pstart = g.pend = alloc(ctx, NULL, sizeof (Renode) * n);
@@ -860,7 +857,7 @@
 	g.source = pattern;
 	g.ncclass = 0;
 	g.nsub = 1;
-	for (i = 0; i < MAXSUB; ++i)
+	for (i = 0; i < REG_MAXSUB; ++i)
 		g.sub[i] = 0;
 
 	g.prog->flags = cflags;
@@ -878,7 +875,7 @@
 #endif
 
 	n = 6 + count(&g, node);
-	if (n < 0 || n > MAXPROG)
+	if (n < 0 || n > REG_MAXPROG)
 		die(&g, "program too large");
 
 	g.prog->nsub = g.nsub;
@@ -988,7 +985,7 @@
 	Rune c;
 
 	/* stack overflow */
-	if (depth > MAXREC)
+	if (depth > REG_MAXREC)
 		return -1;
 
 	for (;;) {
@@ -1154,7 +1151,7 @@
 		sub = &scratch;
 
 	sub->nsub = prog->nsub;
-	for (i = 0; i < MAXSUB; ++i)
+	for (i = 0; i < REG_MAXSUB; ++i)
 		sub->sub[i].sp = sub->sub[i].ep = NULL;
 
 	return match(prog->start, sp, sp, prog->flags | eflags, sub, 0);
--- a/regexp.h
+++ b/regexp.h
@@ -26,10 +26,14 @@
 
 	/* regexec flags */
 	REG_NOTBOL = 4,
-
-	/* limits */
-	REG_MAXSUB = 10
 };
+
+/* If you redefine REG_MAXSUB, you must make sure both the calling
+ * code and the regexp.c compilation unit use the same value!
+ */
+#ifndef REG_MAXSUB
+#define REG_MAXSUB 10
+#endif
 
 struct Resub {
 	int nsub;