Skip to content
This repository has been archived by the owner on Dec 14, 2020. It is now read-only.

Commit

Permalink
Fix null type handling in readAny (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencegripper authored and vcabbage committed Aug 1, 2018
1 parent 0ae7451 commit 1c4e912
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 6 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func readArrayHeader(r *buffer) (length int64, _ error) {
}
length = int64(binary.BigEndian.Uint32(buf[4:8]))
default:
return 0, errorErrorf("type code %#02x is not a recognized list type", type_)
return 0, errorErrorf("type code %#02x is not a recognized array type", type_)
}
return length, nil
}
Expand Down Expand Up @@ -568,7 +568,7 @@ func readBinary(r *buffer) ([]byte, error) {
}
length = int64(binary.BigEndian.Uint32(buf))
default:
return nil, errorErrorf("type code %#02x is not a recognized string type", type_)
return nil, errorErrorf("type code %#02x is not a recognized binary type", type_)
}

buf, ok := r.next(length)
Expand All @@ -579,6 +579,10 @@ func readBinary(r *buffer) ([]byte, error) {
}

func readAny(r *buffer) (interface{}, error) {
if tryReadNull(r) {
return nil, nil
}

type_, err := r.peekType()
if err != nil {
return nil, errorNew("invalid length")
Expand Down
2 changes: 2 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type marshaler interface {

func marshal(wr *buffer, i interface{}) error {
switch t := i.(type) {
case nil:
wr.writeByte(byte(typeCodeNull))
case bool:
if t {
wr.writeByte(byte(typeCodeBoolTrue))
Expand Down
12 changes: 11 additions & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,22 @@ func TestMarshalUnmarshal(t *testing.T) {
}
}

// handle special case around nil type
if type_ == nil {
err = unmarshal(&buf, nil)
if err != nil {
t.Fatal(fmt.Sprintf("%+v", err))
return
}
return
}

newType := reflect.New(reflect.TypeOf(type_))
err = unmarshal(&buf, newType.Interface())
if err != nil {
t.Fatal(fmt.Sprintf("%+v", err))
return
}

cmpType := reflect.Indirect(newType).Interface()
if !testEqual(type_, cmpType) {
t.Errorf("Roundtrip produced different results:\n %s", testDiff(type_, cmpType))
Expand Down Expand Up @@ -537,6 +546,7 @@ var (
}

generalTypes = []interface{}{
nil,
UUID{1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16},
bool(true),
int8(math.MaxInt8),
Expand Down

0 comments on commit 1c4e912

Please sign in to comment.