Skip to content

Commit ddfce5f

Browse files
[FIXED] More protection against stree being nil (#5662)
There are cases in filestore where errors can leave an stree nil. We report those errors but do not terminate all executions so this protects us generally from the stree being nil. Resolves: #5656 Signed-off-by: Derek Collison <[email protected]>
2 parents b7a3df8 + 179e7e9 commit ddfce5f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

server/stree/stree.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func (t *SubjectTree[T]) Empty() *SubjectTree[T] {
5151

5252
// Insert a value into the tree. Will return if the value was updated and if so the old value.
5353
func (t *SubjectTree[T]) Insert(subject []byte, value T) (*T, bool) {
54+
if t == nil {
55+
return nil, false
56+
}
57+
5458
old, updated := t.insert(&t.root, subject, value, 0)
5559
if !updated {
5660
t.size++
@@ -60,6 +64,10 @@ func (t *SubjectTree[T]) Insert(subject []byte, value T) (*T, bool) {
6064

6165
// Find will find the value and return it or false if it was not found.
6266
func (t *SubjectTree[T]) Find(subject []byte) (*T, bool) {
67+
if t == nil {
68+
return nil, false
69+
}
70+
6371
var si int
6472
for n := t.root; n != nil; {
6573
if n.isLeaf() {
@@ -88,6 +96,10 @@ func (t *SubjectTree[T]) Find(subject []byte) (*T, bool) {
8896

8997
// Delete will delete the item and return its value, or not found if it did not exist.
9098
func (t *SubjectTree[T]) Delete(subject []byte) (*T, bool) {
99+
if t == nil {
100+
return nil, false
101+
}
102+
91103
val, deleted := t.delete(&t.root, subject, 0)
92104
if deleted {
93105
t.size--
@@ -97,7 +109,7 @@ func (t *SubjectTree[T]) Delete(subject []byte) (*T, bool) {
97109

98110
// Match will match against a subject that can have wildcards and invoke the callback func for each matched value.
99111
func (t *SubjectTree[T]) Match(filter []byte, cb func(subject []byte, val *T)) {
100-
if len(filter) == 0 || cb == nil {
112+
if t == nil || t.root == nil || len(filter) == 0 || cb == nil {
101113
return
102114
}
103115
// We need to break this up into chunks based on wildcards, either pwc '*' or fwc '>'.

server/stree/stree_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,14 @@ func TestSubjectTreeMatchNoCallbackDupe(t *testing.T) {
764764
})
765765
}
766766
}
767+
768+
func TestSubjectTreeNilNoPanic(t *testing.T) {
769+
var st *SubjectTree[int]
770+
st.Match([]byte("foo"), func(_ []byte, _ *int) {})
771+
_, found := st.Find([]byte("foo"))
772+
require_False(t, found)
773+
_, found = st.Delete([]byte("foo"))
774+
require_False(t, found)
775+
_, found = st.Insert([]byte("foo"), 22)
776+
require_False(t, found)
777+
}

0 commit comments

Comments
 (0)