Subject: Re: bug#15015: Fix some minor races in hosts lacking mkostemp
From: Paul Eggert <[email-masked]>
To: Eli Zaretskii <[email-masked]>
Cc: [email-masked]
Date: Sun, 04 Aug 2013 10:01:42 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

On 08/04/2013 08:44 AM, Eli Zaretskii wrote:
> So I went ahead and implemented mkostemp for MS-Windows (trunk
> revision 113687).  I also removed sys_mktemp, as it is no longer
> needed.

Thanks, that should do it on the Microsoft side.  I installed
the patch and am marking this done.  The patch I installed
also included the following additional simplification:

=== modified file 'ChangeLog'
--- ChangeLog	2013-08-04 01:23:59 +0000
+++ ChangeLog	2013-08-04 16:52:55 +0000
@@ -2,6 +2,9 @@
 
 	Fix some minor races in hosts lacking mkostemp (Bug#15015).
 	Gnulib's emulation of mkostemp doesn't have races that Emacs's does.
+	* configure.ac (mkostemp): Remove check for this function;
+	gnulib does the check now.
+	(mkstemp): Remove check for this no-longer-used function.
 	* lib/mkostemp.c, lib/secure_getenv.c, lib/tempname.c, lib/tempname.h:
 	* m4/mkostemp.m4, m4/secure_getenv.m4, m4/tempname.m4:
 	New files, copied from Gnulib.

=== modified file 'configure.ac'
--- configure.ac	2013-07-27 01:18:21 +0000
+++ configure.ac	2013-08-04 16:52:25 +0000
@@ -3277,7 +3277,7 @@
 getrlimit setrlimit shutdown getaddrinfo \
 strsignal setitimer \
 sendto recvfrom getsockname getpeername getifaddrs freeifaddrs \
-gai_strerror mkostemp mkstemp getline getdelim sync \
+gai_strerror getline getdelim sync \
 difftime posix_memalign \
 getpwent endpwent getgrent endgrent \
 touchlock \

=== modified file 'lib-src/ChangeLog'
--- lib-src/ChangeLog	2013-07-10 23:23:57 +0000
+++ lib-src/ChangeLog	2013-08-04 16:48:02 +0000
@@ -1,3 +1,12 @@
+2013-08-04  Paul Eggert  <[email-masked]>
+
+	Fix some minor races in hosts lacking mkostemp (Bug#15015).
+	* movemail.c (main):
+	* update-game-score.c (write_scores):
+	Use mkostemp (which now works on all platforms, due to changes
+	in the portability layer) rather than mktemp (which has a race)
+	or mkstemp (which we no longer bother with).
+
 2013-07-10  Paul Eggert  <[email-masked]>
 
 	Port to C89.

=== modified file 'lib-src/movemail.c'
--- lib-src/movemail.c	2013-03-13 18:42:22 +0000
+++ lib-src/movemail.c	2013-08-04 16:35:45 +0000
@@ -304,24 +304,13 @@
 
 	  memcpy (tempname, inname, inname_dirlen);
 	  strcpy (tempname + inname_dirlen, "EXXXXXX");
-#ifdef HAVE_MKSTEMP
-	  desc = mkstemp (tempname);
-#else
-	  mktemp (tempname);
-	  if (!*tempname)
-	    desc = -1;
-	  else
-	    {
-	      unlink (tempname);
-	      desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
-	    }
-#endif
+	  desc = mkostemp (tempname, 0);
 	  if (desc < 0)
 	    {
-	      int mkstemp_errno = errno;
+	      int mkostemp_errno = errno;
 	      error ("error while creating what would become the lock file",
 		     0, 0);
-	      errno = mkstemp_errno;
+	      errno = mkostemp_errno;
 	      pfatal_with_name (tempname);
 	    }
 	  close (desc);

=== modified file 'lib-src/update-game-score.c'
--- lib-src/update-game-score.c	2013-03-30 17:00:51 +0000
+++ lib-src/update-game-score.c	2013-08-04 16:28:47 +0000
@@ -383,6 +383,7 @@
 static int
 write_scores (const char *filename, const struct score_entry *scores, int count)
 {
+  int fd;
   FILE *f;
   int i;
   char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
@@ -390,12 +391,11 @@
     return -1;
   strcpy (tempfile, filename);
   strcat (tempfile, ".tempXXXXXX");
-#ifdef HAVE_MKSTEMP
-  if (mkstemp (tempfile) < 0
-#else
-  if (mktemp (tempfile) != tempfile
-#endif
-      || !(f = fopen (tempfile, "w")))
+  fd = mkostemp (tempfile, 0);
+  if (fd < 0)
+    return -1;
+  f = fdopen (fd, "w");
+  if (! f)
     return -1;
   for (i = 0; i < count; i++)
     if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,