Skip to content

Commit

Permalink
net: don't return io.EOF on reading data from datagram, raw sockets o…
Browse files Browse the repository at this point in the history
…n windows

Preventing returning io.EOF on non-connection oriented sockets is
already applied to Unix variants. This CL applies it to Windows.

Update #4856.

Change-Id: I82071d40f617e2962d0540b9d1d6a10ea4cdb2ec
Reviewed-on: https://go-review.googlesource.com/2203
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
  • Loading branch information
cixtor committed Jan 1, 2015
1 parent a456801 commit a877e81
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
21 changes: 21 additions & 0 deletions src/net/fd_posix.go
@@ -0,0 +1,21 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows

package net

import (
"io"
"syscall"
)

// eofError returns io.EOF when fd is available for reading end of
// file.
func (fd *netFD) eofError(n int, err error) error {
if n == 0 && err == nil && fd.sotype != syscall.SOCK_DGRAM && fd.sotype != syscall.SOCK_RAW {
return io.EOF
}
return err
}
13 changes: 6 additions & 7 deletions src/net/fd_unix_test.go → src/net/fd_posix_test.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows

package net

Expand All @@ -12,13 +12,12 @@ import (
"testing"
)

var chkReadErrTests = []struct {
var eofErrorTests = []struct {
n int
err error
fd *netFD
expected error
}{

{100, nil, &netFD{sotype: syscall.SOCK_STREAM}, nil},
{100, io.EOF, &netFD{sotype: syscall.SOCK_STREAM}, io.EOF},
{100, errClosing, &netFD{sotype: syscall.SOCK_STREAM}, errClosing},
Expand Down Expand Up @@ -48,11 +47,11 @@ var chkReadErrTests = []struct {
{0, errClosing, &netFD{sotype: syscall.SOCK_RAW}, errClosing},
}

func TestChkReadErr(t *testing.T) {
for _, tt := range chkReadErrTests {
actual := chkReadErr(tt.n, tt.err, tt.fd)
func TestEOFError(t *testing.T) {
for _, tt := range eofErrorTests {
actual := tt.fd.eofError(tt.n, tt.err)
if actual != tt.expected {
t.Errorf("chkReadError(%v, %v, %v): expected %v, actual %v", tt.n, tt.err, tt.fd.sotype, tt.expected, actual)
t.Errorf("eofError(%v, %v, %v): expected %v, actual %v", tt.n, tt.err, tt.fd.sotype, tt.expected, actual)
}
}
}
13 changes: 3 additions & 10 deletions src/net/fd_unix.go
Expand Up @@ -244,7 +244,7 @@ func (fd *netFD) Read(p []byte) (n int, err error) {
}
}
}
err = chkReadErr(n, err, fd)
err = fd.eofError(n, err)
break
}
if err != nil && err != io.EOF {
Expand All @@ -271,7 +271,7 @@ func (fd *netFD) readFrom(p []byte) (n int, sa syscall.Sockaddr, err error) {
}
}
}
err = chkReadErr(n, err, fd)
err = fd.eofError(n, err)
break
}
if err != nil && err != io.EOF {
Expand All @@ -298,7 +298,7 @@ func (fd *netFD) readMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S
}
}
}
err = chkReadErr(n, err, fd)
err = fd.eofError(n, err)
break
}
if err != nil && err != io.EOF {
Expand All @@ -307,13 +307,6 @@ func (fd *netFD) readMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S
return
}

func chkReadErr(n int, err error, fd *netFD) error {
if n == 0 && err == nil && fd.sotype != syscall.SOCK_DGRAM && fd.sotype != syscall.SOCK_RAW {
return io.EOF
}
return err
}

func (fd *netFD) Write(p []byte) (nn int, err error) {
if err := fd.writeLock(); err != nil {
return 0, err
Expand Down
6 changes: 2 additions & 4 deletions src/net/fd_windows.go
Expand Up @@ -6,7 +6,6 @@ package net

import (
"errors"
"io"
"os"
"runtime"
"sync"
Expand Down Expand Up @@ -468,12 +467,10 @@ func (fd *netFD) Read(buf []byte) (int, error) {
n, err := rsrv.ExecIO(o, "WSARecv", func(o *operation) error {
return syscall.WSARecv(o.fd.sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil)
})
if err == nil && n == 0 {
err = io.EOF
}
if raceenabled {
raceAcquire(unsafe.Pointer(&ioSync))
}
err = fd.eofError(n, err)
return n, err
}

Expand All @@ -494,6 +491,7 @@ func (fd *netFD) readFrom(buf []byte) (n int, sa syscall.Sockaddr, err error) {
o.rsan = int32(unsafe.Sizeof(*o.rsa))
return syscall.WSARecvFrom(o.fd.sysfd, &o.buf, 1, &o.qty, &o.flags, o.rsa, &o.rsan, &o.o, nil)
})
err = fd.eofError(n, err)
if err != nil {
return 0, nil, err
}
Expand Down

0 comments on commit a877e81

Please sign in to comment.