Skip to content

[FIXED] Potential panic if subject to delete shorter then interior node index and prefix. #6914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions server/stree/stree.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (t *SubjectTree[T]) Match(filter []byte, cb func(subject []byte, val *T)) {
t.match(t.root, parts, _pre[:0], cb)
}

// IterOrdered will walk all entries in the SubjectTree lexographically. The callback can return false to terminate the walk.
// IterOrdered will walk all entries in the SubjectTree lexicographically. The callback can return false to terminate the walk.
func (t *SubjectTree[T]) IterOrdered(cb func(subject []byte, val *T) bool) {
if t == nil || t.root == nil {
return
Expand Down Expand Up @@ -244,6 +244,10 @@ func (t *SubjectTree[T]) delete(np *node, subject []byte, si int) (*T, bool) {
}
// Not a leaf node.
if bn := n.base(); len(bn.prefix) > 0 {
// subject could be shorter and would panic on bad index into subject slice.
if len(subject) < si+len(bn.prefix) {
return nil, false
}
if !bytes.Equal(subject[si:si+len(bn.prefix)], bn.prefix) {
return nil, false
}
Expand Down Expand Up @@ -377,7 +381,7 @@ func (t *SubjectTree[T]) match(n node, parts [][]byte, pre []byte, cb func(subje
}
}

// Interal iter function to walk nodes in lexigraphical order.
// Internal iter function to walk nodes in lexicographical order.
func (t *SubjectTree[T]) iter(n node, pre []byte, ordered bool, cb func(subject []byte, val *T) bool) bool {
if n.isLeaf() {
ln := n.(*leaf[T])
Expand Down
22 changes: 22 additions & 0 deletions server/stree/stree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,25 @@ func TestSubjectTreeLazyIntersect(t *testing.T) {
require_Equal(t, intersected["foo.bar"], 1)
require_Equal(t, intersected["foo.bar.baz.qux"], 1)
}

func TestSubjectTreeDeleteShortSubjectNoPanic(t *testing.T) {
defer func() {
p := recover()
require_True(t, p == nil)
}()

st := NewSubjectTree[int]()

st.Insert(b("foo.bar.baz"), 1)
st.Insert(b("foo.bar.qux"), 2)

v, found := st.Delete(b("foo.bar"))
require_False(t, found)
require_Equal(t, v, nil)
v, found = st.Find(b("foo.bar.baz"))
require_True(t, found)
require_Equal(t, *v, 1)
v, found = st.Find(b("foo.bar.qux"))
require_True(t, found)
require_Equal(t, *v, 2)
}