yl2chen / cidranger

Fast IP to CIDR lookup in Golang
MIT License
899 stars 106 forks source link

Finding exact ip from /32 subnet causes index out of range #4

Closed oskarwojciski closed 7 years ago

oskarwojciski commented 7 years ago

Similar to #3 - when you try to search for exact IP from "one ip subnet" code panics with runtime error: index out of range .

Test to replicate the issue:

func TestToReplicateIssue(t *testing.T) {
    cases := []struct {
        version  rnet.IPVersion
        inserts  []string
        ip       net.IP
        networks []string
        name     string
    }{
        {
            rnet.IPv4,
            []string{"192.168.0.1/32"},
            net.ParseIP("192.168.0.1"),
            []string{"192.168.0.1/32"},
            "basic containing network for /32 mask",
        },
        {
            rnet.IPv6,
            []string{"a::1/128"},
            net.ParseIP("a::1"),
            []string{"a::1/128"},
            "basic containing network for /128 mask",
        },
    }
    for _, tc := range cases {
        t.Run(tc.name, func(t *testing.T) {
            trie := newPrefixTree(tc.version)
            for _, insert := range tc.inserts {
                _, network, _ := net.ParseCIDR(insert)
                err := trie.Insert(NewBasicRangerEntry(*network))
                assert.NoError(t, err)
            }
            expectedEntries := []RangerEntry{}
            for _, network := range tc.networks {
                _, net, _ := net.ParseCIDR(network)
                expectedEntries = append(expectedEntries, NewBasicRangerEntry(*net))
            }
            networks, err := trie.ContainingNetworks(tc.ip)
            assert.NoError(t, err)
            assert.Equal(t, expectedEntries, networks)
        })
    }
}
yl2chen commented 7 years ago

Thanks will address this ASAP I get some free cycles.

yl2chen commented 7 years ago

Fix is in, closing the issue, please re-open if necessary, thanks!