Skip to content

Commit

Permalink
fix: handle empty byte slices (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemmsilva authored Jun 11, 2024
1 parent 3e4ac04 commit ec0fc22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bcs/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ func (d *Decoder) decodeByteSlice(v reflect.Value) (int, error) {
return n, err
}

if size == 0 {
return n, nil
}

tmp := make([]byte, size)

read, err := d.reader.Read(tmp)
Expand Down
15 changes: 15 additions & 0 deletions bcs/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,18 @@ func TestUnmarshal(t *testing.T) {
}
}
}

func TestEmptyByteSlice(t *testing.T) {
encoded, err := bcs.Marshal([]byte{})
if err != nil {
t.Error(err)
}
decoded := new([]byte)
n, err := bcs.Unmarshal(encoded, decoded)
if err != nil {
t.Error(err)
}
if n != 1 {
t.Errorf("want parsed length 1")
}
}

0 comments on commit ec0fc22

Please sign in to comment.