In os_windows.cpp, the handling of GetLastError() and errno has some issues. errno is not GetLastError(). GetLastError() returns a 32bit value, a system error code [1]. errno is the C standard errno.
1) at some points we assign GetLastError() to errno. This is wrong since the calling code won't (or should not) expect Windows System codes in errno:
int fd = ::_wopen(wide_path, oflag | O_BINARY | O_NOINHERIT, mode);
os::free(wide_path);
if (fd == -1) {
errno = ::GetLastError();
}
Note that in other places we translate the system error code into errno values (e.g. manually setting errno=EACCESS). This is the proper way to do it.
2) Then, we have the windows-specific os::get_last_error(), which is a weird beast since it returns either GetLastErorr() or errno, depending on what is set. This is quite arbitrary. Both errno and GetLastError() are set by different APIs, so conceivably both are set from different earlier API calls. Then, the returned value is either errno or GetLastError? How is the caller to use this API? Also, there is no guarantee that value ranges don't overlap (they probably don't but that is UB).
[1] https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
1) at some points we assign GetLastError() to errno. This is wrong since the calling code won't (or should not) expect Windows System codes in errno:
int fd = ::_wopen(wide_path, oflag | O_BINARY | O_NOINHERIT, mode);
os::free(wide_path);
if (fd == -1) {
errno = ::GetLastError();
}
Note that in other places we translate the system error code into errno values (e.g. manually setting errno=EACCESS). This is the proper way to do it.
2) Then, we have the windows-specific os::get_last_error(), which is a weird beast since it returns either GetLastErorr() or errno, depending on what is set. This is quite arbitrary. Both errno and GetLastError() are set by different APIs, so conceivably both are set from different earlier API calls. Then, the returned value is either errno or GetLastError? How is the caller to use this API? Also, there is no guarantee that value ranges don't overlap (they probably don't but that is UB).
[1] https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-