]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: document that Read returns io.EOF iff zero bytes are read
authorJoe Tsai <joetsai@digital-static.net>
Wed, 30 Sep 2015 20:35:03 +0000 (13:35 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 30 Sep 2015 22:10:44 +0000 (22:10 +0000)
Also add a unit test to lock this behavior into the API.

Fixes #12016

Change-Id: Ib6ec6e7948f0705f3504ede9143b5dc4e790fc44
Reviewed-on: https://go-review.googlesource.com/15171
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/binary/binary.go
src/encoding/binary/binary_test.go

index 2bbe07c02ff3153886c76f8756ca9c83a3bfbfe0..1c2577b68decdeafe4dacf4c134e03add1ab00c9 100644 (file)
@@ -135,6 +135,10 @@ func (bigEndian) GoString() string { return "binary.BigEndian" }
 // blank (_) field names is skipped; i.e., blank field names
 // may be used for padding.
 // When reading into a struct, all non-blank fields must be exported.
+//
+// The error is EOF only if no bytes were read.
+// If an EOF happens after reading some but not all the bytes,
+// Read returns ErrUnexpectedEOF.
 func Read(r io.Reader, order ByteOrder, data interface{}) error {
        // Fast path for basic types and slices.
        if n := intDataSize(data); n != 0 {
index 8ee595fa476aabb677c87b08a065aec30b90af8c..7fd36fa4efa9bc3b7e886349a428add945d12e9e 100644 (file)
@@ -309,6 +309,36 @@ func TestReadErrorMsg(t *testing.T) {
        read(&p)
 }
 
+func TestReadTruncated(t *testing.T) {
+       const data = "0123456789abcdef"
+
+       var b1 = make([]int32, 4)
+       var b2 struct {
+               A, B, C, D byte
+               E          int32
+               F          float64
+       }
+
+       for i := 0; i <= len(data); i++ {
+               var errWant error
+               switch i {
+               case 0:
+                       errWant = io.EOF
+               case len(data):
+                       errWant = nil
+               default:
+                       errWant = io.ErrUnexpectedEOF
+               }
+
+               if err := Read(strings.NewReader(data[:i]), LittleEndian, &b1); err != errWant {
+                       t.Errorf("Read(%d) with slice: got %v, want %v", i, err, errWant)
+               }
+               if err := Read(strings.NewReader(data[:i]), LittleEndian, &b2); err != errWant {
+                       t.Errorf("Read(%d) with struct: got %v, want %v", i, err, errWant)
+               }
+       }
+}
+
 type byteSliceReader struct {
        remain []byte
 }