@@ -384,9 +384,13 @@ func (vr *valueReader) ReadBinary() (b []byte, btype byte, err error) {
384
384
if err != nil {
385
385
return nil , 0 , err
386
386
}
387
+ // Make a copy of the returned byte slice because it's just a subslice from the valueReader's
388
+ // buffer and is not safe to return in the unmarshaled value.
389
+ cp := make ([]byte , len (b ))
390
+ copy (cp , b )
387
391
388
392
vr .pop ()
389
- return b , btype , nil
393
+ return cp , btype , nil
390
394
}
391
395
392
396
func (vr * valueReader ) ReadBoolean () (bool , error ) {
@@ -737,6 +741,9 @@ func (vr *valueReader) ReadValue() (ValueReader, error) {
737
741
return vr , nil
738
742
}
739
743
744
+ // readBytes reads length bytes from the valueReader starting at the current offset. Note that the
745
+ // returned byte slice is a subslice from the valueReader buffer and must be converted or copied
746
+ // before returning in an unmarshaled value.
740
747
func (vr * valueReader ) readBytes (length int32 ) ([]byte , error ) {
741
748
if length < 0 {
742
749
return nil , fmt .Errorf ("invalid length: %d" , length )
@@ -749,10 +756,7 @@ func (vr *valueReader) readBytes(length int32) ([]byte, error) {
749
756
start := vr .offset
750
757
vr .offset += int64 (length )
751
758
752
- b := make ([]byte , length )
753
- copy (b , vr .d [start :start + int64 (length )])
754
-
755
- return b , nil
759
+ return vr .d [start : start + int64 (length )], nil
756
760
}
757
761
758
762
func (vr * valueReader ) appendBytes (dst []byte , length int32 ) ([]byte , error ) {
0 commit comments