tape: Safely cast when dynamically encoding/decoding

This commit is contained in:
2025-08-29 12:03:39 -04:00
parent 0ea7e222cc
commit 04c352fad6
3 changed files with 20 additions and 4 deletions

View File

@@ -171,10 +171,12 @@ func decodeAnyOrError(decoder *Decoder, destination reflect.Value, tag Tag) (n i
if length > uint64(MaxStructureLength) {
return 0, ErrTooLong
}
lengthCast, err := Uint64ToIntSafe(length)
if err != nil { return n, err }
oneTag, nn, err := decoder.ReadTag()
n += nn; if err != nil { return n, err }
if destination.Cap() < int(length) {
destination.Grow(int(length) - destination.Cap())
if destination.Cap() < lengthCast {
destination.Grow(lengthCast - destination.Cap())
}
// skip the rest of the array if the one tag doesn't
// match up with the destination
@@ -187,7 +189,7 @@ func decodeAnyOrError(decoder *Decoder, destination reflect.Value, tag Tag) (n i
break
}
if err != nil { return n, err }
destination.SetLen(int(length))
destination.SetLen(lengthCast)
for index := range length {
nn, err := decodeAny(decoder, destination.Index(int(index)), oneTag)
n += nn