github-vet / github-vet/rangeloop-pointer-findings

WunderkindNYC/lbmlp-caddy: caddyhttp/gzip/gzip.go; 52 LoC

Open
#9,630 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [WunderkindNYC/lbmlp-caddy](https://www.github.com/WunderkindNYC/lbmlp-caddy) at [caddyhttp/gzip/gzip.go](https://github.com/WunderkindNYC/lbmlp-caddy/blob/ad4e5371f087952447f9eafc3457d78526596ba9/caddyhttp/gzip/gzip.go#L60-L111)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable c used in defer or goroutine at line 83

[Click here to see the code in its original context.](https://github.com/WunderkindNYC/lbmlp-caddy/blob/ad4e5371f087952447f9eafc3457d78526596ba9/caddyhttp/gzip/gzip.go#L60-L111)

Click here to show the 52 line(s) of Go which triggered the analyzer.

```go
for _, c := range g.Configs {

// Check request filters to determine if gzipping is permitted for this request
for _, filter := range c.RequestFilters {
if !filter.ShouldCompress(r) {
continue outer
}
}

// In order to avoid unused memory allocation, gzip.putWriter only be called when gzip compression happened.
// see https://github.com/mholt/caddy/issues/2395
gz := &gzipResponseWriter{
ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: w},
newWriter: func() io.Writer {
// gzipWriter modifies underlying writer at init,
// use a discard writer instead to leave ResponseWriter in
// original form.
return getWriter(c.Level)
},
}

defer func() {
if gzWriter, ok := gz.internalWriter.(*gzip.Writer); ok {
putWriter(c.Level, gzWriter)
}
}()

var rw http.ResponseWriter
// if no response filter is used
if len(c.ResponseFilters) == 0 {
// replace discard writer with ResponseWriter
if gzWriter, ok := gz.Writer().(*gzip.Writer); ok {
gzWriter.Reset(w)
}
rw = gz
} else {
// wrap gzip writer with ResponseFilterWriter
rw = NewResponseFilterWriter(c.ResponseFilters, gz)
}

// Any response in forward middleware will now be compressed
status, err := g.Next.ServeHTTP(rw, r)

// If there was an error that remained unhandled, we need
// to send something back before gzipWriter gets closed at
// the return of this method!
if status >= 400 {
httpserver.DefaultErrorFunc(w, r, status)
return 0, err
}
return status, err
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: ad4e5371f087952447f9eafc3457d78526596ba9

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.