2
0
mirror of synced 2025-02-23 22:28:11 +00:00
Matt Joiner abb5cbc96e
Check for chunks overflowing piece bounds on request read
Test for integer overflow in when checking read requests are within the bounds of the associated piece. Another fix is required to limit the amount of memory that can be allocated for such requests.
2023-02-13 23:27:15 +11:00

51 lines
844 B
Go

package peer_protocol
import (
"encoding/binary"
"io"
"math"
"github.com/pkg/errors"
)
type (
// An alias for the underlying type of Integer. This is needed for fuzzing.
IntegerKind = uint32
Integer IntegerKind
)
const IntegerMax = math.MaxUint32
func (i *Integer) UnmarshalBinary(b []byte) error {
if len(b) != 4 {
return errors.New("expected 4 bytes")
}
*i = Integer(binary.BigEndian.Uint32(b))
return nil
}
func (i *Integer) Read(r io.Reader) error {
var b [4]byte
n, err := io.ReadFull(r, b[:])
if err == nil {
if n != 4 {
panic(n)
}
return i.UnmarshalBinary(b[:])
}
return err
}
// It's perfectly fine to cast these to an int. TODO: Or is it?
func (i Integer) Int() int {
return int(i)
}
func (i Integer) Uint64() uint64 {
return uint64(i)
}
func (i Integer) Uint32() uint32 {
return uint32(i)
}