mirror of
https://github.com/golang/sys.git
synced 2026-02-08 19:56:04 +03:00
unix: don't overwrite unrelated file descriptors in TestDup
TestDup used a file descriptor without ensuring it was free, leading to rare crashes in the runtime netpoller when the victim fd was the polling descriptor. Updates golang/go#29423 Change-Id: Idc8b6b47f7e966e045f57f2028e7b6b79e0fb3f3 Reviewed-on: https://go-review.googlesource.com/c/163638 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
@@ -396,14 +396,24 @@ func TestDup(t *testing.T) {
|
||||
t.Fatalf("Dup: %v", err)
|
||||
}
|
||||
|
||||
err = unix.Dup2(newFd, newFd+1)
|
||||
// Create and reserve a file descriptor.
|
||||
// Dup2 automatically closes it before reusing it.
|
||||
nullFile, err := os.Open("/dev/null")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dupFd := int(file.Fd())
|
||||
err = unix.Dup2(newFd, dupFd)
|
||||
if err != nil {
|
||||
t.Fatalf("Dup2: %v", err)
|
||||
}
|
||||
// Keep the dummy file open long enough to not be closed in
|
||||
// its finalizer.
|
||||
runtime.KeepAlive(nullFile)
|
||||
|
||||
b1 := []byte("Test123")
|
||||
b2 := make([]byte, 7)
|
||||
_, err = unix.Write(newFd+1, b1)
|
||||
_, err = unix.Write(dupFd, b1)
|
||||
if err != nil {
|
||||
t.Fatalf("Write to dup2 fd failed: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user