From 72deae88e543b815339a7ba5a3d83a649e90b555 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 24 Jun 2021 12:38:53 +0200 Subject: [PATCH] get last error before calling a method that might fail as well (#54667) --- .../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 7a13a80..40c1e18 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 @@ namespace Microsoft.Win32.SafeHandles 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 @@ namespace Microsoft.Win32.SafeHandles 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) { -- 2.7.4