diff options
author | Niklas Therning <niklas@therning.org> | 2017-02-07 11:45:06 +0300 |
---|---|---|
committer | Niklas Therning <niklas@therning.org> | 2017-02-07 11:45:06 +0300 |
commit | 0311475c606775981aa9be487a493903920bfc3c (patch) | |
tree | c1fd89b373ba1a39c38a9cf7b0662c902c3826d5 /mcs/class/Mono.Posix | |
parent | f2f19e950300b9b6b8e4306836beb4c619db88e7 (diff) |
Make sure Stdlib.GetLastError() never clobbers Marshal.GetLastWin32Error()
Diffstat (limited to 'mcs/class/Mono.Posix')
-rw-r--r-- | mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs b/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs index 9b95fba6f3c..dbabb29c7f1 100644 --- a/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs +++ b/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs @@ -426,14 +426,20 @@ namespace Mono.Unix.Native { public static Errno GetLastError () { + // Always call Marshal.GetLastWin32Error() before the OS check, + // even on Windows where we don't use the return value. If we do + // the OS check first Environment.OSVersion (if it happens to be + // the first ever access) will clobber Marshal.GetLastWin32Error() + // and we won't get the desired errno value on non-Windows platforms. + int errno = Marshal.GetLastWin32Error (); if (Environment.OSVersion.Platform != PlatformID.Unix) { // On Windows Marshal.GetLastWin32Error() doesn't take errno // into account so we need to call Mono_Posix_Stdlib_GetLastError() // which returns the value of errno in the C runtime // libMonoPosixHelper.dll was linked against. - return (Errno) _GetLastError (); + errno = _GetLastError (); } - return NativeConvert.ToErrno (Marshal.GetLastWin32Error ()); + return NativeConvert.ToErrno (errno); } [DllImport (MPH, CallingConvention=CallingConvention.Cdecl, |