Merge pull request #79 from infinite-iroha/fix/v1-findcaseinsensitivepath-wildchild-order

fix: avoid panic in case-insensitive wildcard lookup
This commit is contained in:
WJQSERVER 2026-04-07 07:25:28 +08:00 committed by GitHub
commit 7f69d5668e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View file

@ -852,7 +852,7 @@ walk: // 外部循环用于遍历路由树
return nil // 未找到, 返回 nil
}
n = n.children[0] // 移动到通配符子节点(通常是唯一一个)
n = n.children[len(n.children)-1] // 通配符子节点约定始终位于末尾
switch n.nType {
case param: // 参数节点
// 查找参数结束位置('/' 或路径末尾)

View file

@ -901,6 +901,34 @@ func TestTreeInvalidNodeType(t *testing.T) {
}
}
func TestFindCaseInsensitivePathWithStaticAndParamRoutesDoesNotPanicOnMiss(t *testing.T) {
tree := &node{}
routes := [...]string{
"/:user/:repo/info/refs",
"/healthz",
"/api/db/data",
"/api/db/sum",
}
for _, route := range routes {
tree.addRoute(route, fakeHandler(route))
}
defer func() {
if r := recover(); r != nil {
t.Fatalf("unexpected panic while looking up missing path: %v", r)
}
}()
if out, found := tree.findCaseInsensitivePath("/does-not-exist", true); found || out != nil {
t.Fatalf("expected missing path lookup to return no match, got %q, %t", string(out), found)
}
if out, found := tree.findCaseInsensitivePath("/does-not-exist", false); found || out != nil {
t.Fatalf("expected missing path lookup without trailing slash fix to return no match, got %q, %t", string(out), found)
}
}
func TestTreeInvalidParamsType(t *testing.T) {
tree := &node{}
// add a child with wildcard