mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
chore(access): to avoid panic when decoding location
. #1001088459 Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
parent
d0493e7541
commit
6d424e5ef5
@ -210,6 +210,11 @@ func DecodeLocation(buf []byte) (Location, int, error) {
|
||||
if val, nn = next(); nn <= 0 {
|
||||
return loc, n, fmt.Errorf("bytes length blobs %d", nn)
|
||||
}
|
||||
// Each slice contains at least three one-byte uvarints.
|
||||
// Avoid panic and excessive allocation on malformed input.
|
||||
if val > 1024 || val > uint64(len(buf)/3) {
|
||||
return loc, n, fmt.Errorf("bytes length blobs %d invalid, buffer %d", nn, len(buf))
|
||||
}
|
||||
length := int(val)
|
||||
|
||||
if length > 0 {
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
mrand "math/rand"
|
||||
"testing"
|
||||
@ -142,6 +143,28 @@ func TestLocationDecodeError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocationDecodeInvalidSlicesLength(t *testing.T) {
|
||||
buf := (&Location{}).Encode()
|
||||
buf = buf[:len(buf)-1] // remove the encoded zero slices length
|
||||
|
||||
var encoded [binary.MaxVarintLen64]byte
|
||||
n := binary.PutUvarint(encoded[:], math.MaxUint64)
|
||||
buf = append(buf, encoded[:n]...)
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
_, _, err := DecodeLocation(buf)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
buf = (&Location{}).Encode()
|
||||
buf = buf[:len(buf)-1]
|
||||
n = binary.PutUvarint(encoded[:], 1024+1)
|
||||
buf = append(buf, encoded[:n]...)
|
||||
buf = append(buf, make([]byte, (1024+1)*3)...)
|
||||
_, _, err := DecodeLocation(buf)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestLocationSpread(t *testing.T) {
|
||||
{
|
||||
var loc Location
|
||||
|
||||
Loading…
Reference in New Issue
Block a user