Thanks for reporting the problem. I installed the attached three patches to the 
development repository on GitHub. The first is minor refactoring nearby; the 
second fixes the 32-bit checking to be as strict as the 64-bit (the main point 
of your bug report); the third looks forward to tightening up the checking even 
more in the future.

On 2026-10-21 20:21, Ana Bobillo via tz wrote:

+    than 64 bits.  Formerly, with 32-bit time_t such a file could cause
+    these functions to use uninitialized transition times.

I didn't see use of uninitialized transition times in the unpatched code. Can 
you give a scenario when that happened? At any rate I hope the attached patches 
fix the problem.

Thanks again.
From 0c7ec005d2286225787f4dac1c66bb6450694519 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Fri, 23 Oct 2026 16:07:55 -0700
Subject: [PROPOSED 1/3] Omit tzloadbody conditional branch

* localtime.c (tzloadbody): Avoid need for conditional branch
when adding a type.
---
 localtime.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/localtime.c b/localtime.c
index 29a70eb4..9a71869e 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1139,10 +1139,11 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
 		timecnt = 0;
 		for (i = 0; i < sp->timecnt; ++i) {
 			unsigned char typ = *p++;
+			unsigned char kepttype = sp->types[i];
 			if (sp->typecnt <= typ)
 			  return EFTYPE;
-			if (sp->types[i])
-				sp->types[timecnt++] = typ;
+			sp->types[timecnt] = typ;
+			timecnt += kepttype;
 		}
 		sp->timecnt = timecnt;
 		for (i = 0; i < sp->typecnt; ++i) {
-- 
2.53.0

From 9fb2c56f4dd3a34e441efdc88e6795004e86e00a Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 24 Oct 2026 22:09:20 -0700
Subject: [PROPOSED 2/3] Make 32- and 64-bit tzloadbody equally picky

Problem reported by Ana Bobillo in:
https://lists.iana.org/hyperkitty/list/[email protected]/thread/2W2JBUSD2LFFRYFWOOEDXV2672ZSV2U2/
* localtime.c (tzloadbody): Fail if any transition is out of order,
even if it is out of range for time_t.  That way, 32-bit time_t
code acts consistently with 64-bit.
---
 NEWS        | 4 ++++
 localtime.c | 8 +++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index e5097661..45a8856d 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,10 @@ Unreleased, experimental changes
     localtime.c no longer supports 49 leap seconds, as 27 now suffice.
     zic now warns if you go over the new limit.
 
+    With 32-bit time_t, localtime.c functions now check for transition
+    ordering in TZif data as carefully as they do with 64-bit time_t.
+    (Problem reported by Ana Bobillo.)
+
   Changes to documentation and commentary
 
     Documentation and commentary now reflect leap seconds’ demise.
diff --git a/localtime.c b/localtime.c
index 9a71869e..61013447 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1108,6 +1108,7 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
 	    else {
 		int_fast64_t prevtr = -1;
 		int_fast32_2s prevcorr;
+		int_fast64_t atmin = INT_FAST64_MIN;
 		set_leapcount(sp, leapcnt);
 		sp->timecnt = timecnt;
 		sp->typecnt = typecnt;
@@ -1120,14 +1121,15 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
 		for (i = 0; i < sp->timecnt; ++i) {
 			int_fast64_t at
 			  = stored == 4 ? detzcode(p) : detzcode64(p);
+			if (at < atmin)
+			  return EFTYPE;
+			atmin = at;
 			sp->types[i] = at <= TIME_T_MAX;
 			if (sp->types[i]) {
 			  time_t attime
 			    = ((TYPE_SIGNED(time_t) ? at < TIME_T_MIN : at < 0)
 			       ? TIME_T_MIN : at);
-			  if (timecnt && attime <= sp->ats[timecnt - 1]) {
-			    if (attime < sp->ats[timecnt - 1])
-			      return EFTYPE;
+			  if (timecnt && sp->ats[timecnt - 1] == attime) {
 			    sp->types[i - 1] = 0;
 			    timecnt--;
 			  }
-- 
2.53.0

From ada6065955bd9a74743f3881a9771f02a8b4e006 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 24 Oct 2026 22:28:48 -0700
Subject: [PROPOSED 3/3] New MIN_TRANSTION_GAP configurable

* Makefile, NEWS: Mention it.
* localtime.c (MIN_TRANSITION_GAP): Default to 0.
(tzloadbody): Reject TZif files whose transition gaps fall
below MIN_TRANSITION_GAP.

* NEWS: Mention this.
---
 Makefile    |  2 ++
 NEWS        |  5 +++++
 localtime.c | 29 +++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index e51ffe0f..06b758ef 100644
--- a/Makefile
+++ b/Makefile
@@ -323,6 +323,8 @@ LDLIBS=
 #	    It also costs CPU time and memory.
 #  -Dtime_tz=\"T\" to use T as the time_t type, rather than the system time_t
 #	This is intended for internal use only; it mangles external names.
+#  -DMIN_TRANSITION_GAP=N to reject TZif files containing transitions
+#	separated by fewer than N seconds.  See localtime.c.
 #  -DTZ_CHANGE_INTERVAL=N if functions depending on TZ should check
 #	no more often than every N seconds for TZif file changes.
 #	If N is negative (the default), no such checking is done.
diff --git a/NEWS b/NEWS
index 45a8856d..0b0b1ead 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,11 @@ Unreleased, experimental changes
     localtime.c no longer supports 49 leap seconds, as 27 now suffice.
     zic now warns if you go over the new limit.
 
+    A new compile-time flag -DMIN_TRANSITION_GAP=N specifies a minimum
+    transition gap of N seconds when reading TZif files.  It currently
+    defaults to 0, but can be set to 1 if TZif files generated by zic
+    2021b and earlier are no longer of concern.
+
     With 32-bit time_t, localtime.c functions now check for transition
     ordering in TZif data as carefully as they do with 64-bit time_t.
     (Problem reported by Ana Bobillo.)
diff --git a/localtime.c b/localtime.c
index 61013447..710889fe 100644
--- a/localtime.c
+++ b/localtime.c
@@ -443,6 +443,16 @@ static char const *utc = etc_utc + sizeof "Etc/" - 1;
 # define SUPPRESS_TZDIR 0
 #endif
 
+/* Minimum separation in seconds between adjacent transition times,
+   as a nonnegative signed integer.  RFC 9636 says it must be at most 1.
+   Currently it defaults to 0, to accept nonconforming
+   TZif files containing multiple transitions at the same time,
+   such as TZif files generated by bugs in zic 2021b and earlier.
+   It can default to 1 once the old zic versions are no longer of concern.   */
+#ifndef MIN_TRANSITION_GAP
+# define MIN_TRANSITION_GAP 0
+#endif
+
 /* Limit to time zone abbreviation length in proleptic TZ strings.
    This is distinct from TZ_MAX_CHARS, which limits TZif file contents.
    It defaults to 254, not 255, so that desigidx_type can be an unsigned char.
@@ -1123,13 +1133,28 @@ tzloadbody(char const *name, struct state *sp, char tzloadflags,
 			  = stored == 4 ? detzcode(p) : detzcode64(p);
 			if (at < atmin)
 			  return EFTYPE;
-			atmin = at;
+			else {
+#ifdef ckd_add
+			  bool v = ckd_add(&atmin, at, MIN_TRANSITION_GAP);
+#else
+			  bool v = INT_FAST64_MAX - MIN_TRANSITION_GAP < at;
+			  if (!v)
+			    atmin = at + MIN_TRANSITION_GAP;
+#endif
+			  /* If the next minimum value for 'at' overflows,
+			     any new 'at' value is less than what it
+			     should be, so fail if such a value exists.  */
+			  if (v && i + 1 < sp->timecnt)
+			    return EFTYPE;
+			}
+
 			sp->types[i] = at <= TIME_T_MAX;
 			if (sp->types[i]) {
 			  time_t attime
 			    = ((TYPE_SIGNED(time_t) ? at < TIME_T_MIN : at < 0)
 			       ? TIME_T_MIN : at);
-			  if (timecnt && sp->ats[timecnt - 1] == attime) {
+			  if (!MIN_TRANSITION_GAP && timecnt
+			      && sp->ats[timecnt - 1] == attime) {
 			    sp->types[i - 1] = 0;
 			    timecnt--;
 			  }
-- 
2.53.0

Reply via email to