From: Ana Bobillo <[email protected]>
When time_t is narrower than 64 bits, tzloadbody checked transition
order only after discarding transitions after TIME_T_MAX and moving
earlier ones up to TIME_T_MIN. Given out-of-order times, it could
then clear the flag of an already-discarded transition instead of
that of the last kept one, so that sp->timecnt counted entries of
sp->ats that were never set. Check order on the times as read, as
is already done for leap seconds.
* localtime.c (tzloadbody): Reject out-of-order transition times
before discarding or adjusting them. The later check for adjusted
times out of order is now unnecessary, so remove it.
* NEWS: Mention this.
---
NEWS | 5 +++++
localtime.c | 13 +++++++++----
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/NEWS b/NEWS
index 1a1b0dc0..ea063caa 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.
+ localtime-related functions now reject TZif files whose transition
+ times are out of order, even when time_t is unsigned or narrower
+ than 64 bits. Formerly, with 32-bit time_t such a file could cause
+ these functions to use uninitialized transition times.
+
Changes to documentation and commentary
Documentation and commentary now reflect leap seconds’ demise.
diff --git a/localtime.c b/localtime.c
index 29a70eb4..a3ee5d85 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1106,6 +1106,7 @@ tzloadbody(char const *name, struct state *sp, char
tzloadflags,
&& (ttisutcnt == typecnt || ttisutcnt == 0)))
return EFTYPE;
else {
+ int_fast64_t prevat = INT_FAST64_MIN;
int_fast64_t prevtr = -1;
int_fast32_2s prevcorr;
set_leapcount(sp, leapcnt);
@@ -1115,19 +1116,23 @@ tzloadbody(char const *name, struct state *sp, char
tzloadflags,
/* Read transitions, discarding those out of time_t range.
But pretend the last transition before TIME_T_MIN
- occurred at TIME_T_MIN. */
+ occurred at TIME_T_MIN. Reject transitions out of order,
+ checking before discarding or adjusting so that discarded
+ transitions all follow kept ones, and so that a kept
+ transition adjusted to its predecessor's time replaces it.
*/
timecnt = 0;
for (i = 0; i < sp->timecnt; ++i) {
int_fast64_t at
= stored == 4 ? detzcode(p) : detzcode64(p);
+ if (at < prevat)
+ return EFTYPE;
+ prevat = 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 && attime == sp->ats[timecnt - 1]) {
sp->types[i - 1] = 0;
timecnt--;
}