Fix params would out of range with the wrong latest node

getValue didn't update the latest node after walking through
the path and may regard the static route as the param type,
but the params cap was prealloc and cause out of range.

For Example:

the route was "/a/b/:c/d" and we send the request path "/a/b/c/d1",
the params cap was 1 but the `d` would also be regarded as param then
would be out of range.
This commit is contained in:
git-hulk 2021-08-13 13:10:29 +08:00
parent b463b1c2a1
commit 7a16098f38
2 changed files with 9 additions and 3 deletions

View File

@ -496,6 +496,7 @@ walk: // Outer loop for walking the tree
if len(n.children) > 0 { if len(n.children) > 0 {
path = path[end:] path = path[end:]
n = n.children[0] n = n.children[0]
latestNode = n
continue walk continue walk
} }

View File

@ -28,8 +28,12 @@ type testRequests []struct {
ps Params ps Params
} }
func getParams() *Params { func getParams(path string) *Params {
ps := make(Params, 0, 20) paramCnt := countParams(path)
if paramCnt == 0 {
paramCnt = 20
}
ps := make(Params, 0, paramCnt)
return &ps return &ps
} }
@ -40,7 +44,7 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ..
} }
for _, request := range requests { for _, request := range requests {
value := tree.getValue(request.path, getParams(), unescape) value := tree.getValue(request.path, getParams(request.path), unescape)
if value.handlers == nil { if value.handlers == nil {
if !request.nilHandler { if !request.nilHandler {
@ -261,6 +265,7 @@ func TestTreeWildcard(t *testing.T) {
{"/c/d/e/f/gg", false, "/:cc/:dd/:ee/:ff/gg", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}}}, {"/c/d/e/f/gg", false, "/:cc/:dd/:ee/:ff/gg", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}}},
{"/c/d/e/f/g/hh", false, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}, Param{Key: "gg", Value: "g"}}}, {"/c/d/e/f/g/hh", false, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}, Param{Key: "gg", Value: "g"}}},
{"/cc/dd/ee/ff/gg/hh", false, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "cc"}, Param{Key: "dd", Value: "dd"}, Param{Key: "ee", Value: "ee"}, Param{Key: "ff", Value: "ff"}, Param{Key: "gg", Value: "gg"}}}, {"/cc/dd/ee/ff/gg/hh", false, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "cc"}, Param{Key: "dd", Value: "dd"}, Param{Key: "ee", Value: "ee"}, Param{Key: "ff", Value: "ff"}, Param{Key: "gg", Value: "gg"}}},
{"/cc/dd/ee/ff/gg/hh1", true, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "cc"}, Param{Key: "dd", Value: "dd"}, Param{Key: "ee", Value: "ee"}, Param{Key: "ff", Value: "ff"}, Param{Key: "gg", Value: "gg"}}},
{"/get/abc", false, "/get/abc", nil}, {"/get/abc", false, "/get/abc", nil},
{"/get/a", false, "/get/:param", Params{Param{Key: "param", Value: "a"}}}, {"/get/a", false, "/get/:param", Params{Param{Key: "param", Value: "a"}}},
{"/get/abz", false, "/get/:param", Params{Param{Key: "param", Value: "abz"}}}, {"/get/abz", false, "/get/:param", Params{Param{Key: "param", Value: "abz"}}},