Skip to content

Commit

Permalink
Fix index-out-of-bound bug when inserting network that shares direct …
Browse files Browse the repository at this point in the history
…branch point with single-IP (leaf) network, closes #3
  • Loading branch information
yl2chen committed Sep 11, 2017
1 parent 6009265 commit e27c5f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (p *prefixTrie) insert(network rnet.Network, entry RangerEntry) error {
if err != nil {
return err
}
if lcb-1 > child.targetBitPosition() {
if int(lcb) > child.targetBitPosition()+1 {
child = newPathprefixTrie(network, p.totalNumberOfBits()-lcb)
err := p.insertPrefix(bit, child)
if err != nil {
Expand Down Expand Up @@ -259,12 +259,14 @@ func (p *prefixTrie) totalNumberOfBits() uint {
return rnet.BitsPerUint32 * uint(len(p.network.Number))
}

func (p *prefixTrie) targetBitPosition() uint {
return p.totalNumberOfBits() - p.numBitsSkipped - 1
func (p *prefixTrie) targetBitPosition() int {
return int(p.totalNumberOfBits()-p.numBitsSkipped) - 1
}

func (p *prefixTrie) targetBitFromIP(n rnet.NetworkNumber) (uint32, error) {
return n.Bit(p.targetBitPosition())
// This is a safe uint boxing of int since we should never attempt to get
// target bit at a negative position.
return n.Bit(uint(p.targetBitPosition()))
}

func (p *prefixTrie) hasEntry() bool {
Expand Down
34 changes: 34 additions & 0 deletions trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ func TestPrefixTrieInsert(t *testing.T) {
name string
}{
{rnet.IPv4, []string{"192.168.0.1/24"}, []string{"192.168.0.1/24"}, "basic insert"},
{
rnet.IPv4,
[]string{"1.2.3.4/32", "1.2.3.5/32"},
[]string{"1.2.3.4/32", "1.2.3.5/32"},
"single ip IPv4 network insert",
},
{
rnet.IPv6,
[]string{"0::1/128", "0::2/128"},
[]string{"0::1/128", "0::2/128"},
"single ip IPv6 network insert",
},
{
rnet.IPv4,
[]string{"192.168.0.1/16", "192.168.0.1/24"},
[]string{"192.168.0.1/16", "192.168.0.1/24"},
"in order insert",
},
{
rnet.IPv4,
[]string{"192.168.0.1/32", "192.168.0.1/32"},
[]string{"192.168.0.1/32"},
"duplicate network insert",
},
{
rnet.IPv4,
[]string{"192.168.0.1/24", "192.168.0.1/16"},
Expand Down Expand Up @@ -97,6 +115,22 @@ func TestPrefixTrieRemove(t *testing.T) {
[]string{},
"basic remove",
},
{
rnet.IPv4,
[]string{"1.2.3.4/32", "1.2.3.5/32"},
[]string{"1.2.3.5/32"},
[]string{"1.2.3.5/32"},
[]string{"1.2.3.4/32"},
"single ip IPv4 network remove",
},
{
rnet.IPv4,
[]string{"0::1/128", "0::2/128"},
[]string{"0::2/128"},
[]string{"0::2/128"},
[]string{"0::1/128"},
"single ip IPv6 network remove",
},
{
rnet.IPv4,
[]string{"192.168.0.1/24", "192.168.0.1/25", "192.168.0.1/26"},
Expand Down

0 comments on commit e27c5f9

Please sign in to comment.