Commit 6293a019 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

nodefs: fix double syscall.Close

syscall.Close is not safe to run twice. Reused fds cause random things
to break.
parent f4f9e240
......@@ -147,22 +147,39 @@ func TestCopyFileRange(t *testing.T) {
if err != nil {
t.Fatalf("Open src: %v", err)
}
defer syscall.Close(f1)
defer func() {
// syscall.Close() is treacherous; because fds are
// reused, a double close can cause serious havoc
if f1 > 0 {
syscall.Close(f1)
}
}()
f2, err := syscall.Open(tc.mntDir+"/dst", syscall.O_RDWR, 0)
if err != nil {
t.Fatalf("Open dst: %v", err)
}
defer syscall.Close(f2)
defer func() {
if f2 > 0 {
defer syscall.Close(f2)
}
}()
srcOff := int64(5)
dstOff := int64(7)
if sz, err := unix.CopyFileRange(f1, &srcOff, f2, &dstOff, 3, 0); err != nil || sz != 3 {
t.Fatalf("CopyFileRange: %d,%v", sz, err)
}
if err := syscall.Close(f1); err != nil {
err = syscall.Close(f1)
f1 = 0
if err != nil {
t.Fatalf("Close src: %v", err)
}
if err := syscall.Close(f2); err != nil {
err = syscall.Close(f2)
f2 = 0
if err != nil {
t.Fatalf("Close dst: %v", err)
}
c, err := ioutil.ReadFile(tc.mntDir + "/dst")
......
......@@ -128,7 +128,7 @@ func TestBasic(t *testing.T) {
}
}
func TestFile(t *testing.T) {
func TestFileBasic(t *testing.T) {
tc := newTestCase(t, true, true)
defer tc.Clean()
......
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