Skip to content

Commit 3a4e181

Browse files
authored
Fix panic on nil ctx in radix code (#26)
We ran into this when looking up a path with parameters using a method other than the one specified. Closes #25
1 parent 637e8c0 commit 3a4e181

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

radix/node.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ walk:
303303
if tsr {
304304
return nil, tsr
305305
} else if h != nil {
306-
for i, key := range child.paramKeys {
307-
ctx.SetUserValue(key, values[i])
306+
if ctx != nil {
307+
for i, key := range child.paramKeys {
308+
ctx.SetUserValue(key, values[i])
309+
}
308310
}
309311

310312
return h, false

radix/tree_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,31 @@ func generateHandler() fasthttp.RequestHandler {
2020
func testHandlerAndParams(
2121
t *testing.T, tree *Tree, requestedPath string, handler fasthttp.RequestHandler, wantTSR bool, params map[string]interface{},
2222
) {
23-
ctx := new(fasthttp.RequestCtx)
23+
for _, ctx := range []*fasthttp.RequestCtx{new(fasthttp.RequestCtx), nil} {
2424

25-
h, tsr := tree.Get(requestedPath, ctx)
26-
if reflect.ValueOf(handler).Pointer() != reflect.ValueOf(h).Pointer() {
27-
t.Errorf("Path '%s' handler == %p, want %p", requestedPath, h, handler)
28-
}
25+
h, tsr := tree.Get(requestedPath, ctx)
26+
if reflect.ValueOf(handler).Pointer() != reflect.ValueOf(h).Pointer() {
27+
t.Errorf("Path '%s' handler == %p, want %p", requestedPath, h, handler)
28+
}
2929

30-
if wantTSR != tsr {
31-
t.Errorf("Path '%s' tsr == %v, want %v", requestedPath, tsr, wantTSR)
32-
}
30+
if wantTSR != tsr {
31+
t.Errorf("Path '%s' tsr == %v, want %v", requestedPath, tsr, wantTSR)
32+
}
3333

34-
resultParams := make(map[string]interface{})
35-
if params == nil {
36-
params = make(map[string]interface{})
37-
}
34+
if ctx != nil {
35+
resultParams := make(map[string]interface{})
36+
if params == nil {
37+
params = make(map[string]interface{})
38+
}
3839

39-
ctx.VisitUserValues(func(key []byte, value interface{}) {
40-
resultParams[string(key)] = value
41-
})
40+
ctx.VisitUserValues(func(key []byte, value interface{}) {
41+
resultParams[string(key)] = value
42+
})
4243

43-
if !reflect.DeepEqual(resultParams, params) {
44-
t.Errorf("User values == %v, want %v", resultParams, params)
44+
if !reflect.DeepEqual(resultParams, params) {
45+
t.Errorf("User values == %v, want %v", resultParams, params)
46+
}
47+
}
4548
}
4649
}
4750

0 commit comments

Comments
 (0)