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.
51 lines
844 B
Go
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)
|
|
}
|