Commit cc5685c2 authored by Elias Naur's avatar Elias Naur

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/163638Reviewed-by: 's avatarTobias Klauser <tobias.klauser@gmail.com>
parent cd391775
......@@ -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)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment