diff --git a/server/stree/stree.go b/server/stree/stree.go index a289a629742..828631888f9 100644 --- a/server/stree/stree.go +++ b/server/stree/stree.go @@ -283,7 +283,7 @@ func (t *SubjectTree[T]) delete(np *node, subject []byte, si int) (*T, bool) { func (t *SubjectTree[T]) match(n node, parts [][]byte, pre []byte, cb func(subject []byte, val *T)) { // Capture if we are sitting on a terminal fwc. var hasFWC bool - if lp := len(parts); lp > 0 && parts[lp-1][0] == fwc { + if lp := len(parts); lp > 0 && len(parts[lp-1]) > 0 && parts[lp-1][0] == fwc { hasFWC = true } diff --git a/server/stree/stree_test.go b/server/stree/stree_test.go index 2a84d3c7598..cf6d08512b9 100644 --- a/server/stree/stree_test.go +++ b/server/stree/stree_test.go @@ -842,3 +842,15 @@ func TestSubjectTreeInsertWithNoPivot(t *testing.T) { require_False(t, updated) require_Equal(t, st.Size(), 0) } + +// Make sure we don't panic when checking for fwc. +func TestSubjectTreeMatchHasFWCNoPanic(t *testing.T) { + defer func() { + p := recover() + require_True(t, p == nil) + }() + st := NewSubjectTree[int]() + subj := []byte("foo") + st.Insert(subj, 1) + st.Match([]byte("."), func(subject []byte, val *int) {}) +}