Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
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,T_DKIM_INVALID
	autolearn=ham autolearn_force=no version=3.4.0
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=brasslantern-com.20150623.gappssmtp.com; s=20150623;
        h=from:message-id:date:in-reply-to:comments:references:to:subject
         :mime-version:content-type;
        bh=DHV2Rl/l4BzXrCf91gAa4gxQl340ULpN7ulsFo5cvF8=;
        b=hjBVFfS3cQ5Ni7U7QWcE4ecF5WmAKzBUNoyLZE4ggXz8jBKLV+mJ05gVyeDt9yo4Oe
         bjOHh1h0Hl5UuSJDd2AhstkSMfdiMmgmYNMzghf3Oeh4LWfxTXwsn6qQx9aRoW/8+hly
         S+SOyb7f/IoMc9vF+tLeLbC4qgc9gmJzFYky4XsH3ExRT9d5lxjT7KqAybgajqTmDrrl
         1eaFQeNrKzANtuZdEVaRZOlUwWKCQwaXeXVjc7kmpP2a6yLOZ6p5eWo3YYSMBsdqZOsF
         SOcmy0AE2QW3t7Fus0WzYxulyN1/8eOtURL/AkRxB9TKshbhza3OR0dd+BE3OdR3w/iT
         G0Eg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:message-id:date:in-reply-to:comments
         :references:to:subject:mime-version:content-type;
        bh=DHV2Rl/l4BzXrCf91gAa4gxQl340ULpN7ulsFo5cvF8=;
        b=IXOmbzbvR/wFcsC/2+sE1WscmksReriRScvwm2+BmYVQ3VxXuQYTg8+my1rAupIb3i
         9OFbMqVfZ3tGe2qybq7ZXnP+m7vT8E8DIEopTyXko6JDO2YnhdwjiqYwbdN9OltIVHiE
         ypdQXPUL/eZidnhoo5RJlIHmwgRtE1B83SI6jM+Rh1Ca+2FNngZY3yXqr8uUqoyRhWjE
         AEcXKsuiz4Cq7JyagBvakmTGV1ZgGdrMsBq9l4qgMlrzEV57ENK9uh0Pmpozq4evJNKi
         r4KgmVI3a8qKx7VkGRpmu9vQprc1VkeZ9RfVT4K7Kl1BUuqxSt2upvHA0SQ5HyCKsefk
         861w==
X-Gm-Message-State: ALoCoQkpJSDMEU0XeiYvxcPtNLdtaa5fMqWldCp2kPFueEUAb/DLBH6ayM72UAfhE3AcDwRgPh2JNuNFgg7sEqYZEtPyNEe+ZA==
X-Received: by 10.98.87.144 with SMTP id i16mr93156019pfj.118.1451771673093;
        Sat, 02 Jan 2016 13:54:33 -0800 (PST)
From: Bart Schaefer <schaefer@brasslantern.com>
Message-Id: <160102135438.ZM12227@torch.brasslantern.com>
Date: Sat, 2 Jan 2016 13:54:38 -0800
In-Reply-To: <160102134237.ZM17072@torch.brasslantern.com>
Comments: In reply to Bart Schaefer <schaefer@brasslantern.com>
        "Re: Printf builtin missing v flag support" (Jan  2,  1:42pm)
References: <068ca8f5-315b-444c-b281-5f183e1daa8c@email.android.com> 
	<151231104858.ZM24513@torch.brasslantern.com> 
	<CAKc7PVCXaicToQHdgaqnOmoqHjqQLxUXQSUrBx=fjs9JH2USKQ@mail.gmail.com> 
	<160101135224.ZM10746@torch.brasslantern.com> 
	<CAKc7PVDsXof4TFRvtY15qThTEPqLz6NiSeL6dsKxqGXyK2aVeg@mail.gmail.com> 
	<160101201724.ZM1844@torch.brasslantern.com> 
	<CAKc7PVA5sdMnjLMObmAV5xkUe1jSgAmaR30S7zZ0od5su13fOA@mail.gmail.com> 
	<160102134237.ZM17072@torch.brasslantern.com>
X-Mailer: OpenZMail Classic (0.9.2 24April2005)
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Printf builtin missing v flag support
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Seq: zsh-workers 37497

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')) {

