Skip to content

Commit 0036af6

Browse files
Update (*valueReader).readString() to support bVR + streaming
1 parent 2e8be2a commit 0036af6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

bson/value_reader.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,21 +907,23 @@ func (vr *valueReader) readString() (string, error) {
907907
if err != nil {
908908
return "", err
909909
}
910+
910911
if length <= 0 {
911912
return "", fmt.Errorf("invalid string length: %d", length)
912913
}
913914

914-
buf := make([]byte, length)
915-
err = vr.read(buf)
915+
raw, err := readBytes(vr.src, int(length))
916916
if err != nil {
917917
return "", err
918918
}
919919

920-
if buf[length-1] != 0x00 {
921-
return "", fmt.Errorf("string does not end with null byte, but with %v", buf[length-1])
920+
// Check that the last byte is the NUL terminator.
921+
if raw[len(raw)-1] != 0x00 {
922+
return "", fmt.Errorf("string does not end with null byte, but with %v", raw[len(raw)-1])
922923
}
923924

924-
return string(buf[:length-1]), nil
925+
// Convert and strip the trailing NUL.
926+
return string(raw[:len(raw)-1]), nil
925927
}
926928

927929
func (vr *valueReader) peekLength() (int32, error) {

0 commit comments

Comments
 (0)