Skip to content

Commit 89942eb

Browse files
Better implementation of GetLastError. (#20623)
Instead of calling the dynamically loaded kernel32.GetLastError, we can extract it from the TEB. As shown by [Wine](https://github.com/wine-mirror/wine/blob/34b1606019982b71818780bc84b76460f650af31/include/winternl.h#L439), the last error lives at offset 0x34 of the TEB in 32-bit Windows and at offset 0x68 in 64-bit Windows.
1 parent cf36d3f commit 89942eb

File tree

9 files changed

+56
-47
lines changed

9 files changed

+56
-47
lines changed

lib/std/Thread.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ const WindowsThreadImpl = struct {
554554
0,
555555
null,
556556
) orelse {
557-
const errno = windows.kernel32.GetLastError();
557+
const errno = windows.GetLastError();
558558
return windows.unexpectedError(errno);
559559
};
560560

lib/std/Thread/Condition.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const WindowsImpl = struct {
178178

179179
// Return error.Timeout if we know the timeout elapsed correctly.
180180
if (rc == os.windows.FALSE) {
181-
assert(os.windows.kernel32.GetLastError() == .TIMEOUT);
181+
assert(os.windows.GetLastError() == .TIMEOUT);
182182
if (!timeout_overflowed) return error.Timeout;
183183
}
184184
}

lib/std/crypto/Certificate/Bundle.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn rescanWindows(cb: *Bundle, gpa: Allocator) RescanWindowsError!void {
132132
cb.map.clearRetainingCapacity();
133133

134134
const w = std.os.windows;
135-
const GetLastError = w.kernel32.GetLastError;
135+
const GetLastError = w.GetLastError;
136136
const root = [4:0]u16{ 'R', 'O', 'O', 'T' };
137137
const store = w.crypt32.CertOpenSystemStoreW(null, &root) orelse switch (GetLastError()) {
138138
.FILE_NOT_FOUND => return error.FileNotFound,

lib/std/debug.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ pub const DebugInfo = struct {
17811781

17821782
const handle = windows.kernel32.CreateToolhelp32Snapshot(windows.TH32CS_SNAPMODULE | windows.TH32CS_SNAPMODULE32, 0);
17831783
if (handle == windows.INVALID_HANDLE_VALUE) {
1784-
switch (windows.kernel32.GetLastError()) {
1784+
switch (windows.GetLastError()) {
17851785
else => |err| return windows.unexpectedError(err),
17861786
}
17871787
}

lib/std/io.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ pub fn Poller(comptime StreamEnum: type) type {
567567
windows.INFINITE,
568568
);
569569
if (status == windows.WAIT_FAILED)
570-
return windows.unexpectedError(windows.kernel32.GetLastError());
570+
return windows.unexpectedError(windows.GetLastError());
571571
if (status == windows.WAIT_TIMEOUT)
572572
return true;
573573

@@ -584,7 +584,7 @@ pub fn Poller(comptime StreamEnum: type) type {
584584
&self.windows.overlapped[stream_idx],
585585
&read_bytes,
586586
0,
587-
)) switch (windows.kernel32.GetLastError()) {
587+
)) switch (windows.GetLastError()) {
588588
.BROKEN_PIPE => {
589589
self.windows.active.removeAt(active_idx);
590590
continue;
@@ -664,7 +664,7 @@ fn windowsAsyncRead(
664664
const buf = try fifo.writableWithSize(bump_amt);
665665
var read_bytes: u32 = undefined;
666666
const read_result = windows.kernel32.ReadFile(handle, buf.ptr, math.cast(u32, buf.len) orelse math.maxInt(u32), &read_bytes, overlapped);
667-
if (read_result == 0) return switch (windows.kernel32.GetLastError()) {
667+
if (read_result == 0) return switch (windows.GetLastError()) {
668668
.IO_PENDING => .pending,
669669
.BROKEN_PIPE => .closed,
670670
else => |err| windows.unexpectedError(err),

lib/std/os.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn accessW(path: [*:0]const u16) windows.GetFileAttributesError!void {
6161
if (ret != windows.INVALID_FILE_ATTRIBUTES) {
6262
return;
6363
}
64-
switch (windows.kernel32.GetLastError()) {
64+
switch (windows.GetLastError()) {
6565
.FILE_NOT_FOUND => return error.FileNotFound,
6666
.PATH_NOT_FOUND => return error.FileNotFound,
6767
.ACCESS_DENIED => return error.PermissionDenied,

0 commit comments

Comments
 (0)