Skip to content

Commit

Permalink
Merge pull request #33 from soheilhy/fix-b32
Browse files Browse the repository at this point in the history
Fix index out of range in patricia tree
  • Loading branch information
soheilhy committed Jul 15, 2016
2 parents e85da30 + eddb3b1 commit b269515
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions patricia.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (n *ptNode) match(b []byte, prefix bool) bool {
return true
}

if l >= len(b) {
return false
}

nextN, ok := n.next[b[l]]
if !ok {
return false
Expand Down
9 changes: 8 additions & 1 deletion patricia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func testPTree(t *testing.T, strs ...string) {
if pt.match(strings.NewReader(s + s)) {
t.Errorf("%s matches %s", s+s, s)
}

// The following tests are just to catch index out of
// range and off-by-one errors and not the functionality.
pt.matchPrefix(strings.NewReader(s[:len(s)-1]))
pt.match(strings.NewReader(s[:len(s)-1]))
pt.matchPrefix(strings.NewReader(s + "$"))
pt.match(strings.NewReader(s + "$"))
}
}

Expand All @@ -45,5 +52,5 @@ func TestPatriciaNonOverlapping(t *testing.T) {
}

func TestPatriciaOverlapping(t *testing.T) {
testPTree(t, "foo", "far", "farther", "boo", "bar")
testPTree(t, "foo", "far", "farther", "boo", "ba", "bar")
}

0 comments on commit b269515

Please sign in to comment.