Repository navigation
fix(router): removing a route with a custom method panics - #3160
Merged
Merged
Conversation
Router.Remove clears the handler by calling setHandler(method, nil). For a method without its own field, routeMethods.set then read r.handler on the nil route and panicked. Treat a nil route like a nil handler and delete the entry.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The narrow nil guard fixes the panic while preserving existing behavior, with regression coverage for both removal outcomes.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes the Echo v5 router panic when removing a route registered with a custom HTTP method.
Changes:
- Handles nil routes before accessing their handlers.
- Adds regression coverage for removal with and without another route remaining.
| File | Description |
|---|---|
| router.go | Guards against nil routes when deleting custom methods. |
| router_test.go | Verifies route listings, 405/404 responses, and the Allow header after removal. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Router.Removepanics with a nil pointer dereference for a route registered with a custom method, for examplee.Add("PURGE", "/cache", h)followed bye.Router().Remove("PURGE", "/cache").ConcurrentRouter.Removedelegates to it, so it panics too.Cause:
Removeclears the handler withsetHandler(method, nil). Methods with their own field (GET, POST, …) just set it to nil. Custom methods fall through to thedefaultbranch ofrouteMethods.set, which readr.handleron the nil route.Fix: treat a nil route like a nil handler and delete the
anyOtherentry.Removeis the only caller that passes nil.Test:
TestDefaultRouter_RemoveCustomMethodcovers two cases:It panics on master and passes here.
go test -race ./...,go vet,staticcheckandgolintpass.v4 has no
Router.Remove, so this is v5 only.