From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13744 invoked by alias); 3 Oct 2015 19:05:07 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 36760 Received: (qmail 5769 invoked from network); 3 Oct 2015 19:05:05 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Originating-IP: [80.3.228.158] X-Spam: 0 X-Authority: v=2.1 cv=TNS4MARa c=1 sm=1 tr=0 a=P+FLVI8RzFchTbbqTxIDRw==:117 a=P+FLVI8RzFchTbbqTxIDRw==:17 a=NLZqzBF-AAAA:8 a=kj9zAlcOel0A:10 a=hD80L64hAAAA:8 a=UT34LePq7lWalCrtNM0A:9 a=FmQHyihgFAi6qFGs:21 a=XHroO7DfEvJZxbbZ:21 a=CjuIK1q_8ugA:10 Date: Sat, 3 Oct 2015 19:59:29 +0100 From: Peter Stephenson To: Peter Stephenson Cc: zsh-workers@zsh.org Subject: Re: Heap corruption [the thread formerly known as substitution] Message-ID: <20151003195929.0a23491f@ntlworld.com> In-Reply-To: <20151001161341.6a15f3c6@pwslap01u.europe.root.pri> References: <150926134410.ZM17546@torch.brasslantern.com> <150927091121.ZM25721@torch.brasslantern.com> <20150928095142.385a33eb@pwslap01u.europe.root.pri> <20150928202312.6679b38e@ntlworld.com> <20150929094436.32b62692@pwslap01u.europe.root.pri> <20150929193726.38235c76@ntlworld.com> <150929122356.ZM30421@torch.brasslantern.com> <20150930095950.6c9c583b@pwslap01u.europe.root.pri> <20150930150433.21f70e13@pwslap01u.europe.root.pri> <150930141937.ZM22962@torch.brasslantern.com> <20151001094140.413b5401@pwslap01u.europe.root.pri> <20151001152823.19512ecd@pwslap01u.europe.root.pri> <20151001161341.6a15f3c6@pwslap01u.europe.root.pri> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.28; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 01 Oct 2015 16:13:41 +0100 Peter Stephenson wrote: > - /* inherited from domatch, but why, exactly? */ > - if (*string == Nularg) { > + /* > + * Special signalling of empty tokinised string. > + */ > + if ((!patstralloc || stringlen > 0) && *string == Nularg) { One extra piece of sanity is that we should do this once and before any unmetafication which will obscure the difference between Nulart and a formerly metafied Nularg. pws diff --git a/Src/pattern.c b/Src/pattern.c index 04d3e3d..8b07cca 100644 --- a/Src/pattern.c +++ b/Src/pattern.c @@ -2023,6 +2023,39 @@ pattrystart(void) } /* + * Fix up string length stuff. + * + * If we call patallocstr() with "force" to set things up early, it's + * done there, else it's done in pattryrefs(). The reason for the + * difference is in the latter case we may not be relying on + * patallocstr() having an effect. + */ + +/**/ +static void +patmungestring(char **string, int *stringlen, int *unmetalenin) +{ + /* + * Special signalling of empty tokenised string. + */ + if (*stringlen > 0 && **string == Nularg) { + (*string)++; + /* + * If we don't have an unmetafied length + * and need it (we may not) we'll get it later. + */ + if (*unmetalenin > 0) + (*unmetalenin)--; + if (*stringlen > 0) + (*stringlen)--; + } + + /* Ensure we have a metafied length */ + if (*stringlen < 0) + *stringlen = strlen(*string); +} + +/* * Allocate memeory for pattern match. Note this is specific to use * of pattern *and* trial string. * @@ -2039,7 +2072,8 @@ pattrystart(void) * force is 1 if we always unmetafy: this is useful if we are going * to try again with different versions of the string. If this is * called from pattryrefs() we don't force unmetafication as it won't - * be optimal. + * be optimal. This option should be used if the resulting + * patstralloc is going to be passed to pattrylen() / pattryrefs(). * In patstralloc (supplied by caller, must last until last pattry is done) * unmetalen is the unmetafied length of the string; it will be * calculated if the input value is negative. @@ -2056,6 +2090,9 @@ char *patallocstr(Patprog prog, char *string, int stringlen, int unmetalen, { int needfullpath; + if (force) + patmungestring(&string, &stringlen, &unmetalen); + /* * For a top-level ~-exclusion, we will need the full * path to exclude, so copy the path so far and append the @@ -2224,21 +2261,9 @@ pattryrefs(Patprog prog, char *string, int stringlen, int unmetalenin, maxnpos = *nump; *nump = 0; } - /* - * Special signalling of empty tokenised string. - */ - if ((!patstralloc || stringlen > 0) && *string == Nularg) { - string++; - if (unmetalenin > 0) - unmetalenin--; - if (stringlen > 0) - stringlen--; - } - if (stringlen < 0) { - DPUTS(patstralloc != NULL, "length needed with patstralloc"); - stringlen = strlen(string); - } + if (!patstralloc) + patmungestring(&string, &stringlen, &unmetalenin); origlen = stringlen; if (patstralloc) {