From 2a41777072f48467bef02bb3bd670d95c2b02102 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 14 Feb 2023 18:55:57 -0700 Subject: compat/json: Handle io.EOF and io.ErrUnexpectedEOF the same --- ReleaseNotes.md | 3 +++ compat/json/compat.go | 25 +++++++++++----------- compat/json/compat_test.go | 2 ++ .../json/testdata/fuzz/FuzzEquiv/930f49fab2367014 | 2 ++ .../json/testdata/fuzz/FuzzEquiv/caf81e9797b19c76 | 2 ++ 5 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 compat/json/testdata/fuzz/FuzzEquiv/930f49fab2367014 create mode 100644 compat/json/testdata/fuzz/FuzzEquiv/caf81e9797b19c76 diff --git a/ReleaseNotes.md b/ReleaseNotes.md index e047f7d..b7a8f76 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -25,6 +25,9 @@ show the raw byte as a `\u00XX` Unicode codepoint, but now as a `\xXX` byte. + - Bugfix: compat/json: `io.EOF` is now correctly converted to + "unexpected end of JSON input", same as `io.ErrUnexpectedEOF`. + # v0.3.7 (2023-02-20) Theme: Fixes from fuzzing (part 1?) diff --git a/compat/json/compat.go b/compat/json/compat.go index 6f13fbb..4dc15ab 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -53,19 +53,18 @@ func convertError(err error, isUnmarshal bool) error { case *lowmemjson.DecodeReadError: return err case *lowmemjson.DecodeSyntaxError: - switch { - case errors.Is(err, io.EOF): - return io.EOF - case errors.Is(err, io.ErrUnexpectedEOF) && isUnmarshal: - return &SyntaxError{ - msg: "unexpected end of JSON input", - Offset: suberr.Offset, - } - default: - return &SyntaxError{ - msg: suberr.Err.Error(), - Offset: suberr.Offset + 1, + if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { + if isUnmarshal { + return &SyntaxError{ + msg: "unexpected end of JSON input", + Offset: suberr.Offset, + } } + return suberr.Err + } + return &SyntaxError{ + msg: suberr.Err.Error(), + Offset: suberr.Offset + 1, } case *lowmemjson.DecodeTypeError: switch subsuberr := suberr.Err.(type) { @@ -120,7 +119,7 @@ func convertError(err error, isUnmarshal bool) error { msg: err.Err.Error(), Offset: err.Offset + 1, } - if errors.Is(err, io.ErrUnexpectedEOF) { + if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { ret.msg = "unexpected end of JSON input" } return ret diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index 2380c53..43f17f1 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -72,6 +72,7 @@ func TestCompatCompact(t *testing.T) { Err string } testcases := map[string]testcase{ + "empty": {In: ``, Out: ``, Err: `unexpected end of JSON input`}, "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, @@ -106,6 +107,7 @@ func TestCompatIndent(t *testing.T) { Err string } testcases := map[string]testcase{ + "empty": {In: ``, Out: ``, Err: `unexpected end of JSON input`}, "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, diff --git a/compat/json/testdata/fuzz/FuzzEquiv/930f49fab2367014 b/compat/json/testdata/fuzz/FuzzEquiv/930f49fab2367014 new file mode 100644 index 0000000..7390d06 --- /dev/null +++ b/compat/json/testdata/fuzz/FuzzEquiv/930f49fab2367014 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte(" ") diff --git a/compat/json/testdata/fuzz/FuzzEquiv/caf81e9797b19c76 b/compat/json/testdata/fuzz/FuzzEquiv/caf81e9797b19c76 new file mode 100644 index 0000000..67322c7 --- /dev/null +++ b/compat/json/testdata/fuzz/FuzzEquiv/caf81e9797b19c76 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("") -- cgit v1.1-4-g5e80