zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <danielsh@apache.org>
To: zsh-workers@zsh.org
Subject: [PATCH 1/3] Fix the mktemp() warning, in debug builds only.
Date: Tue, 17 Dec 2019 07:44:26 +0000	[thread overview]
Message-ID: <20191217074428.3699-1-danielsh@apache.org> (raw)

On Linux, linking to mktemp() generates the following warning:
.
    utils.o: In function `gettempname':
    ./Src/utils.c:2229: warning: the use of `mktemp' is dangerous, better use `mkstemp' or `mkdtemp'

The warning cannot be disabled.

Work around that by using mkstemp() instead, and massage its output so
it behaves like mktemp().  See the new comment for further details.
---
The purpose of this patch is to eliminate the spurious warning from the
edit-compile-test cycle.

Cheers,

Daniel


 Src/utils.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/Src/utils.c b/Src/utils.c
index 1d916e71f..086c0dfcb 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2201,6 +2201,31 @@ gettempname(const char *prefix, int use_heap)
 #ifdef HAVE__MKTEMP
     /* Zsh uses mktemp() safely, so silence the warnings */
     ret = (char *) _mktemp(ret);
+#elif HAVE_MKSTEMP && defined(DEBUG)
+    {
+	/* zsh uses mktemp() safely (all callers use O_EXCL, and one of them
+	 * uses mkfifo()/mknod(), as opposed to open()), but some compilers
+	 * warn about this anyway and give no way to disable the warning. To
+	 * appease them, use mkstemp() and then close the fd and unlink the
+	 * filename, to match callers' expectations.
+	 *
+	 * But do this in debug builds only, because we don't want to suffer
+	 * x3 the disk access (touch, unlink, touch again) in production.
+	 */
+	int fd;
+	errno = 0;
+	fd = mkstemp(ret);
+	if (fd < 0)
+	    zwarn("can't get a temporary filename: %e", errno);
+	else {
+	    close(fd);
+	    ret = ztrdup(ret);
+
+	    errno = 0;
+	    if (unlink(ret) < 0)
+		zwarn("unlinking a temporary filename failed: %e", errno);
+	}
+    }
 #else
     ret = (char *) mktemp(ret);
 #endif

             reply	other threads:[~2019-12-17  7:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-17  7:44 Daniel Shahaf [this message]
2019-12-17  7:44 ` [PATCH 2/3] Make 'make -s' print nothing when it does nothing Daniel Shahaf
2019-12-17  7:44 ` [PATCH 3/3] internal: Document forklevel, locallevel, and exit_pending Daniel Shahaf

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=20191217074428.3699-1-danielsh@apache.org \
    --to=danielsh@apache.org \
    --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).