add zero max ports error

This commit is contained in:
soypat
2025-07-15 00:57:24 -03:00
parent ad22c61b28
commit 63dc5d1079
4 changed files with 8 additions and 0 deletions
+1
View File
@@ -42,6 +42,7 @@ type node struct {
} }
var ( var (
errZeroMaxNodes = errors.New("zero max ports")
errZeroPort = errors.New("port must be greater than zero") errZeroPort = errors.New("port must be greater than zero")
errInvalidProto = errors.New("invalid protocol") errInvalidProto = errors.New("invalid protocol")
errProtoRegistered = errors.New("protocol already registered") errProtoRegistered = errors.New("protocol already registered")
+2
View File
@@ -36,6 +36,8 @@ func (ls *StackEthernet) HardwareAddr6() [6]byte {
func (ls *StackEthernet) Reset6(mac, gateway [6]byte, mtu, maxNodes int) error { func (ls *StackEthernet) Reset6(mac, gateway [6]byte, mtu, maxNodes int) error {
if mtu > math.MaxUint16 || mtu < 256 { if mtu > math.MaxUint16 || mtu < 256 {
return errors.New("invalid MTU") return errors.New("invalid MTU")
} else if maxNodes <= 0 {
return errZeroMaxNodes
} }
ls.handlers = slices.Grow(ls.handlers[:0], maxNodes) ls.handlers = slices.Grow(ls.handlers[:0], maxNodes)
*ls = StackEthernet{ *ls = StackEthernet{
+3
View File
@@ -27,6 +27,9 @@ type StackIP struct {
} }
func (sb *StackIP) Reset(addr netip.Addr, maxNodes int) error { func (sb *StackIP) Reset(addr netip.Addr, maxNodes int) error {
if maxNodes <= 0 {
return errZeroMaxNodes
}
err := sb.SetAddr(addr) err := sb.SetAddr(addr)
if err != nil { if err != nil {
return err return err
+2
View File
@@ -27,6 +27,8 @@ func (ps *StackPorts) ResetTCP(maxNodes int) error {
func (ps *StackPorts) Reset(protocol uint64, dstPortOffset uint16, maxNodes int) error { func (ps *StackPorts) Reset(protocol uint64, dstPortOffset uint16, maxNodes int) error {
if protocol > math.MaxUint16 { if protocol > math.MaxUint16 {
return errInvalidProto return errInvalidProto
} else if maxNodes <= 0 {
return errZeroMaxNodes
} }
ps.handlers = slices.Grow(ps.handlers[:0], maxNodes) ps.handlers = slices.Grow(ps.handlers[:0], maxNodes)
*ps = StackPorts{ *ps = StackPorts{