Skip to content

Commit b6f1ba8

Browse files
committed
Ensure last Win32 error is preserved for IO operations (case 1047186)
1 parent bee08b6 commit b6f1ba8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

mono/metadata/w32file-win32.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <windows.h>
1313
#include "mono/metadata/w32file-win32-internals.h"
1414
#include "mono/metadata/profiler-private.h"
15+
#include "mono/metadata/w32error.h"
1516

1617
void
1718
mono_w32file_init (void)
@@ -97,12 +98,17 @@ mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *
9798
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
9899
if (!interrupted)
99100
{
101+
guint32 last_error;
100102
MONO_ENTER_GC_SAFE;
101103
res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
102104
MONO_PROFILER_RAISE (fileio, (1, *bytesread));
103105
MONO_EXIT_GC_SAFE;
104106

107+
/* need to save and restore since clients expect error code set for
108+
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
109+
last_error = mono_w32error_get_last ();
105110
mono_thread_info_uninstall_interrupt (&interrupted);
111+
mono_w32error_set_last (last_error);
106112
}
107113

108114
return res;
@@ -117,12 +123,17 @@ mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, gui
117123
mono_thread_info_install_interrupt (cancel_w32_io, handle, &interrupted);
118124
if (!interrupted)
119125
{
126+
guint32 last_error;
120127
MONO_ENTER_GC_SAFE;
121128
res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
122129
MONO_PROFILER_RAISE (fileio, (0, *byteswritten));
123130
MONO_EXIT_GC_SAFE;
124131

132+
/* need to save and restore since clients expect error code set for
133+
* failed IO calls and mono_thread_info_uninstall_interrupt overwrites value */
134+
last_error = mono_w32error_get_last ();
125135
mono_thread_info_uninstall_interrupt (&interrupted);
136+
mono_w32error_set_last (last_error);
126137
}
127138

128139
return res;

0 commit comments

Comments
 (0)