From a0832a1b82376b4fa42cfdd20f940a1e85a0e336 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 24 Jun 2021 08:21:37 +0200 Subject: [PATCH] get last error before calling a method that might fail as well --- .../src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs b/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs index 7a13a80610d04..40c1e1866688c 100644 --- a/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs @@ -44,8 +44,8 @@ private static SafeFileHandle Open(string path, Interop.Sys.OpenFlags flags, int if (handle.IsInvalid) { - handle.Dispose(); Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo(); + handle.Dispose(); // If we fail to open the file due to a path not existing, we need to know whether to blame // the file itself or its directory. If we're creating the file, then we blame the directory, @@ -70,8 +70,9 @@ private static SafeFileHandle Open(string path, Interop.Sys.OpenFlags flags, int Interop.Sys.FileStatus status; if (Interop.Sys.FStat(handle, out status) != 0) { + Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo(); handle.Dispose(); - throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo(), path); + throw Interop.GetExceptionForIoErrno(error, path); } if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR) {