* lib/mktime.c (__mktime_internal) [!_LIBC]: Double the number of
probes.  Although this isn’t guaranteed to suffice, it should be
good enough for practical applications, and fixing the problem
in general would require access to the underlying tz state lock
which would be hard to do.
---
 ChangeLog    | 7 +++++++
 lib/mktime.c | 8 ++++++++
 2 files changed, 15 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index cea31f84e6..65fd11b2bf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2024-10-04  Paul Eggert  <egg...@cs.ucla.edu>
 
+       mktime: improve thread-safety
+       * lib/mktime.c (__mktime_internal) [!_LIBC]: Double the number of
+       probes.  Although this isn’t guaranteed to suffice, it should be
+       good enough for practical applications, and fixing the problem
+       in general would require access to the underlying tz state lock
+       which would be hard to do.
+
        mktime: fix timegm bug that set tmp->tm_isdst
        * lib/timegm.c (__timegm64): Omit now-unnecessary initialization
        of tm_isdst.  Anyway, the initialization was always wrong, since
diff --git a/lib/mktime.c b/lib/mktime.c
index f21dfe3838..7f07abb814 100644
--- a/lib/mktime.c
+++ b/lib/mktime.c
@@ -332,6 +332,14 @@ __mktime_internal (struct tm *tp, bool local, 
mktime_offset_t *offset)
      leap seconds, but some hosts have them anyway.  */
   int remaining_probes = 6;
 
+#ifndef _LIBC
+  /* Gnulib mktime doesn't lock the tz state, so it may need to probe
+     more often if some other thread changes local time while
+     __mktime_internal is probing.  Double the number of probes; this
+     should suffice for practical cases that are at all likely.  */
+  remaining_probes *= 2;
+#endif
+
   /* Time requested.  Copy it in case gmtime/localtime modify *TP;
      this can occur if TP is localtime's returned value and CONVERT is
      localtime.  */
-- 
2.43.0


Reply via email to