Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: zregexparse dumps core when match data is not defined



I forgot to handle the situation which match data is not defined.

% zsh -f
serein% zmodload zsh/zutil
serein% zregexparse p1 p2 a /a/
zsh: segmentation fault (core dumped)  zsh -f

Index: Src/Modules/zutil.c
===================================================================
RCS file: /projects/zsh/zsh/Src/Modules/zutil.c,v
retrieving revision 1.1.1.10
diff -u -r1.1.1.10 zutil.c
--- Src/Modules/zutil.c	2000/02/03 09:40:04	1.1.1.10
+++ Src/Modules/zutil.c	2000/02/03 11:57:20
@@ -683,6 +683,55 @@
 /* Zregexparse stuff. */
 
 typedef struct {
+    char **match;
+    char **mbegin;
+    char **mend;
+} MatchData;
+
+static void
+savematch(MatchData *m)
+{
+    char **a;
+
+    PERMALLOC {
+	a = getaparam("match");
+	m->match = a ? arrdup(a) : NULL;
+	a = getaparam("mbegin");
+	m->mbegin = a ? arrdup(a) : NULL;
+	a = getaparam("mend");
+	m->mend = a ? arrdup(a) : NULL;
+    } LASTALLOC;
+}
+
+static void
+restorematch(MatchData *m)
+{
+    if (m->match)
+	setaparam("match", m->match);
+    else
+	unsetparam("match");
+    if (m->mbegin)
+	setaparam("mbegin", m->mbegin);
+    else
+	unsetparam("mbegin");
+    if (m->mend)
+	setaparam("mend", m->mend);
+    else
+	unsetparam("mend");
+}
+
+static void
+freematch(MatchData *m)
+{
+    if (m->match)
+	freearray(m->match);
+    if (m->mbegin)
+	freearray(m->mbegin);
+    if (m->mend)
+	freearray(m->mend);
+}
+
+typedef struct {
     int cutoff;
     char *pattern;
     Patprog patprog;
@@ -692,8 +741,8 @@
 } RParseState;
 
 typedef struct {
-  RParseState *state;
-  LinkList actions;
+    RParseState *state;
+    LinkList actions;
 } RParseBranch;
 
 typedef struct {
@@ -982,14 +1031,9 @@
     nexts = sm->in;
     addlinknode(nextslist, nexts);
     do {
-	char **savematch1, **savembegin1, **savemend1;
-	char **savematch2, **savembegin2, **savemend2;
+	MatchData match1, match2;
 
-	PERMALLOC {
-	    savematch1 = arrdup(getaparam("match"));
-	    savembegin1 = arrdup(getaparam("mbegin"));
-	    savemend1 = arrdup(getaparam("mend"));
-	} LASTALLOC;
+	savematch(&match1);
 
 	for (ln = firstnode(nexts); ln; ln = nextnode(ln)) {
 	    int i;
@@ -1012,18 +1056,8 @@
 		  if (*subj++ == Meta)
 		    subj++;
 
-		PERMALLOC {
-		    savematch2 = arrdup(getaparam("match"));
-		    savembegin2 = arrdup(getaparam("mbegin"));
-		    savemend2 = arrdup(getaparam("mend"));
-		} LASTALLOC;
-
-		if (savematch1)
-		    setaparam("match", savematch1);
-		if (savembegin1)
-		    setaparam("mbegin", savembegin1);
-		if (savemend1)
-		    setaparam("mend", savemend1);
+		savematch(&match2);
+		restorematch(&match1);
 
 		for (aln = firstnode(br->actions); aln; aln = nextnode(aln)) {
 		    char *action = getdata(aln);
@@ -1031,12 +1065,7 @@
 		    if (action)
 			execstring(action, 1, 0);
 		}
-		if (savematch2)
-		    setaparam("match", savematch2);
-		if (savembegin2)
-		    setaparam("mbegin", savembegin2);
-		if (savemend2)
-		    setaparam("mend", savemend2);
+		restorematch(&match2);
 
 		point2 += len;
 		setiparam(var2, point2);
@@ -1050,15 +1079,9 @@
 		addlinknode(nextslist, nexts);
 		break;
 	    }
-	}
-	if (!ln) {
-	    if (savematch1)
-		freearray(savematch1);
-	    if (savembegin1)
-		freearray(savembegin1);
-	    if (savemend1)
-		freearray(savemend1);
 	}
+	if (!ln)
+	    freematch(&match1);
     } while (ln);
 
     if (!comp && !*subj)
-- 
Tanaka Akira



Messages sorted by: Reverse Date, Date, Thread, Author