Skip to content

Commit

Permalink
feat: fix unintended allocation when returning params iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Oct 11, 2024
1 parent e836ae5 commit ee48785
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
17 changes: 14 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,21 @@ func (c *cTx) ClientIP() (*net.IPAddr, error) {

// Params returns an iterator over the matched wildcard parameters for the current route.
func (c *cTx) Params() iter.Seq[Param] {
if c.tsr {
return slices.Values(*c.tsrParams)
return func(yield func(Param) bool) {
if c.tsr {
for _, p := range *c.tsrParams {
if !yield(p) {
return

Check warning on line 210 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L210

Added line #L210 was not covered by tests
}
}
return
}
for _, p := range *c.params {
if !yield(p) {
return
}
}
}
return slices.Values(*c.params)
}

// Param retrieve a matching wildcard segment by name.
Expand Down
8 changes: 7 additions & 1 deletion fox.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ func (r *Route) Path() string {

// Tags returns a range iterator over the tags associated with the route.
func (r *Route) Tags() iter.Seq[string] {
return slices.Values(r.tags)
return func(yield func(string) bool) {
for _, tag := range r.tags {
if !yield(tag) {
return
}
}
}
}

// Tag checks if the specified query exists among the route's tags. It handles multiple wildcards (*) at any position.
Expand Down
14 changes: 14 additions & 0 deletions fox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,20 @@ func TestRouteTagMalloc(t *testing.T) {

}

func TestRouteTagsMalloc(t *testing.T) {
rte := &Route{
tags: []string{"foo:bar:baz", "bar", "boom"},
}

allocs := testing.AllocsPerRun(100, func() {
for tag := range rte.Tags() {
_ = tag
}
})

assert.Equal(t, float64(0), allocs)
}

func TestFuzzRouteTag(t *testing.T) {
rte := &Route{
tags: make([]string, 1),
Expand Down

0 comments on commit ee48785

Please sign in to comment.