Subject: Re: Printf builtin missing v flag support
From: Bart Schaefer <[email-masked]>
To: Zsh hackers list <[email-masked]>
Date: Sat, 2 Jan 2016 13:54:38 -0800
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Jan 2,  1:42pm, Bart Schaefer wrote:
}
} Having written that out, it looks like there might be bugs with using
} all off -u/-p plus -z/-s/-v plus -c/-C.  Haven't actually tried.  There
} might also be bugs with printf-ing of NUL bytes into the memstream.

Yes, that byte count I keep saying is important was even more important
than I thought.  Also add some error checking on the tempfile branch.

diff --git a/Src/builtin.c b/Src/builtin.c
index 557487c..04d8f11 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4872,17 +4872,20 @@ bin_print(char *name, char **args, Options ops, int func)
 
     if (OPT_ISSET(ops,'z') || OPT_ISSET(ops,'s') || OPT_ISSET(ops,'v')) {
 #ifdef HAVE_OPEN_MEMSTREAM
-	putc(0, fout);
+	putc(0, fout);		/* not needed?  open_memstream() maintains? */
 	fclose(fout);
 	fout = NULL;
+	rcount = mcount;	/* now includes the trailing NUL we added */
 #else
 	rewind(fout);
 	buf = (char *)zalloc(count + 1);
-	fread(buf, count, 1, fout);
-	buf[count] = '\0';
+	rcount = fread(buf, count, 1, fout);
+	if (rcount < count)
+	    zwarnnam(name, "i/o error: %e", errno);
+	buf[rcount] = '\0';
 #endif
 	queue_signals();
-	stringval = metafy(buf, -1, META_REALLOC);
+	stringval = metafy(buf, rcount - 1, META_REALLOC);
 	if (OPT_ISSET(ops,'z')) {
 	    zpushnode(bufstack, stringval);
 	} else if (OPT_ISSET(ops,'v')) {