zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@zsh.org
Subject: Re: printf %q segfault
Date: Tue, 18 Oct 2016 20:57:15 +0100	[thread overview]
Message-ID: <20161018205715.09036030@ntlworld.com> (raw)
In-Reply-To: <20161016160312.GA695@fujitsu.shahaf.local2>

On Sun, 16 Oct 2016 16:03:12 +0000
Daniel Shahaf <d.s@daniel.shahaf.name> wrote:

> lolilolicon wrote on Sun, Oct 16, 2016 at 22:58:14 +0800:
> > The following produces segmentation fault:
> > 
> >     printf '%q' 你
> 
> The reason 0xA0 is output literally is that the code takes the "if (itok(*u))"
> branch in quotestring(); if it didn't take that branch, it'd behave
> correctly.

hmm...

mumble mumble metafy mumble bin_print mumble mumble total madness mumble
metafy shmetafy token shmoken grmph.

pws

diff --git a/Src/builtin.c b/Src/builtin.c
index 8b8b217..2db739f 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4874,9 +4874,10 @@ bin_print(char *name, char **args, Options ops, int func)
 		break;
 	    case 'q':
 		stringval = curarg ?
-		    quotestring(curarg, QT_BACKSLASH_SHOWNULL) : &nullstr;
+		    quotestring(metafy(curarg, curlen, META_USEHEAP),
+				QT_BACKSLASH_SHOWNULL) : &nullstr;
 		*d = 's';
-		print_val(stringval);
+		print_val(unmetafy(stringval, &curlen));
 		break;
 	    case 'd':
 	    case 'i':
diff --git a/Src/utils.c b/Src/utils.c
index db43529..e2657de 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -5916,7 +5916,24 @@ quotestring(const char *s, int instring)
 		}
 	    }
 
-	    if (itok(*u) || instring != QT_BACKSLASH) {
+	    /*
+	     * Now check if the output is unprintable in the
+	     * current character set.
+	     */
+	    uend = u + MB_METACHARLENCONV(u, &cc);
+	    if (
+#ifdef MULTIBYTE_SUPPORT
+		cc != WEOF &&
+#endif
+		WC_ISPRINT(cc)) {
+		if (dobackslash)
+		    *v++ = '\\';
+		while (u < uend) {
+		    if (*u == Meta)
+			*v++ = *u++;
+		    *v++ = *u++;
+		}
+	    } else if (itok(*u) || instring != QT_BACKSLASH) {
 		/* Needs to be passed straight through. */
 		if (dobackslash)
 		    *v++ = '\\';
@@ -5940,25 +5957,6 @@ quotestring(const char *s, int instring)
 		} else
 		    *v++ = *u++;
 		continue;
-	    }
-
-	    /*
-	     * Now check if the output is unprintable in the
-	     * current character set.
-	     */
-	    uend = u + MB_METACHARLENCONV(u, &cc);
-	    if (
-#ifdef MULTIBYTE_SUPPORT
-		cc != WEOF &&
-#endif
-		WC_ISPRINT(cc)) {
-		if (dobackslash)
-		    *v++ = '\\';
-		while (u < uend) {
-		    if (*u == Meta)
-			*v++ = *u++;
-		    *v++ = *u++;
-		}
 	    } else {
 		/* Not printable */
 		*v++ = '$';
diff --git a/Test/D07multibyte.ztst b/Test/D07multibyte.ztst
index 1b1d042..3a6e955 100644
--- a/Test/D07multibyte.ztst
+++ b/Test/D07multibyte.ztst
@@ -579,3 +579,7 @@
 0:Sorting of metafied Polish characters
 >a ą b c ć d e ę f
 >a ą b c ć d e ę f
+
+  printf '%q%q\n' 你你
+0:printf %q and quotestring and general metafy / token madness
+>你你


  reply	other threads:[~2016-10-18 20:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-16 14:58 lolilolicon
2016-10-16 16:03 ` Daniel Shahaf
2016-10-18 19:57   ` Peter Stephenson [this message]
2016-10-19  8:52     ` Peter Stephenson

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=20161018205715.09036030@ntlworld.com \
    --to=p.w.stephenson@ntlworld.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).