Repository: moby/moby
File: daemon/libnetwork/ipams/defaultipam/address_space.go (Go)

Code before:
//
// This method is safe for concurrent use.
func (aSpace *addrSpace) allocatePredefinedPool(reserved []netip.Prefix, prefixSize int) (netip.Prefix, error) {
	aSpace.mu.Lock()
	defer aSpace.mu.Unlock()

	var pdfID int
	var partialOverlap bool
	var prevAlloc netip.Prefix

	it := newMergeIter(aSpace.allocated, reserved, netiputil.PrefixCompare)

	makeAlloc := func(subnet netip.Prefix) netip.Prefix {
		// it.ia tracks the position of the mergeIter within aSpace.allocated.
		aSpace.allocated = slices.Insert(aSpace.allocated, it.ia, subnet)
		aSpace.subnets[subnet] = newPoolData(subnet)
		return subnet
	}

	// Filter the pools to only those that match the requested subnet size (if one is specified).
	var predefined []*ipamutils.NetworkToSplit
	if prefixSize == 0 {
		predefined = aSpace.predefined
	} else {
		for _, pdf := range aSpace.predefined {
			if pdf.Base.Bits() > prefixSize || (pdf.Base.Addr().Is4() && prefixSize > 32) || prefixSize > 128 {
				// The subnet size isn't valid for the pool
				continue
			}

			predefined = append(predefined, &ipamutils.NetworkToSplit{
				Base: pdf.Base,
				Size: prefixSize,
			})
		}
	}

	if len(predefined) == 0 {
		// If we don't have any valid predefined networks
		return netip.Prefix{}, ipamapi.ErrInvalidPool
	}

	for {
		allocated := it.Get()
		if allocated == (netip.Prefix{}) {
			// We reached the end of both 'aSpace.allocated' and 'reserved'.
			break
		}

		if pdfID >= len(predefined) {
			// We ran out of predefined networks.

Review comment (suggestion):
```suggestion
			if pdf.Base.Bits() > prefixSize || prefixSize > pdf.Base.Addr().BitLen() {
```

Code after:
//
// This method is safe for concurrent use.
func (aSpace *addrSpace) allocatePredefinedPool(reserved []netip.Prefix, prefixSize int) (netip.Prefix, error) {
	aSpace.mu.Lock()
	defer aSpace.mu.Unlock()

	var pdfID int
	var partialOverlap bool
	var prevAlloc netip.Prefix

	it := newMergeIter(aSpace.allocated, reserved, netiputil.PrefixCompare)

	makeAlloc := func(subnet netip.Prefix) netip.Prefix {
		// it.ia tracks the position of the mergeIter within aSpace.allocated.
		aSpace.allocated = slices.Insert(aSpace.allocated, it.ia, subnet)
		aSpace.subnets[subnet] = newPoolData(subnet)
		return subnet
	}

	// Filter the pools to only those that match the requested subnet size (if one is specified).
	var predefined []*ipamutils.NetworkToSplit
	if prefixSize == 0 {
		predefined = aSpace.predefined
	} else {
		for _, pdf := range aSpace.predefined {
			if pdf.Base.Bits() > prefixSize || prefixSize > pdf.Base.Addr().BitLen() {
				// The subnet size isn't valid for the pool
				continue
			}

			predefined = append(predefined, &ipamutils.NetworkToSplit{
				Base: pdf.Base,
				Size: prefixSize,
			})
		}
	}

	if len(predefined) == 0 {
		// If we don't have any valid predefined networks
		return netip.Prefix{}, ipamapi.ErrInvalidPool
	}

	for {
		allocated := it.Get()
		if allocated == (netip.Prefix{}) {
			// We reached the end of both 'aSpace.allocated' and 'reserved'.
			break
		}

		if pdfID >= len(predefined) {
			// We ran out of predefined networks.