Skip to content

Commit ea53388

Browse files
authored
fix(tree): Keep panic infos consistent when wildcard type build faild (#4077)
1 parent 9d11234 commit ea53388

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Diff for: .github/workflows/gin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Setup golangci-lint
2727
uses: golangci/golangci-lint-action@v6
2828
with:
29-
version: v1.58.1
29+
version: v1.61.0
3030
args: --verbose
3131
test:
3232
needs: lint

Diff for: tree.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
369369

370370
// currently fixed width 1 for '/'
371371
i--
372-
if path[i] != '/' {
372+
if i < 0 || path[i] != '/' {
373373
panic("no / before catch-all in path '" + fullPath + "'")
374374
}
375375

Diff for: tree_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,28 @@ func TestTreeInvalidEscape(t *testing.T) {
993993
}
994994
}
995995
}
996+
997+
func TestWildcardInvalidSlash(t *testing.T) {
998+
const panicMsgPrefix = "no / before catch-all in path"
999+
1000+
routes := map[string]bool{
1001+
"/foo/bar": true,
1002+
"/foo/x*zy": false,
1003+
"/foo/b*r": false,
1004+
}
1005+
1006+
for route, valid := range routes {
1007+
tree := &node{}
1008+
recv := catchPanic(func() {
1009+
tree.addRoute(route, nil)
1010+
})
1011+
1012+
if recv == nil != valid {
1013+
t.Fatalf("%s should be %t but got %v", route, valid, recv)
1014+
}
1015+
1016+
if rs, ok := recv.(string); recv != nil && (!ok || !strings.HasPrefix(rs, panicMsgPrefix)) {
1017+
t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsgPrefix, route, recv)
1018+
}
1019+
}
1020+
}

0 commit comments

Comments
 (0)