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

Re: printf %q segfault



On Sun, 16 Oct 2016 16:03:12 +0000
Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> 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
+>你你



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