zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Peter Stephenson <p.stephenson@samsung.com>
Cc: zsh-workers@zsh.org
Subject: Re: Heap corruption [the thread formerly known as substitution]
Date: Sat, 3 Oct 2015 19:59:29 +0100	[thread overview]
Message-ID: <20151003195929.0a23491f@ntlworld.com> (raw)
In-Reply-To: <20151001161341.6a15f3c6@pwslap01u.europe.root.pri>

On Thu, 01 Oct 2015 16:13:41 +0100
Peter Stephenson <p.stephenson@samsung.com> 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) {


  reply	other threads:[~2015-10-03 19:05 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-26 12:19 Substitution ${...///} slows down when certain UTF character occurs Sebastian Gniazdowski
2015-09-26 20:44 ` Bart Schaefer
2015-09-27  8:13   ` Sebastian Gniazdowski
2015-09-27 16:11     ` Bart Schaefer
2015-09-28  8:51       ` Peter Stephenson
2015-09-28 11:30         ` Peter Stephenson
2015-09-28 19:23         ` Peter Stephenson
2015-09-29  8:44           ` Peter Stephenson
2015-09-29 18:37             ` Peter Stephenson
2015-09-29 19:23               ` Bart Schaefer
2015-09-30  8:59                 ` Peter Stephenson
2015-09-30 14:04                   ` Peter Stephenson
2015-09-30 21:19                     ` Bart Schaefer
2015-10-01  8:41                       ` Peter Stephenson
2015-10-01 14:28                         ` Heap corruption [the thread formerly known as substitution] Peter Stephenson
2015-10-01 15:07                           ` Bart Schaefer
2015-10-01 15:13                           ` Peter Stephenson
2015-10-03 18:59                             ` Peter Stephenson [this message]
2015-10-01 13:45       ` Substitution ${...///} slows down when certain UTF character occurs Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151003195929.0a23491f@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).