With HandleMethodNotAllowed enabled, handleHTTPRequest searches every
method tree to build the Allow response header. It reuses the pooled
Context's skippedNodes slice across all getValue calls in that loop.
getValue extends the slice with a raw reslice:
*skippedNodes = (*skippedNodes)[:index+1] // tree.go:435
The slice has a fixed capacity equal to engine.maxSections (allocated
once when the Context is created). Context.reset() zeroes the length at
the start of each request, but nothing zeroes it between getValue calls
inside the HandleMethodNotAllowed loop. With enough wildcard routes,
residual entries from earlier trees fill the slice until the reslice
exceeds capacity and panics:
panic: runtime error: slice bounds out of range [:7] with capacity 6
Because the panic fires during route matching — before any handler
runs — gin.Recovery() cannot intercept it. net/http's per-connection
recover logs "http: panic serving …" and closes the connection with
no response; callers see an empty reply or a connection reset.
Fix: zero the slice length before each getValue call in the loop.
This matches the same reset Context.reset() performs at request start
and costs only a single pointer write per method tree.
Add TestMethodNotAllowedSkippedNodesPanic reproducing the exact route
set and request from issue #4818 to guard against regression.
Fixes#4818
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* perf: replace regex with custom functions in redirectTrailingSlash
* perf: use more efficient removeRepeatedChar for path slash handling
---------
Co-authored-by: 1911860538 <alxps1911@gmail.com>
* fix: call updateRouteTrees in ServeHTTP using sync.Once to support literal colon routes in all usage scenarios (#4413)
* chore: fixed golangci-lint issue in test cases for literal colon
* fix: gofumpt formatting issue
* fix: gofumpt issue in gin.go
* chore: updated routeTreesUpdated comments
* chore: removed unused variable and updated TestUpdateRouteTreesCalledOnce testcase
* chore: moved tests from literal_colon_test.go into gin_test.go
---------
Co-authored-by: pawannn <pawan@zenz.tech>
- Update linting configuration to exclude G115 gosec check instead of including specific checks
- Add the safeInt8 helper for safer type conversions and use it to prevent int8 overflow in middleware handler execution
- Group related constants and variables together for better organization in gin.go
- Refactor HTTP server instantiation to use a dedicated http.Server object for all Run methods
- Add the safeUint16 helper and use it to safely handle conversions in tree node functions to prevent uint16 overflow
Signed-off-by: appleboy <appleboy.tw@gmail.com>
* ci: update golangci-lint configuration and lint settings
- Update golangci-lint to version 2
- Enable new linters and adjust existing ones
- Update lint settings across multiple test files
- Remove unused struct and variable checks
- Add new lint exclusions for generated code and specific directories
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
* ci(github): update golangci-lint-action to v8 and lint version to v2.3.4
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
* ci: downgrade golangci-lint to v2.1.6
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
* ci(golangci): add gofumpt linter and fix related issues- Added gofumpt linter to .golangci.yml
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
* test: ignore testifylint and gofumpt lints in specific test cases
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
* build(deps): remove golang.org/x/lint
- Remove golang.org/x/lint package from go.mod
- Update related dependencies in go.sum
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* build(deps): downgrade golang.org/x/mod and golang.org/x/tools
- Downgrade golang.org/x/mod from v0.24.0 to v0.18.0
- Downgrade golang.org/x/tools from v0.33.0 to v.22.0
These changes are made to address compatibility issues with the current project setup.
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
---------
Signed-off-by: Flc <four_leaf_clover@foxmail.com>
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* experimental support http3
* remove go1.14 and go1.15
* update quic-go package path
* only support go1.19+
* remove go19 support
* update gomod
* chore: refine CI configuration and dependencies
- Remove dynamic Go versioning in favor of pinning to major version `1`
- Update linter version from `v1.56.2` to `v1.58.1` in GitHub Actions workflow
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* chore: refactor CI workflow and improve tests
- Update the golangci-lint-action version from `v5` to `v6` in the GitHub workflow configuration
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* chore: update dependencies and CI configurations
- Update Go version requirement from `1.20` to `1.21` in `go.mod`
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* style: refactor codebase and update tests
- Add an empty line in the import section of `gin.go`
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* chore: enhance code quality and consistency
- Add `gin.go` to the list of files with specific linters in `.golangci.yml`, applying the `gci` linter.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
---------
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>