Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ When `-check-consumption` is enabled, the following patterns are recognized as v
- `json.NewDecoder(resp.Body)`
- `bufio.NewScanner(resp.Body)`
- `bufio.NewReader(resp.Body)`
- `gzip.NewReader(resp.Body)`

##### Limitations and False Positives

Expand Down
3 changes: 2 additions & 1 deletion passes/bodyclose/bodyclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ func (r *runner) isConsumptionFunction(call *ssa.Call) bool {
if (pkg == "io" && (name == "Copy" || name == "ReadAll")) ||
(pkg == "io/ioutil" && name == "ReadAll") ||
(pkg == "encoding/json" && name == "NewDecoder") ||
(pkg == "bufio" && (name == "NewScanner" || name == "NewReader")) {
(pkg == "bufio" && (name == "NewScanner" || name == "NewReader")) ||
(pkg == "compress/gzip" && name == "NewReader") {
return true
}
}
Expand Down
17 changes: 17 additions & 0 deletions passes/bodyclose/testdata/src/consumption/consumption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consumption

import (
"bufio"
"compress/gzip"
"encoding/json"
"io"
"io/ioutil"
Expand Down Expand Up @@ -80,6 +81,22 @@ func consumedWithBufioReader() {
_, _, _ = reader.ReadLine()
}

// Pattern 7: gzip.NewReader
func consumedWithGzipReader() {
resp, err := http.Get("http://example.com/") // OK - gzip.NewReader detected
if err != nil {
return
}
defer resp.Body.Close()

reader, err := gzip.NewReader(resp.Body)
if err != nil {
return
}

reader.Close()
}

// Pattern 7: Helper function with known consumption
func consumedInHelper() {
resp, err := http.Get("http://example.com/") // OK - helper uses io.Copy
Expand Down