summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-14 18:55:57 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-25 17:55:27 -0700
commit2a41777072f48467bef02bb3bd670d95c2b02102 (patch)
treeb4ac55094c142b3967acfdbf580d161988f822b7
parentf68498a6fdb421483d9aebb45527452f6255bb68 (diff)
compat/json: Handle io.EOF and io.ErrUnexpectedEOF the same
-rw-r--r--ReleaseNotes.md3
-rw-r--r--compat/json/compat.go25
-rw-r--r--compat/json/compat_test.go2
-rw-r--r--compat/json/testdata/fuzz/FuzzEquiv/930f49fab23670142
-rw-r--r--compat/json/testdata/fuzz/FuzzEquiv/caf81e9797b19c762
5 files changed, 21 insertions, 13 deletions
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("")