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

Panic runtime error: index out of range in context.go:108 #1678

Closed
Collapsik opened this issue Dec 3, 2018 · 5 comments
Closed

Panic runtime error: index out of range in context.go:108 #1678

Collapsik opened this issue Dec 3, 2018 · 5 comments

Comments

@Collapsik
Copy link

After commit b97ccf3 I catch panic that never been before. In my case length of c.handlers equal 1 and c.index equal 1.

image_2018-12-03_11-55-57

@thinkerou thinkerou added the bug label Dec 4, 2018
@thinkerou
Copy link
Member

@Collapsik can you post your test code? I use it reproduce the issue, thanks!

@kirides
Copy link

kirides commented Dec 7, 2018

I get the same / a simmilar issue. It appears to happen after my handler returned

Did not happen before, but i cannot pin in down to a specific version, was, about August when i first installed gin-gonic, and just updated all my stuff on monday using go get -u all

Code: https://pastebin.com/kM6UwyB3

Stacktrace:

2018/12/07 09:34:55 http: panic serving 192.168.50.34:63110: runtime error: index out of range
goroutine 23 [running]:
net/http.(*conn).serve.func1(0xc00016b180)
        C:/Go/src/net/http/server.go:1746 +0xd7
panic(0xa254e0, 0xf9e740)
        C:/Go/src/runtime/panic.go:513 +0x1c7
github.com/gin-gonic/gin.(*Context).Next(0xc0001ec210)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/context.go:109 +0x87
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0000a9680, 0xc0001ec210)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/gin.go:383 +0x5b2
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0000a9680, 0xbb20a0, 0xc0001e8380, 0xc000100900)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/gin.go:349 +0x139
net/http.serverHandler.ServeHTTP(0xc00006be10, 0xbb20a0, 0xc0001e8380, 0xc000100900)
        C:/Go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00016b180, 0xbb2aa0, 0xc0001b83c0)
        C:/Go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
        C:/Go/src/net/http/server.go:2851 +0x2fc
2018/12/07 09:34:55 http: panic serving 192.168.50.34:63113: runtime error: index out of range
goroutine 24 [running]:
net/http.(*conn).serve.func1(0xc00016b220)
        C:/Go/src/net/http/server.go:1746 +0xd7
panic(0xa254e0, 0xf9e740)
        C:/Go/src/runtime/panic.go:513 +0x1c7
github.com/gin-gonic/gin.(*Context).Next(0xc0000c04d0)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/context.go:109 +0x87
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0000a9680, 0xc0000c04d0)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/gin.go:383 +0x5b2
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0000a9680, 0xbb20a0, 0xc0001d82a0, 0xc000100a00)
        C:/Users/USERNAME/go/src/github.com/gin-gonic/gin/gin.go:349 +0x139
net/http.serverHandler.ServeHTTP(0xc00006be10, 0xbb20a0, 0xc0001d82a0, 0xc000100a00)
        C:/Go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00016b220, 0xbb2aa0, 0xc000178980)
        C:/Go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
        C:/Go/src/net/http/server.go:2851 +0x2fc

@thinkerou
Copy link
Member

Sorry, I still can't reproduce it.

@vkd
Copy link
Contributor

vkd commented Dec 26, 2018

I have reproduced it.

It happens because during func (c *Context) Next() { method it iterates over c.handlers with precalculated size of c.handlers.

func (c *Context) Next() {
	c.index++
	for s := int8(len(c.handlers)); c.index < s; c.index++ {
		c.handlers[c.index](c)
	}
}

But func (group *RouterGroup) StaticFS(...) ... { rewrite c.handlers to slice with other size than we calculated in c.Next(...) (https://github.com/gin-gonic/gin/blob/master/routergroup.go#L198)

func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
	...
	return func(c *Context) {
		...
		// Check if file exists and/or if we have permission to access it
		if _, err := fs.Open(file); err != nil {
			c.Writer.WriteHeader(http.StatusNotFound)
			c.handlers = group.engine.allNoRoute
			// Reset index
			c.index = -1
			return
		}

		fileServer.ServeHTTP(c.Writer, c.Request)
	}
}

@thinkerou
Copy link
Member

#1744

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

No branches or pull requests

4 participants