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

slice bounds out of range in tree.go #2817

Closed
boindil opened this issue Aug 11, 2021 · 4 comments
Closed

slice bounds out of range in tree.go #2817

boindil opened this issue Aug 11, 2021 · 4 comments

Comments

@boindil
Copy link

boindil commented Aug 11, 2021

Description

I stumbled upon this problem where gin seems trying to extend the slice further than the capacity allows.

I am unsure of the actual reason since my route looks pretty simple and its actually only affecting one of the routes (the others work fine).

The error only occurs, when the route is accessed.

2021/08/11 11:04:37 http: panic serving 10.42.28.200:59776: runtime error: slice bounds out of range [:4] with capacity 3
goroutine 241 [running]:
net/http.(*conn).serve.func1(0xc000366280)
	/usr/lib/go-1.15/src/net/http/server.go:1801 +0x147
panic(0xd26e60, 0xc0015d6be0)
	/usr/lib/go-1.15/src/runtime/panic.go:975 +0x47a
github.com/gin-gonic/gin.(*node).getValue(0xc0001ca380, 0xc00004b140, 0xa, 0xc0015da900, 0x486200, 0x0, 0x0, 0x0, 0xc0015da900, 0x0, ...)
	/build/myservice-0.0.0/obj-x86_64-linux-gnu/pkg/mod/github.com/gin-gonic/gin@v1.7.3/tree.go:481 +0xc65
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0003c5520, 0xc0015aa700)
	/build/myservice-0.0.0/obj-x86_64-linux-gnu/pkg/mod/github.com/gin-gonic/gin@v1.7.3/gin.go:482 +0x1b3
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0003c5520, 0xea7360, 0xc0015ae8c0, 0xc0001e8c00)
	/build/myservice-0.0.0/obj-x86_64-linux-gnu/pkg/mod/github.com/gin-gonic/gin@v1.7.3/gin.go:445 +0x15c
net/http.serverHandler.ServeHTTP(0xc0000f41c0, 0xea7360, 0xc0015ae8c0, 0xc0001e8c00)
	/usr/lib/go-1.15/src/net/http/server.go:2843 +0xa3
net/http.(*conn).serve(0xc000366280, 0xeaf2a0, 0xc0000fef80)
	/usr/lib/go-1.15/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
	/usr/lib/go-1.15/src/net/http/server.go:2969 +0x36c

How to reproduce

unsure, route is called as following: "/servicename/api/v1/:paramname/myresource/:paramname2"

minimal example as to how the routes are built

	routes := router.Group("/servicename")
	privRoutes := routes.Group("/api/v1")
	privRoutesMyResource := privRoutes.Group("/:paramname/myresource")
	privRoutesMyResource .GET("/:paramname2", getResource)

Expectations

No error

Actual result

runrime error

Environment

  • go version: Go 1.15.9
  • gin version (or commit ref): 1.7.3
  • operating system: debian/sid
@jimbirthday
Copy link

@boindil
I'm sorry I can't reproduce your mistake, can you give a more detailed code or use case?

func main() {
	router := gin.Default()
	routes := router.Group("/servicename")
	privRoutes := routes.Group("/api/v1")
	privRoutesMyResource := privRoutes.Group("/:paramname/myresource")
	privRoutesMyResource.GET("/:paramname2", getResource)
	router.Run(":8080")
}

func getResource(c *gin.Context) {
	c.JSON(200, gin.H{
		"dada": "ok",
	})
}

As you can see, I did not appear abnormal

image

@git-hulk
Copy link

Should be the same problem with this PR: #2820

@boindil
Copy link
Author

boindil commented Aug 13, 2021

@jimbirthday @git-hulk would make sense. I probably have a typo in one of my calling services which would be the reason for it only affecting this one route and me not being able to reproduce this right.

Good find, thanks @git-hulk !

@appleboy
Copy link
Member

@boindil OK. I closed the issue.

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

No branches or pull requests

4 participants