From 6378295b771b2e621918d20e45debf91ee114eac Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 2 Mar 2025 22:31:02 +0000 Subject: [PATCH] std.os.uefi: Fix integer overflow in Time.toEpoch() Instead of thinking hard about what the actual supported maximum value for each sub-calculation is we can simply use an u64 from hours onwards. --- lib/std/os/uefi.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/std/os/uefi.zig b/lib/std/os/uefi.zig index f67fe5623e..d7f6da26cf 100644 --- a/lib/std/os/uefi.zig +++ b/lib/std/os/uefi.zig @@ -132,12 +132,12 @@ pub const Time = extern struct { /// Time is to be interpreted as local time pub const unspecified_timezone: i16 = 0x7ff; - fn daysInYear(year: u16, maxMonth: u4) u32 { - const leapYear: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap; - var days: u32 = 0; + fn daysInYear(year: u16, max_month: u4) u9 { + const leap_year: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap; + var days: u9 = 0; var month: u4 = 0; - while (month < maxMonth) : (month += 1) { - days += std.time.epoch.getDaysInMonth(leapYear, @enumFromInt(month + 1)); + while (month < max_month) : (month += 1) { + days += std.time.epoch.getDaysInMonth(leap_year, @enumFromInt(month + 1)); } return days; } @@ -151,9 +151,9 @@ pub const Time = extern struct { } days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day; - const hours = self.hour + (days * 24); - const minutes = self.minute + (hours * 60); - const seconds = self.second + (minutes * std.time.s_per_min); + const hours: u64 = self.hour + (days * 24); + const minutes: u64 = self.minute + (hours * 60); + const seconds: u64 = self.second + (minutes * std.time.s_per_min); return self.nanosecond + (seconds * std.time.ns_per_s); } };