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 autolearn=ham
	autolearn_force=no version=3.4.0
X-Biglobe-Sender: <takimoto-j@kba.biglobe.ne.jp>
Content-Type: text/plain; charset=us-ascii
Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\))
Subject: Re: PATCH: refactor memstream for "print -v"
From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
In-Reply-To: <160104231830.ZM20279@torch.brasslantern.com>
Date: Wed, 6 Jan 2016 01:31:03 +0900
Content-Transfer-Encoding: quoted-printable
Message-Id: <4052A17B-0432-44A7-8A84-F615FD836FCF@kba.biglobe.ne.jp>
References: <160104231830.ZM20279@torch.brasslantern.com>
To: zsh-workers@zsh.org
X-Mailer: Apple Mail (2.1878.6)
X-Biglobe-Spnum: 51366
X-Seq: zsh-workers 37508

The patch below is against 37503+37504.

The first part of the second hank is to deal with a rare case
that gettempfile() succeeds but fdopen() fails.

The last part (READ_MSTREAM) is for not adding a trailing NULL
to buf. The cast (int)count is just to silence the following
warning from clang:

builtin.c:4435:56: warning: comparison of unsigned expression < 0 is =
always
      false [-Wtautological-compare]
        if (IS_MSTREAM(fout) && READ_MSTREAM(buf,rcount,fout) < 0)
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~


diff --git a/Src/builtin.c b/Src/builtin.c
index 2201184..e04f090 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4028,7 +4028,7 @@ bin_print(char *name, char **args, Options ops, =
int func)
     size_t mcount;
 #define ASSIGN_MSTREAM(BUF,FOUT) \
     do { \
-        if ((fout =3D open_memstream(&BUF, &mcount)) =3D=3D NULL) { \
+        if ((FOUT =3D open_memstream(&BUF, &mcount)) =3D=3D NULL) { \
             zwarnnam(name, "open_memstream failed"); \
             return 1; \
         } \
@@ -4049,17 +4049,21 @@ bin_print(char *name, char **args, Options ops, =
int func)
     do { \
         int tempfd; \
         char *tmpf; \
-        if ((tempfd =3D gettempfile(NULL, 1, &tmpf)) < 0 || \
-            (fout =3D fdopen(tempfd, "w+")) =3D=3D NULL) { \
-            zwarnnam(name, "can't open temp file: %e", errno); \
-            return 1; \
-        } \
-        unlink(tmpf); \
+	if ((tempfd =3D gettempfile(NULL, 1, &tmpf)) < 0) { \
+	    zwarnnam(name, "can't create temp file: %e", errno); \
+	    return 1; \
+	} \
+	unlink(tmpf); \
+	if ((fout =3D fdopen(tempfd, "w+")) =3D=3D NULL) { \
+	    close(tempfd);  \
+	    zwarnnam(name, "can't open temp file: %e", errno); \
+	    return 1; \
+	} \
     } while (0)
 #define READ_MSTREAM(BUF,COUNT,FOUT) \
-    ((((count =3D ftell(FOUT)), (BUF =3D (char *)zalloc(count + 1))) && =
\
-      ((fseek(FOUT, 0L, SEEK_SET) =3D=3D 0) && !(BUF[count] =3D '\0')) =
&& \
-      ((COUNT =3D fread(BUF, 1, count, FOUT)) =3D=3D count)) ? count : =
-1)
+    ((((count =3D ftell(FOUT)), (BUF =3D (char *)zalloc(count))) && \
+      (fseek(FOUT, 0L, SEEK_SET) =3D=3D 0) && \
+      ((COUNT =3D fread(BUF, 1, count, FOUT)) =3D=3D count)) ? =
(int)count : -1)
 #define CLOSE_MSTREAM(FOUT) fclose(FOUT)
=20
 #endif


