Skip to content

windows: avoid runtime crash in fromSysTime #10720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1760,14 +1760,16 @@ pub fn peb() *PEB {
return teb().ProcessEnvironmentBlock;
}

/// A file time is a 64-bit value that represents the number of 100-nanosecond
/// intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated
/// Universal Time (UTC).
/// A file time (or "SysTime" as this function is named) is a 64-bit value that
/// represents the number of 100-nanosecond intervals that have elapsed since
/// 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
/// This function returns the number of nanoseconds since the canonical epoch,
/// which is the POSIX one (Jan 01, 1970 AD).
pub fn fromSysTime(hns: i64) i128 {
const adjusted_epoch: i128 = hns + std.time.epoch.windows * (std.time.ns_per_s / 100);
return adjusted_epoch * 100;
// Note that we can't assign the expression `hns + std.time.epoch.windows * (std.time.ns_per_s / 100)`
// into a temporary variable because it causes a segfault on Windows.
// See https://github.com/ziglang/zig/issues/10719
return @intCast(i128, hns + std.time.epoch.windows * (std.time.ns_per_s / 100)) * 100;
}

pub fn toSysTime(ns: i128) i64 {
Expand Down