From e43b12e34322b0f8834265cb9bafbafd880dc421 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 1 Aug 2025 00:49:53 +0000 Subject: [PATCH 1/2] fix: correct shallow copy in router backtracking The router could panic with a 'slice bounds out of range' error when handling requests that trigger its backtracking logic. The root cause was a shallow copy of the node's `children` slice when creating a `skippedNode` for backtracking. This could lead to a corrupted state if the router needed to backtrack and then proceed down a wildcard path. This commit fixes the issue by introducing a `copyChildren` method on the `node` struct, which creates a safe copy of the children slice. This method is now used when creating a `skippedNode`, ensuring that the backtracking logic is isolated and robust. --- tree.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tree.go b/tree.go index 6f99223..1082629 100644 --- a/tree.go +++ b/tree.go @@ -293,6 +293,12 @@ walk: // 外部循环用于遍历和构建路由树 } } +func (n *node) copyChildren() []*node { + children := make([]*node, len(n.children)) + copy(children, n.children) + return children +} + // findWildcard 搜索通配符段并检查名称是否包含无效字符。 // 如果未找到通配符,则返回 -1 作为索引。 func findWildcard(path string) (wildcard string, i int, valid bool) { @@ -486,7 +492,7 @@ walk: // 外部循环用于遍历路由树 wildChild: n.wildChild, nType: n.nType, priority: n.priority, - children: n.children, + children: n.copyChildren(), handlers: n.handlers, fullPath: n.fullPath, }, From 783370fd7918b3416f3e3ed967ce479e3d8790e7 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Fri, 1 Aug 2025 09:09:46 +0800 Subject: [PATCH 2/2] update --- tree.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tree.go b/tree.go index 1082629..f78f1d1 100644 --- a/tree.go +++ b/tree.go @@ -489,6 +489,7 @@ walk: // 外部循环用于遍历路由树 path: prefix + path, // 记录跳过的路径 node: &node{ // 复制当前节点的状态 path: n.path, + indices: n.indices, wildChild: n.wildChild, nType: n.nType, priority: n.priority,