Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some 'index out of range' error in tree.go #3796

Closed
cainmusic opened this issue Dec 8, 2023 · 2 comments · Fixed by #3812
Closed

Some 'index out of range' error in tree.go #3796

cainmusic opened this issue Dec 8, 2023 · 2 comments · Fixed by #3812
Labels

Comments

@cainmusic
Copy link

Router tree bug

when add a catch-all router, there may be an error reported:

panic: runtime error: index out of range [0] with length 0

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/static/", func(c *gin.Context) { c.String(200, "static") })
	r.GET("/static/*file", func(c *gin.Context) { c.String(200, "static file") })

	r.Run()
}

Source code

tree.go

		if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
			pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]
			panic("catch-all wildcard '" + path +
				"' in new path '" + fullPath +
				"' conflicts with existing path segment '" + pathSeg +
				"' in existing prefix '" + n.path + pathSeg +
				"'")
		}

this line pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]

actually, n node do not have any children.

Environment

  • go version: go1.20
  • gin version (or commit ref): v1.9.1
  • operating system: mac os
@cainmusic
Copy link
Author

i thought:
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' { should be i == 0
n.children[0].path should be n.path

@FirePing32
Copy link
Contributor

The below line expects that the path will end with a wildcard name.

gin/tree.go

Line 354 in 53fbf4d

pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]

/static/*file is already a catch-all wildcard. You are trying to initialize a path /static/ (notice the trailing slash). This path is already a type of catch-all wildcard with no child paths, which leads to the conflict.
WIll open a PR by today EOD to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants