Skip to content

Commit 92f8048

Browse files
committed
Prevent read methods from succeeding on partial reads
Fixes the following tests for Go: - [EofExceptionU4](https://github.com/kaitai-io/kaitai_struct_tests/blob/3c5fb218ba78fb040243b5a35cea69fb974118cd/formats/eof_exception_u4.ksy) - [EosExceptionU4](https://github.com/kaitai-io/kaitai_struct_tests/blob/3c5fb218ba78fb040243b5a35cea69fb974118cd/formats/eos_exception_u4.ksy) The failures of the above tests in CI have been indicating for a while that the read primitives in the Go runtime library succeed even if there are not enough bytes available in the stream. More specifically, they only fail if we are right at the end of file and there are 0 bytes available, but they apparently never return an error if there is at least 1 byte remaining regardless of how many bytes the particular read primitive actually needs. The problem is using the `io.Reader.Read` function, see https://pkg.go.dev/io#Reader (**emphasis** mine): > Read reads **up to** len(p) bytes into p. (...) If some data is > available but not len(p) bytes, Read conventionally **returns what is > available instead of waiting for more**. > When Read encounters an error or end-of-file condition after > successfully reading n > 0 bytes, it returns the number of bytes read. > It may return the (non-nil) error from the same call or return the > error (and n == 0) from a subsequent call. An instance of this general > case is that a Reader returning a non-zero number of bytes at the end > of the input stream may return either err == EOF **or err == nil**. > The next Read should return 0, EOF. So this function is specifically allowed to read fewer bytes than requested and this situation is not guaranteed to be indicated by an error. This is exactly what we saw in the tests. That's why we're now using [`io.ReadFull`](https://pkg.go.dev/io#ReadFull) instead: > ReadFull reads **exactly len(buf) bytes** from r into buf. It returns the > number of bytes copied and an error if fewer bytes were read. The > error is EOF only if no bytes were read. If an EOF happens after > reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF. > On return, **n == len(buf) if and only if err == nil**. The distinction between EOF and ErrUnexpectedEOF is a bit weird, we generally have only one kind of EOF error in other languages, but let's not worry about that now. At least it fixes the primary bug.
1 parent 7d8bdef commit 92f8048

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎kaitai/stream.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,55 +104,55 @@ func (k *Stream) Pos() (int64, error) {
104104

105105
// ReadU1 reads 1 byte and returns this as uint8.
106106
func (k *Stream) ReadU1() (v uint8, err error) {
107-
if _, err = k.Read(k.buf[:1]); err != nil {
107+
if _, err = io.ReadFull(k, k.buf[:1]); err != nil {
108108
return 0, fmt.Errorf("ReadU1: error reading 1 byte: %w", err)
109109
}
110110
return k.buf[0], nil
111111
}
112112

113113
// ReadU2be reads 2 bytes in big-endian order and returns those as uint16.
114114
func (k *Stream) ReadU2be() (v uint16, err error) {
115-
if _, err = k.Read(k.buf[:2]); err != nil {
115+
if _, err = io.ReadFull(k, k.buf[:2]); err != nil {
116116
return 0, fmt.Errorf("ReadU2be: error reading 2 bytes: %w", err)
117117
}
118118
return binary.BigEndian.Uint16(k.buf[:2]), nil
119119
}
120120

121121
// ReadU4be reads 4 bytes in big-endian order and returns those as uint32.
122122
func (k *Stream) ReadU4be() (v uint32, err error) {
123-
if _, err = k.Read(k.buf[:4]); err != nil {
123+
if _, err = io.ReadFull(k, k.buf[:4]); err != nil {
124124
return 0, fmt.Errorf("ReadU4be: error reading 4 bytes: %w", err)
125125
}
126126
return binary.BigEndian.Uint32(k.buf[:4]), nil
127127
}
128128

129129
// ReadU8be reads 8 bytes in big-endian order and returns those as uint64.
130130
func (k *Stream) ReadU8be() (v uint64, err error) {
131-
if _, err = k.Read(k.buf[:8]); err != nil {
131+
if _, err = io.ReadFull(k, k.buf[:8]); err != nil {
132132
return 0, fmt.Errorf("ReadU8be: error reading 8 bytes: %w", err)
133133
}
134134
return binary.BigEndian.Uint64(k.buf[:8]), nil
135135
}
136136

137137
// ReadU2le reads 2 bytes in little-endian order and returns those as uint16.
138138
func (k *Stream) ReadU2le() (v uint16, err error) {
139-
if _, err = k.Read(k.buf[:2]); err != nil {
139+
if _, err = io.ReadFull(k, k.buf[:2]); err != nil {
140140
return 0, fmt.Errorf("ReadU2le: error reading 2 bytes: %w", err)
141141
}
142142
return binary.LittleEndian.Uint16(k.buf[:2]), nil
143143
}
144144

145145
// ReadU4le reads 4 bytes in little-endian order and returns those as uint32.
146146
func (k *Stream) ReadU4le() (v uint32, err error) {
147-
if _, err = k.Read(k.buf[:4]); err != nil {
147+
if _, err = io.ReadFull(k, k.buf[:4]); err != nil {
148148
return 0, fmt.Errorf("ReadU4le: error reading 4 bytes: %w", err)
149149
}
150150
return binary.LittleEndian.Uint32(k.buf[:4]), nil
151151
}
152152

153153
// ReadU8le reads 8 bytes in little-endian order and returns those as uint64.
154154
func (k *Stream) ReadU8le() (v uint64, err error) {
155-
if _, err = k.Read(k.buf[:8]); err != nil {
155+
if _, err = io.ReadFull(k, k.buf[:8]); err != nil {
156156
return 0, fmt.Errorf("ReadU8le: error reading 8 bytes: %w", err)
157157
}
158158
return binary.LittleEndian.Uint64(k.buf[:8]), nil
@@ -327,7 +327,7 @@ func (k *Stream) ReadBitsIntBe(n int) (res uint64, err error) {
327327
if bytesNeeded > 8 {
328328
return res, fmt.Errorf("ReadBitsIntBe(%d): more than 8 bytes requested: %w", n, ErrInvalidSizeRequested)
329329
}
330-
_, err = k.Read(k.buf[:bytesNeeded])
330+
_, err = io.ReadFull(k, k.buf[:bytesNeeded])
331331
if err != nil {
332332
return res, fmt.Errorf("ReadBitsIntBe(%d): %w", n, err)
333333
}
@@ -368,7 +368,7 @@ func (k *Stream) ReadBitsIntLe(n int) (res uint64, err error) {
368368
if bytesNeeded > 8 {
369369
return res, fmt.Errorf("ReadBitsIntLe(%d): more than 8 bytes requested: %w", n, ErrInvalidSizeRequested)
370370
}
371-
_, err = k.Read(k.buf[:bytesNeeded])
371+
_, err = io.ReadFull(k, k.buf[:bytesNeeded])
372372
if err != nil {
373373
return res, fmt.Errorf("ReadBitsIntLe(%d): %w", n, err)
374374
}

‎kaitai/stream_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestStream_ReadU2be(t *testing.T) {
128128
wantV uint16
129129
wantErr bool
130130
}{
131-
{"Read", NewStream(bytes.NewReader([]byte{1})), 256, false},
131+
{"Read", NewStream(bytes.NewReader([]byte{1, 0})), 256, false},
132132
{"empty Read", NewStream(bytes.NewReader([]byte(""))), 0, true},
133133
}
134134
for _, tt := range tests {
@@ -716,9 +716,11 @@ func TestStream_ReadBitsInt(t *testing.T) {
716716
{"ReadBitsIntBe", NewStream(bytes.NewReader([]byte{0xF0})), args{5}, 0x1E, false},
717717
{"ReadBitsIntBe", NewStream(bytes.NewReader([]byte{0x12, 0x34, 0x56, 0xFF})), args{24}, 0x123456, false},
718718
{"ReadBitsIntBe", NewStream(bytes.NewReader([]byte{0xAB, 0xC7})), args{12}, 0xABC, false},
719+
{"ReadBitsIntBe", NewStream(bytes.NewReader([]byte{1, 2})), args{17}, 0, true},
719720
{"ReadBitsIntLe", NewStream(bytes.NewReader([]byte{0xF0})), args{5}, 16, false},
720721
{"ReadBitsIntLe", NewStream(bytes.NewReader([]byte{0x56, 0x34, 0x12, 0xFF})), args{24}, 0x123456, false},
721722
{"ReadBitsIntLe", NewStream(bytes.NewReader([]byte{0xBC, 0x7A})), args{12}, 0xABC, false},
723+
{"ReadBitsIntLe", NewStream(bytes.NewReader([]byte{1, 2})), args{17}, 0, true},
722724
}
723725
for _, tt := range tests {
724726
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)