zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] isearch: do not use PAT_STATIC since we call zle hooks
@ 2017-01-06 17:25 m0viefreak
  2017-01-06 17:35 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: m0viefreak @ 2017-01-06 17:25 UTC (permalink / raw)
  To: zsh-workers

Using PAT_STATIC, any of the functions called in the hook that make
use of patterns, implicitly change the pattern we use in the
isearch loop. In the best case this results in pattern-not-found,
in the worst case we get a dump.

Use PAT_ZDUP instead and take care of freeing it again.

Minimal reproducing example:

% bindkey '^R' history-incremental-pattern-search-backward
% evil_hook() { a=(); : ${a[(r)foo*]}; };
% zle -N zle-isearch-update evil_hook
% : foo
% : bar
% : baz
%

type: <^R>b
% : baz
bck-i-search: b_

type: <^R>
% : foo
bck-i-search: b_

': foo' is found instead of ': bar' because evil_hook modified the
static pattern used in isearch.

Related: zsh-syntax-highlighting issue which found this bug:
https://github.com/zsh-users/zsh-syntax-highlighting/issues/407
---
 Src/Zle/zle_hist.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/Src/Zle/zle_hist.c b/Src/Zle/zle_hist.c
index abd6e17..434735d 100644
--- a/Src/Zle/zle_hist.c
+++ b/Src/Zle/zle_hist.c
@@ -1220,13 +1220,12 @@ doisearch(char **args, int dir, int pattern)
 		char *patbuf = ztrdup(sbuf);
 		char *patstring;
 		/*
-		 * Use static pattern buffer since we don't need
-		 * to maintain it and won't call other pattern functions
-		 * meanwhile.
+		 * Do not use static pattern buffer (PAT_STATIC) since we call zle hooks,
+		 * which might call other pattern functions. Use PAT_ZDUP instead.
 		 * Use PAT_NOANCH because we don't need the match
 		 * anchored to the end, even if it is at the start.
 		 */
-		int patflags = PAT_STATIC|PAT_NOANCH;
+		int patflags = PAT_ZDUP|PAT_NOANCH;
 		if (sbuf[0] == '^') {
 		    /*
 		     * We'll handle the anchor later when
@@ -1521,6 +1520,7 @@ doisearch(char **args, int dir, int pattern)
 		    if (only_one || !top_spot || old_sbptr != sbptr)
 			break;
 		}
+		freepatprog(patprog);
 		patprog = NULL;
 		nosearch = 1;
 		skip_pos = 0;
@@ -1632,6 +1632,7 @@ doisearch(char **args, int dir, int pattern)
 	    }
 	    strcpy(sbuf + sbptr, paste);
 	    sbptr += pastelen;
+	    freepatprog(patprog);
 	    patprog = NULL;
 	    free(paste);
 	} else if (cmd == Th(z_acceptsearch)) {
@@ -1682,6 +1683,7 @@ doisearch(char **args, int dir, int pattern)
 	     * always valid at this point.
 	     */
 	    sbptr += zlecharasstring(LASTFULLCHAR, sbuf + sbptr);
+	    freepatprog(patprog);
 	    patprog = NULL;
 	}
 	if (feep)
@@ -1702,6 +1704,7 @@ doisearch(char **args, int dir, int pattern)
     zsfree(okeymap);
     if (matchlist)
 	freematchlist(matchlist);
+    freepatprog(patprog);
     isearch_active = 0;
     /*
      * Don't allow unused characters provided as a string to the
-- 
2.8.3



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-01-29 16:24 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-06 17:25 [PATCH] isearch: do not use PAT_STATIC since we call zle hooks m0viefreak
2017-01-06 17:35 ` Bart Schaefer
2017-01-08 19:33 ` Bart Schaefer
2017-01-08 19:40   ` Peter Stephenson
2017-01-28 19:02     ` Bart Schaefer
2017-01-28 19:15       ` Peter Stephenson
2017-01-28 19:39       ` Bart Schaefer
2017-01-28 19:47         ` Peter Stephenson
2017-01-29  0:01           ` Bart Schaefer
2017-01-29 16:24             ` Bart Schaefer
2017-01-09  1:35 ` Daniel Shahaf
2017-01-09  1:48   ` Bart Schaefer
2017-01-11  2:01     ` Fwd: " Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).