Skip to content

Commit

Permalink
Changes to ReadElement
Browse files Browse the repository at this point in the history
  • Loading branch information
timothy-kim-mongo committed Jul 12, 2024
1 parent b5594d4 commit 404a0ba
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions x/bsonx/bsoncore/bsoncore.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,22 @@ func ReadElement(src []byte) (Element, []byte, bool) {
return nil, src, false
}
t := Type(src[0])
idx := bytes.IndexByte(src[1:], 0x00)
if idx == -1 {
idx := 1
for idx < len(src) && src[idx] != 0x00 {
idx++
}
if idx >= len(src) {
return nil, src, false
}
length, ok := valueLength(src[idx+2:], t) // We add 2 here because we called IndexByte with src[1:]
idx++ // Move past the null byte
length, ok := valueLength(src[idx:], t)
if !ok {
return nil, src, false
}
elemLength := 1 + idx + 1 + int(length)
elemLength := idx + int(length)
if elemLength > len(src) {
return nil, src, false
}
if elemLength < 0 {
return nil, src, false
}
return src[:elemLength], src[elemLength:], true
}

Expand Down

0 comments on commit 404a0ba

Please sign in to comment.