zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: PATCH: refactor memstream for "print -v"
Date: Wed, 6 Jan 2016 01:31:03 +0900	[thread overview]
Message-ID: <4052A17B-0432-44A7-8A84-F615FD836FCF@kba.biglobe.ne.jp> (raw)
In-Reply-To: <160104231830.ZM20279@torch.brasslantern.com>

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 = open_memstream(&BUF, &mcount)) == NULL) { \
+        if ((FOUT = open_memstream(&BUF, &mcount)) == 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 = gettempfile(NULL, 1, &tmpf)) < 0 || \
-            (fout = fdopen(tempfd, "w+")) == NULL) { \
-            zwarnnam(name, "can't open temp file: %e", errno); \
-            return 1; \
-        } \
-        unlink(tmpf); \
+	if ((tempfd = gettempfile(NULL, 1, &tmpf)) < 0) { \
+	    zwarnnam(name, "can't create temp file: %e", errno); \
+	    return 1; \
+	} \
+	unlink(tmpf); \
+	if ((fout = fdopen(tempfd, "w+")) == NULL) { \
+	    close(tempfd);  \
+	    zwarnnam(name, "can't open temp file: %e", errno); \
+	    return 1; \
+	} \
     } while (0)
 #define READ_MSTREAM(BUF,COUNT,FOUT) \
-    ((((count = ftell(FOUT)), (BUF = (char *)zalloc(count + 1))) && \
-      ((fseek(FOUT, 0L, SEEK_SET) == 0) && !(BUF[count] = '\0')) && \
-      ((COUNT = fread(BUF, 1, count, FOUT)) == count)) ? count : -1)
+    ((((count = ftell(FOUT)), (BUF = (char *)zalloc(count))) && \
+      (fseek(FOUT, 0L, SEEK_SET) == 0) && \
+      ((COUNT = fread(BUF, 1, count, FOUT)) == count)) ? (int)count : -1)
 #define CLOSE_MSTREAM(FOUT) fclose(FOUT)
 
 #endif



  parent reply	other threads:[~2016-01-05 17:14 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-05  7:18 Bart Schaefer
2016-01-05  9:48 ` Peter Stephenson
2016-01-05 11:47   ` Peter Stephenson
2016-01-05 16:33     ` Bart Schaefer
2016-01-05 16:31 ` Jun T. [this message]
2016-01-05 17:58   ` Bart Schaefer
2016-01-06  6:02     ` Jun T.
2016-01-06 21:30       ` Bart Schaefer
2016-01-08 12:32         ` Jun T.
2016-01-09  4:37           ` Bart Schaefer

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=4052A17B-0432-44A7-8A84-F615FD836FCF@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --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).