Skip to content

Commit 6378295

Browse files
committed
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 parent 0367d68 commit 6378295

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/std/os/uefi.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ pub const Time = extern struct {
132132
/// Time is to be interpreted as local time
133133
pub const unspecified_timezone: i16 = 0x7ff;
134134

135-
fn daysInYear(year: u16, maxMonth: u4) u32 {
136-
const leapYear: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
137-
var days: u32 = 0;
135+
fn daysInYear(year: u16, max_month: u4) u9 {
136+
const leap_year: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
137+
var days: u9 = 0;
138138
var month: u4 = 0;
139-
while (month < maxMonth) : (month += 1) {
140-
days += std.time.epoch.getDaysInMonth(leapYear, @enumFromInt(month + 1));
139+
while (month < max_month) : (month += 1) {
140+
days += std.time.epoch.getDaysInMonth(leap_year, @enumFromInt(month + 1));
141141
}
142142
return days;
143143
}
@@ -151,9 +151,9 @@ pub const Time = extern struct {
151151
}
152152

153153
days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
154-
const hours = self.hour + (days * 24);
155-
const minutes = self.minute + (hours * 60);
156-
const seconds = self.second + (minutes * std.time.s_per_min);
154+
const hours: u64 = self.hour + (days * 24);
155+
const minutes: u64 = self.minute + (hours * 60);
156+
const seconds: u64 = self.second + (minutes * std.time.s_per_min);
157157
return self.nanosecond + (seconds * std.time.ns_per_s);
158158
}
159159
};

0 commit comments

Comments
 (0)