ipaddr icon indicating copy to clipboard operation
ipaddr copied to clipboard

Infinity loop at branchingFactorIPv4 function

Open savely-krasovsky opened this issue 3 years ago • 0 comments

It seems like on some sets of prefixes (128.0.0.0/1 and 0.0.0.0/1 in my case), it causes infinity loop:

func branchingFactorIPv4(ps []Prefix) (int, bool) {
	var lastBF, lastN int
	base := ipToIPv4Int(ps[0].IP.Mask(ps[0].Mask))
	mask := ipMaskToIPv4Int(ps[0].Mask)
	l := ps[0].Len()
	for bf := 1; bf < IPv4PrefixLen; bf++ {
		n, nfull := 0, 1<<uint(bf)
		max := ipv4Int(1 << uint(bf))
		aggrMask := mask << uint(bf)
		for pat := ipv4Int(0); pat < max; pat++ {
			aggr := base&aggrMask | pat<<uint(IPv4PrefixLen-l)
			for _, p := range ps {
				i := ipToIPv4Int(p.IP)
				if aggr == i&mask {
					n++
				}
			}
		}
		if n < nfull {
			break
		}
		lastBF = bf
		lastN = n
	}
	n := 1 << uint(lastBF)
	return n, lastN >= n
}

After some calculation it causes panic:

runtime error: slice bounds out of range [:2147483648] with capacity 24

savely-krasovsky avatar Jul 04 '21 14:07 savely-krasovsky