authorgravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2025-03-02 22:31:02+00:00
committergravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2025-03-02 22:58:45+00:00
log6378295b771b2e621918d20e45debf91ee114eac
treef2eff891f7157ce248187071bb24c0dc3115d72c
parent0367d684fccf8bf011fe8ac1a984820c824871a8

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.

1 files changed, 8 insertions(+), 8 deletions(-)

lib/std/os/uefi.zig+8-8
...@@ -132,12 +132,12 @@ pub const Time = extern struct {...@@ -132,12 +132,12 @@ pub const Time = extern struct {
132 /// Time is to be interpreted as local time132 /// Time is to be interpreted as local time
133 pub const unspecified_timezone: i16 = 0x7ff;133 pub const unspecified_timezone: i16 = 0x7ff;
134134
135 fn daysInYear(year: u16, maxMonth: u4) u32 {135 fn daysInYear(year: u16, max_month: u4) u9 {
136 const leapYear: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;136 const leap_year: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
137 var days: u32 = 0;137 var days: u9 = 0;
138 var month: u4 = 0;138 var month: u4 = 0;
139 while (month < maxMonth) : (month += 1) {139 while (month < max_month) : (month += 1) {
140 days += std.time.epoch.getDaysInMonth(leapYear, @enumFromInt(month + 1));140 days += std.time.epoch.getDaysInMonth(leap_year, @enumFromInt(month + 1));
141 }141 }
142 return days;142 return days;
143 }143 }
...@@ -151,9 +151,9 @@ pub const Time = extern struct {...@@ -151,9 +151,9 @@ pub const Time = extern struct {
151 }151 }
152152
153 days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;153 days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
154 const hours = self.hour + (days * 24);154 const hours: u64 = self.hour + (days * 24);
155 const minutes = self.minute + (hours * 60);155 const minutes: u64 = self.minute + (hours * 60);
156 const seconds = self.second + (minutes * std.time.s_per_min);156 const seconds: u64 = self.second + (minutes * std.time.s_per_min);
157 return self.nanosecond + (seconds * std.time.ns_per_s);157 return self.nanosecond + (seconds * std.time.ns_per_s);
158 }158 }
159};159};