From 43b79bfcab412eeb73e92181a2190e97a5520566 Mon Sep 17 00:00:00 2001 From: Ugorji Nwoke Date: Tue, 28 Nov 2023 06:01:21 -0500 Subject: [PATCH] codec: return io.ErrUnexpectedEOF (not io.EOF) when incomplete data read Fixes #398 --- codec/codec_test.go | 3 ++- codec/helper.go | 5 ++--- codec/msgpack.go | 2 +- codec/reader.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/codec/codec_test.go b/codec/codec_test.go index 929e460b..188e9234 100644 --- a/codec/codec_test.go +++ b/codec/codec_test.go @@ -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) diff --git a/codec/helper.go b/codec/helper.go index a371191f..ecd87ba5 100644 --- a/codec/helper.go +++ b/codec/helper.go @@ -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. // @@ -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) } diff --git a/codec/msgpack.go b/codec/msgpack.go index c8b539d4..c0861df5 100644 --- a/codec/msgpack.go +++ b/codec/msgpack.go @@ -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 diff --git a/codec/reader.go b/codec/reader.go index 3fea9f4c..ec5dac0e 100644 --- a/codec/reader.go +++ b/codec/reader.go @@ -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.