THis strategies checks if the primary IP community comprises the second (the maskSize of first ought to be <= that the maskSize of second, and their IPs ought to be similar all through the primary masks:
func ContainsIPNetwork(first, second web.IPNet) bool {
if len(first.IP) != len(second.IP) {
return false
}
i := len(first.IP) - 1
for i > 0 && (first.Masks[i]|second.Masks[i] == 0) {
i--
}
if first.Masks[i] > second.Masks[i] || ((first.IP[i]^second.IP[i])&first.Masks[i] != 0) {
return false
}
i--
for i >= 0 && first.IP[i] == second.IP[i] {
i--
}
return i == -1
}
There’s a bug within the code above, it really works for many instances, however not all. Can you work it out ?
I for one can not, however there’s actually a bug. What ought to I do to determine it out ?
I didn’t checked the code however:
- how have you learnt there;s a bug? (assessments, procedures, …)
- have you ever tried to debug the code?
listed here are 2 check instances that fail:
first {IP:0.0.0.0 Masks:00000000}, second {IP:2.0.0.0 Masks:ffffffff}, comprises false <–unsuitable
first {IP:0.0.0.0 Masks:00000000}, second {IP:255.255.255.255 Masks:ffffffff}, comprises false <–unsuitable
To your first instance, on this loop:
when i == 0
, first.IP[0] == 0
and second.IP[0] == 2
, so the loop breaks and returns i == -1
(0 == -1
, false
).
To your second instance, in the identical loop, when i
is decremented from 3
to 2
, first.IP[2] == 0
and second.IP[2] == 255
, so the loop breaks and returns i == -1
(2 == -1
, false
).
1 Like
This subject was routinely closed 90 days after the final reply. New replies are now not allowed.