Repository navigation
Commit 92f8048
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
| 147 | + | |
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
| 155 | + | |
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | | - | |
| 330 | + | |
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
| |||
368 | 368 | | |
369 | 369 | | |
370 | 370 | | |
371 | | - | |
| 371 | + | |
372 | 372 | | |
373 | 373 | | |
374 | 374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
716 | 716 | | |
717 | 717 | | |
718 | 718 | | |
| 719 | + | |
719 | 720 | | |
720 | 721 | | |
721 | 722 | | |
| 723 | + | |
722 | 724 | | |
723 | 725 | | |
724 | 726 | | |
| |||
0 commit comments