Skip to content

Commit 628365d

Browse files
committed
windows: support nil done parameter in ReadFile and WriteFile
Win32 defines the `done` param as optional for ReadFile and WriteFile functions. We should support this case too. Fixes golang/go#65365. Change-Id: I961ff66a63d3a8ffa5560b6dab21fbd4ac9817ae Reviewed-on: https://go-review.googlesource.com/c/sys/+/559375 Reviewed-by: Mauri de Souza Meneguzzo <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bef1bd8 commit 628365d

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

windows/syscall_windows.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -572,23 +572,31 @@ func Write(fd Handle, p []byte) (n int, err error) {
572572
}
573573

574574
func ReadFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
575-
err := readFile(fd, p, done, overlapped)
575+
var n uint32
576+
err := readFile(fd, p, &n, overlapped)
576577
if raceenabled {
577-
if *done > 0 {
578-
raceWriteRange(unsafe.Pointer(&p[0]), int(*done))
578+
if n > 0 {
579+
raceWriteRange(unsafe.Pointer(&p[0]), int(n))
579580
}
580581
raceAcquire(unsafe.Pointer(&ioSync))
581582
}
583+
if done != nil {
584+
*done = n
585+
}
582586
return err
583587
}
584588

585589
func WriteFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
586590
if raceenabled {
587591
raceReleaseMerge(unsafe.Pointer(&ioSync))
588592
}
589-
err := writeFile(fd, p, done, overlapped)
590-
if raceenabled && *done > 0 {
591-
raceReadRange(unsafe.Pointer(&p[0]), int(*done))
593+
var n uint32
594+
err := writeFile(fd, p, &n, overlapped)
595+
if raceenabled && n > 0 {
596+
raceReadRange(unsafe.Pointer(&p[0]), int(n))
597+
}
598+
if done != nil {
599+
*done = n
592600
}
593601
return err
594602
}

windows/syscall_windows_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -1275,3 +1275,58 @@ uintptr_t beep(void) {
12751275
t.Fatal("LoadLibraryEx unexpectedly found beep.dll")
12761276
}
12771277
}
1278+
1279+
func TestReadWriteFileOverlapped(t *testing.T) {
1280+
name := filepath.Join(t.TempDir(), "test.txt")
1281+
fd, err := windows.CreateFile(windows.StringToUTF16Ptr(name), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.CREATE_NEW, windows.FILE_FLAG_OVERLAPPED, 0)
1282+
if err != nil {
1283+
t.Fatal(err)
1284+
}
1285+
defer windows.CloseHandle(fd)
1286+
1287+
content := []byte("hello")
1288+
// Test that we can write to a file using overlapped I/O.
1289+
var ow windows.Overlapped
1290+
ow.HEvent, err = windows.CreateEvent(nil, 0, 0, nil)
1291+
if err != nil {
1292+
t.Fatal(err)
1293+
}
1294+
defer windows.CloseHandle(ow.HEvent)
1295+
if err := windows.WriteFile(fd, content, nil, &ow); err != nil && err != windows.ERROR_IO_PENDING {
1296+
t.Fatal(err)
1297+
}
1298+
if _, err := windows.WaitForSingleObject(ow.HEvent, windows.INFINITE); err != nil {
1299+
t.Fatal(err)
1300+
}
1301+
var n uint32
1302+
if err := windows.GetOverlappedResult(fd, &ow, &n, true); err != nil {
1303+
t.Fatal(err)
1304+
}
1305+
if n != uint32(len(content)) {
1306+
t.Fatalf("got %d bytes written; want %d", n, len(content))
1307+
}
1308+
1309+
// Test that we can read from a file using overlapped I/O.
1310+
var or windows.Overlapped
1311+
or.HEvent, err = windows.CreateEvent(nil, 0, 0, nil)
1312+
if err != nil {
1313+
t.Fatal(err)
1314+
}
1315+
defer windows.CloseHandle(ow.HEvent)
1316+
buf := make([]byte, len(content))
1317+
if err := windows.ReadFile(fd, buf, nil, &or); err != nil && err != windows.ERROR_IO_PENDING {
1318+
t.Fatal(err)
1319+
}
1320+
if err != nil {
1321+
t.Fatal(err)
1322+
}
1323+
if _, err := windows.WaitForSingleObject(or.HEvent, windows.INFINITE); err != nil {
1324+
t.Fatal(err)
1325+
}
1326+
if err := windows.GetOverlappedResult(fd, &or, &n, true); err != nil {
1327+
t.Fatal(err)
1328+
}
1329+
if string(buf) != string(content) {
1330+
t.Fatalf("got %q; want %q", buf, content)
1331+
}
1332+
}

0 commit comments

Comments
 (0)