git: 86f463044f95 - releng/15.2 - tzcode: Avoid integer truncation resulting in wrong weekday

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 00:12:03 UTC
The branch releng/15.2 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=86f463044f951c456016d40dbb0aeb7095aa8a10

commit 86f463044f951c456016d40dbb0aeb7095aa8a10
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2026-11-25 21:39:41 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-27 00:11:51 +0000

    tzcode: Avoid integer truncation resulting in wrong weekday
    
    From a report[1] on the tz mailing list:
    
    In timesub() the weekday is stored in (int) tm_wday before reduction
    modulo seven (DAYSPERWEEK).  Previously this could overflow a 32-bit
    int, resulting in an incorrect weekday calculation.
    
    Calculate two additional intermediate terms modulo seven to avoid an
    intermediate value that does not fit in 32 bits.
    
    We're committing this patch now so we can have something in place for
    FreeBSD 15.2.  If a different change is committed upstream we will
    replace this.
    
    [1] https://lists.iana.org/hyperkitty/list/tz@iana.org/thread/O3RZGBKSO5GXBFDZKW2JXO3OQIIGYBF3/
    
    Approved by:    re (cperciva)
    PR:             300977
    Requested by:   re (cperciva)
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D62073
    
    (cherry picked from commit ecb47941d089b9f9946afa6c52a33418491bfc66)
    (cherry picked from commit da83cb9c21e5fe5cca75fd5398b48b7522a88f0c)
---
 contrib/tzcode/localtime.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/tzcode/localtime.c b/contrib/tzcode/localtime.c
index 397d7e8185c2..9bfbd332f81e 100644
--- a/contrib/tzcode/localtime.c
+++ b/contrib/tzcode/localtime.c
@@ -2419,8 +2419,8 @@ timesub(const time_t *timep, int_fast32_t offset,
 			+ ((y % DAYSPERWEEK - TM_YEAR_BASE % DAYSPERWEEK)
 			   % DAYSPERWEEK
 			   * (DAYSPERNYEAR % DAYSPERWEEK))
-			+ leaps_thru_end_of(y - 1)
-			- leaps_thru_end_of(TM_YEAR_BASE - 1)
+			+ leaps_thru_end_of(y - 1) % DAYSPERWEEK
+			- leaps_thru_end_of(TM_YEAR_BASE - 1) % DAYSPERWEEK
 			+ idays);
 	tmp->tm_wday %= DAYSPERWEEK;
 	if (tmp->tm_wday < 0)