Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump go to 1.20; drop pkg/errors; fix fuzz tests #24

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go: [1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18]
go: [1.15, 1.16, 1.17, 1.18, 1.19, "1.20", 1.21]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand All @@ -29,4 +29,4 @@ jobs:
run: go test ./...

- name: Race
run: go test -race ./...
run: go test -race -count 10 ./...
33 changes: 16 additions & 17 deletions decode.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ubjson

import (
"errors"
"fmt"
"io"
"reflect"

"github.com/pkg/errors"
)

// MaxCollectionAlloc is the default maximum collection capacity allocation.
Expand Down Expand Up @@ -48,7 +47,7 @@ func (d *Decoder) DecodeValue(v Value) error {
// decodeValue asserts a value's type marker, then decodes the data.
func (d *Decoder) decodeValue(m Marker, decodeData func(*Decoder) error) error {
if r, err := d.readValType(); err != nil {
return errors.Wrapf(err, "failed trying to read type '%s'", m)
return fmt.Errorf("failed trying to read type '%s': %w", m, err)
} else if r != m {
return errWrongTypeRead(m, r)
}
Expand All @@ -59,7 +58,7 @@ func (d *Decoder) decodeValue(m Marker, decodeData func(*Decoder) error) error {
func (d *Decoder) assertType(m Marker) error {
r, err := d.readMarker()
if err != nil {
return errors.Wrapf(err, "failed trying to read type '%s'", m)
return fmt.Errorf("failed trying to read type '%s': %w", m, err)
}
if r != m {
return errWrongTypeRead(m, r)
Expand Down Expand Up @@ -587,7 +586,7 @@ func (d *Decoder) Decode(v interface{}) error {

value := reflect.ValueOf(v)
if value.Kind() != reflect.Ptr {
return errors.Errorf("can only decode into pointers, not: %s", value.Type())
return fmt.Errorf("can only decode into pointers, not: %s", value.Type())
}
// Containers
switch value.Elem().Kind() {
Expand Down Expand Up @@ -618,7 +617,7 @@ func arrayToArray(arrayPtr reflect.Value) func(*ArrayDecoder) error {
elemType := arrayValue.Type().Elem()
if ad.Len > 0 {
if ad.Len >= 0 && ad.Len != arrayValue.Len() {
return errors.Errorf("unable to decode data length %d into array of length %d", ad.Len, arrayValue.Len())
return fmt.Errorf("unable to decode data length %d into array of length %d", ad.Len, arrayValue.Len())
}
}

Expand Down Expand Up @@ -649,7 +648,7 @@ func arrayToSlice(slicePtr reflect.Value) func(*ArrayDecoder) error {
sliceValue = reflect.Append(sliceValue, elemPtr.Elem())
}
} else if ad.Len > ad.MaxCollectionAlloc {
return errors.Errorf("collection exceeds max allocation limit of %d: %d", ad.MaxCollectionAlloc, ad.Len)
return fmt.Errorf("collection exceeds max allocation limit of %d: %d", ad.MaxCollectionAlloc, ad.Len)
} else {
sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), ad.Len, ad.Len))

Expand All @@ -672,18 +671,18 @@ func objectIntoStruct(structPtr reflect.Value) func(*ObjectDecoder) error {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return errors.Wrapf(err, "failed to decode key with call #%d", o.count)
return fmt.Errorf("failed to decode key with call #%d: %w", o.count, err)
}
structValue := structPtr.Elem()
f := fieldByName(structValue, k)
if f == zeroValue {
// Discard value with no matching field.
// TODO could be more efficient with custom discardValue() method
if _, err := o.decodeInterface(); err != nil {
return errors.Wrapf(err, "failed to discard value for %q with call #%d", k, o.count)
return fmt.Errorf("failed to discard value for %q with call #%d: %w", k, o.count, err)
}
} else if err := o.Decode(f.Addr().Interface()); err != nil {
return errors.Wrapf(err, "failed to decode value for %q with call #%d", k, o.count)
return fmt.Errorf("failed to decode value for %q with call #%d: %w", k, o.count, err)
}
}
return o.End()
Expand All @@ -703,7 +702,7 @@ func fieldByName(structValue reflect.Value, k string) reflect.Value {
func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
return func(o *ObjectDecoder) error {
if o.Len > o.MaxCollectionAlloc {
return errors.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
return fmt.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
}
mapValue := mapPtr.Elem()
mapValue.Set(makeMap(mapValue.Type(), o.Len))
Expand All @@ -712,13 +711,13 @@ func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return errors.Wrapf(err, "failed to decode key #%d", o.count)
return fmt.Errorf("failed to decode key #%d: %w", o.count, err)
}

valPtr := reflect.New(elemType)

if err := o.Decode(valPtr.Interface()); err != nil {
return errors.Wrapf(err, "failed to decode value #%d", o.count)
return fmt.Errorf("failed to decode value #%d: %w", o.count, err)
}

mapValue.SetMapIndex(reflect.ValueOf(k), valPtr.Elem())
Expand All @@ -731,7 +730,7 @@ func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
// either interface{} or a stricter type if the object is strongly typed.
func objectAsInterface(o *ObjectDecoder) (interface{}, error) {
if o.Len > o.MaxCollectionAlloc {
return nil, errors.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
return nil, fmt.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
}
if o.ValType == NoOpMarker {
return nil, errors.New("No-Op (N) is not a legal strong type")
Expand All @@ -742,11 +741,11 @@ func objectAsInterface(o *ObjectDecoder) (interface{}, error) {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return nil, errors.Wrapf(err, "failed to decode key #%d", o.count)
return nil, fmt.Errorf("failed to decode key #%d: %w", o.count, err)
}
valPtr := reflect.New(mapType.Elem())
if err := o.Decode(valPtr.Interface()); err != nil {
return nil, errors.Wrapf(err, "failed to decode value #%d", o.count)
return nil, fmt.Errorf("failed to decode value #%d: %w", o.count, err)
}

mapValue.SetMapIndex(reflect.ValueOf(k), valPtr.Elem())
Expand Down Expand Up @@ -774,7 +773,7 @@ func arrayAsInterface(a *ArrayDecoder) (interface{}, error) {
sliceValue = reflect.Append(sliceValue, elemPtr.Elem())
}
} else if a.Len > a.MaxCollectionAlloc {
return "", errors.Errorf("collection exceeds max allocation limit of %d: %d", a.MaxCollectionAlloc, a.Len)
return "", fmt.Errorf("collection exceeds max allocation limit of %d: %d", a.MaxCollectionAlloc, a.Len)
} else {
sliceValue = reflect.MakeSlice(sliceType, a.Len, a.Len)

Expand Down
24 changes: 12 additions & 12 deletions encode.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ubjson

import (
"errors"
"fmt"
"io"
"reflect"

"github.com/pkg/errors"
)

// Encoder provides methods for encoding UBJSON data types.
Expand Down Expand Up @@ -115,7 +115,7 @@ func (e *Encoder) EncodeInt(v int) error {
case Int64Marker:
return e.EncodeInt64(int64(v))
default:
return errors.Errorf("unsupported marker: %s", string(m))
return fmt.Errorf("unsupported marker: %s", string(m))
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (a *ArrayEncoder) End() error {
return err
}
} else if a.len != a.count {
return errors.Errorf("unable to end array of length %d after %d elements", a.len, a.count)
return fmt.Errorf("unable to end array of length %d after %d elements", a.len, a.count)
}

return a.Flush()
Expand Down Expand Up @@ -339,7 +339,7 @@ func (o *ObjectEncoder) End() error {
return err
}
} else if 2*o.len != o.count {
return errors.Errorf("unable to end map of %d entries after %d", o.len, o.count/2)
return fmt.Errorf("unable to end map of %d entries after %d", o.len, o.count/2)
}

return o.Flush()
Expand Down Expand Up @@ -464,7 +464,7 @@ func (e *Encoder) Encode(v interface{}) error {

case reflect.Map:
if k := value.Type().Key().Kind(); k != reflect.String {
return errors.Errorf("unable to encode map of type %s: key reflect.Kind must be reflect.String but is %s", value.Type(), k)
return fmt.Errorf("unable to encode map of type %s: key reflect.Kind must be reflect.String but is %s", value.Type(), k)
}
return e.encode(ObjectStartMarker, encodeMap(value))

Expand All @@ -478,7 +478,7 @@ func (e *Encoder) Encode(v interface{}) error {
return e.Encode(value.Elem().Interface())
}

return errors.Errorf("unable to encode value: %v", v)
return fmt.Errorf("unable to encode value: %v", v)
}

func encodeArray(arrayValue reflect.Value) func(*Encoder) error {
Expand All @@ -501,7 +501,7 @@ func encodeArray(arrayValue reflect.Value) func(*Encoder) error {

for i := 0; i < arrayValue.Len(); i++ {
if err := ae.Encode(arrayValue.Index(i).Interface()); err != nil {
return errors.Wrapf(err, "failed to encode array element %d", i)
return fmt.Errorf("failed to encode array element %d: %w", i, err)
}
}

Expand Down Expand Up @@ -533,10 +533,10 @@ func encodeMap(mapValue reflect.Value) func(*Encoder) error {

for _, key := range keys {
if err := o.EncodeKey(key.String()); err != nil {
return errors.Wrapf(err, "failed to encode key %q", key.String())
return fmt.Errorf("failed to encode key %q: %w", key.String(), err)
}
if err := o.Encode(mapValue.MapIndex(key).Interface()); err != nil {
return errors.Wrapf(err, "failed to encode value for key %q", key.String())
return fmt.Errorf("failed to encode value for key %q: %w", key.String(), err)
}
}

Expand All @@ -562,11 +562,11 @@ func encodeStruct(structValue reflect.Value) func(*Encoder) error {
panic("invalid cached type info: no index for field " + name)
}
if err := o.EncodeKey(name); err != nil {
return errors.Wrapf(err, "failed to encode key %q", name)
return fmt.Errorf("failed to encode key %q: %w", name, err)
}
val := structValue.Field(i).Interface()
if err := o.Encode(val); err != nil {
return errors.Wrapf(err, "failed to encode value for key %q", name)
return fmt.Errorf("failed to encode value for key %q: %w", name, err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ubjson

import "github.com/pkg/errors"
import "fmt"

func errTooMany(len int) error {
return errors.Errorf("too many calls for container with len %d", len)
return fmt.Errorf("too many calls for container with len %d", len)
}

func errWrongTypeWrite(exp, got Marker) error {
return errors.Errorf("unable to write element type '%s' to container type '%s'", got, exp)
return fmt.Errorf("unable to write element type '%s' to container type '%s'", got, exp)
}

func errWrongTypeRead(exp, got Marker) error {
return errors.Errorf("tried to read type '%s' but found type '%s'", exp, got)
return fmt.Errorf("tried to read type '%s' but found type '%s'", exp, got)
}
3 changes: 3 additions & 0 deletions fuzz_1.18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func FuzzBinary(f *testing.F) {
for _, c := range cases {
f.Add(c.binary)
}
f.Add([]byte("[$C#U\x010"))
f.Add([]byte("[$d#U\x190000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
const corpus = "testdata/bin/corpus"
if dir, err := testdata.ReadDir(corpus); err != nil {
f.Fatal("failed to read corpus dir:", err)
Expand Down Expand Up @@ -54,6 +56,7 @@ func FuzzBlock(f *testing.F) {
for _, c := range cases {
f.Add(c.block)
}
f.Add("[[][$][C][#][U][1][0]")
const corpus = "testdata/block/corpus"
if dir, err := testdata.ReadDir(corpus); err != nil {
f.Fatal("failed to read corpus dir:", err)
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/jmank88/ubjson

go 1.18

require github.com/pkg/errors v0.8.1
go 1.20
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Loading
Loading