From 193625ce29b887ecd79c4d01fa9c9b7962561848 Mon Sep 17 00:00:00 2001 From: mahailiang Date: Thu, 12 Nov 2026 13:37:57 +0800 Subject: [PATCH] sudoers: fix leap year calculation in getdate() get_date() counts the days between EPOCH and the requested year with Julian += 365 + (i % 4 == 0); which treats every year divisible by 4 as a leap year. Century years that are not divisible by 400 (2100, 2200, 2300, ...) are therefore counted with an extra day. Any date in a later year is then computed too large by one day for every such year that has passed, e.g. "Jan 1, 2101" is returned as Jan 2, and "Jan 1, 2300" is off by two days. Use the full Gregorian rule, as is already done for DaysInMonth[1]. The generated getdate.c contains a copy of the same function, so update it as well. Fixes: https://github.com/sudo-project/sudo/issues/446 Signed-off-by: mahailiang --- plugins/sudoers/getdate.c | 2 +- plugins/sudoers/getdate.y | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/sudoers/getdate.c b/plugins/sudoers/getdate.c index 70bd75ee7d..f47a5818cb 100644 --- a/plugins/sudoers/getdate.c +++ b/plugins/sudoers/getdate.c @@ -2104,7 +2104,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes, for (Julian = Day - 1, i = 0; i < Month; i++) Julian += DaysInMonth[i]; for (i = EPOCH; i < Year; i++) - Julian += 365 + (i % 4 == 0); + Julian += 365 + (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)); Julian *= SECSPERDAY; Julian += yyTimezone * 60L; if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0) diff --git a/plugins/sudoers/getdate.y b/plugins/sudoers/getdate.y index b7737636ce..edf452370c 100644 --- a/plugins/sudoers/getdate.y +++ b/plugins/sudoers/getdate.y @@ -575,7 +575,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes, for (Julian = Day - 1, i = 0; i < Month; i++) Julian += DaysInMonth[i]; for (i = EPOCH; i < Year; i++) - Julian += 365 + (i % 4 == 0); + Julian += 365 + (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)); Julian *= SECSPERDAY; Julian += yyTimezone * 60L; if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)