Skip to content

Commit 72deae8

Browse files
authored
get last error before calling a method that might fail as well (#54667)
1 parent 7833828 commit 72deae8

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ private static SafeFileHandle Open(string path, Interop.Sys.OpenFlags flags, int
4444

4545
if (handle.IsInvalid)
4646
{
47-
handle.Dispose();
4847
Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
48+
handle.Dispose();
4949

5050
// If we fail to open the file due to a path not existing, we need to know whether to blame
5151
// 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
7070
Interop.Sys.FileStatus status;
7171
if (Interop.Sys.FStat(handle, out status) != 0)
7272
{
73+
Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
7374
handle.Dispose();
74-
throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo(), path);
75+
throw Interop.GetExceptionForIoErrno(error, path);
7576
}
7677
if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
7778
{

0 commit comments

Comments
 (0)