Subject: bug#57129: 29.0.50; Improve behavior of conditionals in Eshell

From: Bruno Haible <[email-masked]>
Date: Tue, 16 Aug 2022 21:59:10 +0200

Eli Zaretskii wrote:
> Looking at your test program, I see that you generate the seconds file
> name without deleting the first one.  When a file by the first
> generated name already exists, gen_tempname will indeed generate a
> different name.  But in the scenario I described, Emacs creates one
> temporary file, uses it, then deletes it, and only after that creates
> another file.

I'm adding this scenario to the unit test. Still, the unit test succeeds
when run once on:
  Linux, FreeBSD, NetBSD, OpenBSD, macOS, Solaris, Cygwin, native Windows.

Since this contradicts what you debugged on mingw, I ran the test 10000
times on native Windows. Result:
  - on 32-bit mingw, no failure.
  - on 64-bit mingw, around 30 failures (or around 10 failures with Paul's
    newest patch). That is, a probability of ca. 0.3% of getting the same
    file name as on the previous call.

Bruno
>From aa52cadc36fb1af0509dc3a4bce4ce73197ece68 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email-masked]>
Date: Tue, 16 Aug 2022 21:50:11 +0200
Subject: [PATCH] tempname: Add more tests.

Based on scenario described by Eli Zaretskii in
<https://lists.gnu.org/archive/html/bug-gnulib/2022-08/msg00043.html>.

* tests/test-tempname.c (main): Add another test.
* modules/tempname-tests (Status): Mark the test as unportable.
---
 ChangeLog              |  8 ++++++
 modules/tempname-tests |  3 ++
 tests/test-tempname.c  | 65 ++++++++++++++++++++++++++++++------------
 3 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eb96281591..de113ce081 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-08-16  Bruno Haible  <[email-masked]>
+
+	tempname: Add more tests.
+	Based on scenario described by Eli Zaretskii in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2022-08/msg00043.html>.
+	* tests/test-tempname.c (main): Add another test.
+	* modules/tempname-tests (Status): Mark the test as unportable.
+
 2022-08-16  Paul Eggert  <[email-masked]>
 
 	tempname: generate better names for MinGW Emacs
diff --git a/modules/tempname-tests b/modules/tempname-tests
index 384f98707b..adccd0d8e9 100644
--- a/modules/tempname-tests
+++ b/modules/tempname-tests
@@ -2,6 +2,9 @@ Files:
 tests/test-tempname.c
 tests/macros.h
 
+Status:
+unportable-test
+
 Depends-on:
 unlink
 
diff --git a/tests/test-tempname.c b/tests/test-tempname.c
index d463eec2b4..4a0ca65f2c 100644
--- a/tests/test-tempname.c
+++ b/tests/test-tempname.c
@@ -34,24 +34,53 @@ main ()
   char filename1[18 + 1];
   char filename2[18 + 1];
 
-  strcpy (filename1, templ18);
-  int fd1 = gen_tempname (filename1, strlen (".xyz"), 0, GT_FILE);
-  ASSERT (fd1 >= 0);
-
-  strcpy (filename2, templ18);
-  int fd2 = gen_tempname (filename2, strlen (".xyz"), 0, GT_FILE);
-  ASSERT (fd2 >= 0);
-
-  /* With 6 'X' and a good pseudo-random number generator behind the scenes,
-     the probability of getting the same file name twice in a row should be
-     1/62^6 < 1/10^10.  */
-  ASSERT (strcmp (filename1, filename2) != 0);
-
-  /* Clean up.  */
-  close (fd1);
-  close (fd2);
-  unlink (filename1);
-  unlink (filename2);
+  /* Case 1: The first file still exists while gen_tempname is called a second
+     time.  */
+  {
+    strcpy (filename1, templ18);
+    int fd1 = gen_tempname (filename1, strlen (".xyz"), 0, GT_FILE);
+    ASSERT (fd1 >= 0);
+
+    strcpy (filename2, templ18);
+    int fd2 = gen_tempname (filename2, strlen (".xyz"), 0, GT_FILE);
+    ASSERT (fd2 >= 0);
+
+    /* gen_tempname arranges (via O_EXCL) to not return the name of an existing
+       file.  */
+    ASSERT (strcmp (filename1, filename2) != 0);
+
+    /* Clean up.  */
+    close (fd1);
+    close (fd2);
+    unlink (filename1);
+    unlink (filename2);
+  }
+
+  /* Case 2: The first file is deleted before gen_tempname is called a second
+     time.  */
+  {
+    strcpy (filename1, templ18);
+    int fd1 = gen_tempname (filename1, strlen (".xyz"), 0, GT_FILE);
+    ASSERT (fd1 >= 0);
+
+    /* Clean up.  */
+    close (fd1);
+    unlink (filename1);
+
+    strcpy (filename2, templ18);
+    int fd2 = gen_tempname (filename2, strlen (".xyz"), 0, GT_FILE);
+    ASSERT (fd2 >= 0);
+
+    /* With 6 'X' and a good pseudo-random number generator behind the scenes,
+       the probability of getting the same file name twice in a row should be
+       1/62^6 < 1/10^10.
+       But on 64-bit native Windows, this probability is ca. 0.1% to 0.3%.  */
+    ASSERT (strcmp (filename1, filename2) != 0);
+
+    /* Clean up.  */
+    close (fd2);
+    unlink (filename2);
+  }
 
   return 0;
 }
-- 
2.34.1