On 8/16/22 09:25, Eli Zaretskii wrote:
why does this work differently on
different platforms?
Platforms that lack clock_gettime and CLOCK_MONOTONIC fall back on a
deterministic algorithm. Presumably MS-DOS one of the few such
platforms, which is why the problem is observed only on MS-DOS.
How about something like the attached patch to Gnulib's lib/tempname.c?
If I understand things correctly this should make the names random
enough on MS-DOS, though Emacs itself still needs a patch as I mentioned
a few minutes ago.
If this patch isn't good enough for MS-DOS, what sort of random bits are
easily available on that platform? We don't need anything
cryptographically secure; a higher-res clock should suffice.diff --git a/lib/tempname.c b/lib/tempname.c
index e6520191d7..e3d09d963d 100644
--- a/lib/tempname.c
+++ b/lib/tempname.c
@@ -71,7 +71,8 @@
/* Use getrandom if it works, falling back on a 64-bit linear
congruential generator that starts with Var's value
- mixed in with a clock's low-order bits if available. */
+ mixed in with a clock's low-order bits (or with some other
+ pseudorandom number) if available. */
typedef uint_fast64_t random_value;
#define RANDOM_VALUE_MAX UINT_FAST64_MAX
#define BASE_62_DIGITS 10 /* 62**10 < UINT_FAST64_MAX */
@@ -89,6 +90,9 @@ random_bits (random_value var, bool use_getrandom)
struct __timespec64 tv;
__clock_gettime64 (CLOCK_MONOTONIC, &tv);
var ^= tv.tv_nsec;
+#else
+ /* Fall back on pseudorandom number <https://bugs.gnu.org/57129>. */
+ var ^= rand ();
#endif
return 2862933555777941757 * var + 3037000493;
}