Skip to content

Commit

Permalink
codec: return io.ErrUnexpectedEOF (not io.EOF) when incomplete data read
Browse files Browse the repository at this point in the history
Fixes #398
  • Loading branch information
ugorji committed Nov 28, 2023
1 parent 190086c commit 43b79bf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4276,10 +4276,11 @@ func doTestJsonInvalidUnicode(t *testing.T, h Handle) {
for k, v := range m {
// call testUnmarshal directly, so we can check for EOF
// testUnmarshalErr(&s, []byte(k), jh, t, "-")
// t.Logf("%s >> %s", k, v)
var s string
err = testUnmarshal(&s, []byte(k), jh)
if err != nil {
if err == io.EOF {
if err == io.EOF || err == io.ErrUnexpectedEOF {
continue
}
t.Logf("%s: unmarshal failed: %v", "-", err)
Expand Down
5 changes: 2 additions & 3 deletions codec/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ package codec
//
// ------------------------------------------
// Bounds Checking
// - Allow bytesDecReader to incur "bounds check error", and
// recover that as an io.EOF.
// - Allow bytesDecReader to incur "bounds check error", and recover that as an io error.
// This allows the bounds check branch to always be taken by the branch predictor,
// giving better performance (in theory), while ensuring that the code is shorter.
//
Expand Down Expand Up @@ -2473,7 +2472,7 @@ func panicValToErr(h errDecorator, v interface{}, err *error) {
case runtime.Error:
d, dok := h.(*Decoder)
if dok && d.bytes && isSliceBoundsError(xerr.Error()) {
*err = io.EOF
*err = io.ErrUnexpectedEOF
} else {
h.wrapErr(xerr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion codec/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {

func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
if cls := c.cls.load(); cls.closed {
return io.EOF
return io.ErrUnexpectedEOF
}

// We read the response header by hand
Expand Down
4 changes: 2 additions & 2 deletions codec/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ func (z *ioDecReader) unreadn1() {

// bytesDecReader is a decReader that reads off a byte slice with zero copying
//
// Note: we do not try to convert index'ing out of bounds to an io.EOF.
// Note: we do not try to convert index'ing out of bounds to an io error.
// instead, we let it bubble up to the exported Encode/Decode method
// and recover it as an io.EOF.
// and recover it as an io error.
//
// Every function here MUST defensively check bounds either explicitly
// or via a bounds check.
Expand Down

0 comments on commit 43b79bf

Please sign in to comment.