Skip to content

Commit

Permalink
Merge pull request #410 from fxamacker/fxamacker/fix-diagnose-empty-d…
Browse files Browse the repository at this point in the history
…ata-error

Fix Diagnose to return io.EOF error on empty data.

This was detected while updating CBOR fuzz tests for v2.5.0-beta3.
  • Loading branch information
fxamacker authored Jun 18, 2023
2 parents d5917a0 + 37f6b99 commit 3ae4001
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func (di *diagnose) diag(cborSequence bool) (string, error) {
}

case io.EOF:
if firstItem {
return di.w.String(), err
}
return di.w.String(), nil

default:
Expand Down
39 changes: 39 additions & 0 deletions diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cbor
import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1079,3 +1080,41 @@ func TestDiagnoseNotwellformedData(t *testing.T) {
t.Errorf("Diagnose(0x%x) returned error %q", cborData, err)
}
}

func TestDiagnoseEmptyData(t *testing.T) {
var emptyData []byte

defaultMode, _ := DiagOptions{}.DiagMode()
sequenceMode, _ := DiagOptions{CBORSequence: true}.DiagMode()

testCases := []struct {
name string
dm DiagMode
}{
{"default", defaultMode},
{"sequence", sequenceMode},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s, err := tc.dm.Diagnose(emptyData)
if len(s) != 0 {
t.Errorf("Diagnose() didn't return empty notation for empty data")
}
if err != io.EOF {
t.Errorf("Diagnose() didn't return io.EOF for empty data")
}

s, rest, err := tc.dm.DiagnoseFirst(emptyData)
if len(s) != 0 {
t.Errorf("DiagnoseFirst() didn't return empty notation for empty data")
}
if len(rest) != 0 {
t.Errorf("DiagnoseFirst() didn't return empty rest for empty data")
}
if err != io.EOF {
t.Errorf("DiagnoseFirst() didn't return io.EOF for empty data")
}
})
}
}

0 comments on commit 3ae4001

Please sign in to comment.