gin
gin copied to clipboard
Panic in context .Value() When Key Not Found and .engine is nil
Description
After upgrading to 1.9.0 (from 1.5.0), our test code is now crashing here in context.go line 1216:
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
The (private) variable engine is nil, hence the crash.
How to reproduce
Can't easily boil down our legacy code to a small example.
Expectations
If there's no Value in a context, return nil instead of panicking.
Actual result
Panic:
goroutine 49 [running]:
testing.tRunner.func1.2({0x18df8c0, 0x20f1f00})
/usr/local/go/src/testing/testing.go:1396 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1399 +0x39f
panic({0x18df8c0, 0x20f1f00})
/usr/local/go/src/runtime/panic.go:884 +0x212
XXX/gin-gonic/gin.(*Context).Value(0xc0003ba100, {0x1891280?, 0x1b42dd0?})
XXX/vendor/github.com/gin-gonic/gin/context.go:1216 +0xfa
XXX.GetLoggerFromContext({0x1b4e018, 0xc0003ba100})
Environment
- go version: 1.20 and 1.19.7
- gin version (or commit ref): v1.9.0
- operating system: Darwin amd64
Note that if I change that one line to this, our tests pass:
if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
Fix proposed here, but not merged yet: https://github.com/gin-gonic/gin/pull/3512