fix(routing): guarantee rune-boundary safety during wildcard parameter slicing#4696
fix(routing): guarantee rune-boundary safety during wildcard parameter slicing#4696HarshalPatel1972 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens route parameter extraction for Unicode paths by ensuring slice boundaries align with UTF-8 rune starts, and adds a concurrent regression test to exercise Unicode params/wildcards under load.
Changes:
- Adjust param slicing to back up
endto a valid UTF-8 rune boundary beforepath[:end]. - Add a concurrency-focused test that hits
:nameand*filepathroutes with Unicode paths.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tree.go | Ensures end is moved to a UTF-8 rune boundary prior to slicing route params. |
| context_test.go | Adds a multi-goroutine test covering Unicode param/wildcard routing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| router.GET("/user/:name", func(c *Context) { | ||
| name := c.Param("name") | ||
| assert.NotEmpty(t, name) | ||
| }) | ||
|
|
||
| router.GET("/files/*filepath", func(c *Context) { | ||
| filepath := c.Param("filepath") | ||
| assert.NotEmpty(t, filepath) | ||
| }) |
| go func() { | ||
| defer wg.Done() | ||
| for _, p := range paths { | ||
| req, _ := http.NewRequest(http.MethodGet, p, nil) | ||
| w := httptest.NewRecorder() | ||
| router.ServeHTTP(w, req) | ||
| assert.Equal(t, http.StatusOK, w.Code) | ||
| } | ||
| }() |
| go func() { | ||
| defer wg.Done() | ||
| for _, p := range paths { | ||
| req, _ := http.NewRequest(http.MethodGet, p, nil) |
…and clean format rules
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4696 +/- ##
==========================================
- Coverage 99.21% 98.32% -0.90%
==========================================
Files 42 48 +6
Lines 3182 3162 -20
==========================================
- Hits 3157 3109 -48
- Misses 17 43 +26
- Partials 8 10 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
517d1ed to
3a0de75
Compare
3a0de75 to
51a1efd
Compare
Fixes #3654
Summary
Implements a high-performance,$O(1)$ bitwise rune-boundary alignment safety gate inside the Radix Tree routing engine (
tree.go) to prevent string slicing truncation and out-of-bounds panics when extracting wildcard parameters from paths containing multi-byte UTF-8 character sequences.Changes
utf8.RuneStartindex alignment routine immediately prior to executing parameter value slices (path[:end]) insidegetValueto catch and correct any index drift caused by unescaping or route fallback handling.TestWildcardParamUnicodeConcurrencyinsidecontext_test.goto aggressively validate parameter parsing accuracy across concurrent goroutines using complex multi-byte scripts and emojis.Motivation
Gin's radix tree naturally tracks path splits using raw byte counters for raw execution speed. However, if index parameters drift under complex unescaping edge cases or URL structural mutations, standard string slicing can cut a multi-byte character directly in half. This leads to malformed parameter buffers or runtime memory exceptions.
Using bitwise checks keeps the validation at an$O(1)$ footprint, ensuring Gin's routing performance metrics are completely preserved without forcing heap arrays or full string conversion allocations.
Tested on
go test -race ./...). All concurrent tests pass cleanly with zero allocation regressions.