You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When decoding data that contains bytes fields, the returned byte slices reference the same underlying array as the input. This can cause unexpected behavior when manipulating those byte slices. Specifically appending to these slices is not a safe operation, because they can change other byte slices.
Click to expand failing test case
Notice how appending to the slice foo had an effect on bar and binary. All three slices use the same underlying array.
package avro
import (
"bytes""testing""github.com/linkedin/goavro/v2"
)
funcTestBytesOverwrite(t*testing.T) {
schema:=`{ "name":"example", "type":"record", "fields":[ {"name":"foo","type":"bytes"}, {"name":"bar","type":"bytes"} ]}`codec, err:=goavro.NewCodec(schema)
iferr!=nil {
t.Fatal(err)
}
// binary data contains {foo:[1,2],bar:[3,4]}binary:= []byte{4, 1, 2, 4, 3, 4}
gotNative, _, err:=codec.NativeFromBinary(binary)
iferr!=nil {
t.Fatal(err)
}
got:=gotNative.(map[string]interface{})
foo:=got["foo"].([]byte)
bar:=got["bar"].([]byte)
ifwant:= []byte{1, 2}; !bytes.Equal(foo, want) {
t.Fatalf("expected foo to be %v, actually got %v", want, foo)
}
ifwant:= []byte{3, 4}; !bytes.Equal(bar, want) {
t.Fatalf("expected bar to be %v, actually got %v", want, bar)
}
// because slices use the same underlying array we can overwrite the others// by appending to foofoo=append(foo, 0, 0)
ifwant:= []byte{1, 2, 0, 0}; !bytes.Equal(foo, want) {
t.Fatalf("expected foo to be %v, actually got %v", want, foo)
}
ifwant:= []byte{3, 4}; !bytes.Equal(bar, want) {
// this assertion fails, bar was changed to [0, 4]t.Errorf("expected bar to be %v, actually got %v", want, bar)
}
ifwant:= []byte{4, 1, 2, 4, 3, 4}; !bytes.Equal(binary, want) {
// this assertion fails, binary was changed to [4, 1, 2, 0, 0, 4]t.Errorf("expected binary to be %v, actually got %v", want, binary)
}
}
This could probably be fixed by allocating a new slice before returning buf[:size] in this line.
The text was updated successfully, but these errors were encountered:
When decoding data that contains bytes fields, the returned byte slices reference the same underlying array as the input. This can cause unexpected behavior when manipulating those byte slices. Specifically appending to these slices is not a safe operation, because they can change other byte slices.
Click to expand failing test case
Notice how appending to the slice
foo
had an effect onbar
andbinary
. All three slices use the same underlying array.This could probably be fixed by allocating a new slice before returning
buf[:size]
in this line.The text was updated successfully, but these errors were encountered: