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

Re: segfault via completion menu



On Thu, 2019-05-23 at 17:34 +0100, Peter Stephenson wrote:
> On Wed, 2019-05-22 at 09:49 +0100, Peter Stephenson wrote:
> > On Tue, 2019-05-21 at 15:19 -0700, Bart Schaefer wrote:
> > It'll be something in the prog passed into pattry() from evalcond() and
> > I'm guesing in this case the pprog in that function came from
> > stat->prog->pats[npat] so was fished out of the existing programme
> > rather than compiled locally.
> 
> If so, something in the following assumptions is being violated, but I
> can't see what from looking at the code.

Alternatively, there could be some internal state in the pattern
matching left over in the case of an error return --- there are
variables that are intended to be used in recursive matching but
currently aren't explicity initialised at the start of matching.  It's
not clear how that could happen, but it would be safest anyway to
sanitise always on entry.

pws

diff --git a/Src/pattern.c b/Src/pattern.c
index 737f5cdcb..3d30b013c 100644
--- a/Src/pattern.c
+++ b/Src/pattern.c
@@ -2030,6 +2030,16 @@ int errsfound;				/* Total error count so far */
 /**/
 int forceerrs;				/* Forced maximum error count */
 
+/*
+ * exactpos is used to remember how far down an exact string we have
+ * matched, if we are doing approximation and can therefore redo from
+ * the same point; we never need to otherwise.
+ *
+ * exactend is a pointer to the end of the string, which isn't
+ * null-terminated.
+ */
+static char *exactpos, *exactend;
+
 /**/
 void
 pattrystart(void)
@@ -2463,6 +2473,8 @@ pattryrefs(Patprog prog, char *string, int stringlen, int unmetalenin,
 
 	patinput = patinstart;
 
+	exactpos = exactend = NULL;
+	/* The only external call to patmatch --- all others are recursive */
 	if (patmatch((Upat)progstr)) {
 	    /*
 	     * we were lazy and didn't save the globflags if an exclusion
@@ -2652,16 +2664,6 @@ patmatchlen(void)
 #define CHARMATCH_EXPR(expr, chpa) \
 	(charmatch_cache = (expr), CHARMATCH(charmatch_cache, chpa))
 
-/*
- * exactpos is used to remember how far down an exact string we have
- * matched, if we are doing approximation and can therefore redo from
- * the same point; we never need to otherwise.
- *
- * exactend is a pointer to the end of the string, which isn't
- * null-terminated.
- */
-static char *exactpos, *exactend;
-
 /*
  * Main matching routine.
  *



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