github-vet / github-vet/rangeloop-pointer-findings
pingcap/tidb: util/expensivequery/memory_usage_alarm.go; 21 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [pingcap/tidb](https://www.github.com/pingcap/tidb) at [util/expensivequery/memory_usage_alarm.go](https://github.com/pingcap/tidb/blob/b1bd2856cabe55b90ef0009fe6bae943a25c5f72/util/expensivequery/memory_usage_alarm.go#L232-L252)
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 item used in defer or goroutine at line 243
[Click here to see the code in its original context.](https://github.com/pingcap/tidb/blob/b1bd2856cabe55b90ef0009fe6bae943a25c5f72/util/expensivequery/memory_usage_alarm.go#L232-L252)
Click here to show the 21 line(s) of Go which triggered the analyzer.
```go
for i, item := range items {
fileName := filepath.Join(record.tmpDir, item.name+record.lastCheckTime.Format(time.RFC3339))
record.lastProfileFileName[i] = append(record.lastProfileFileName[i], fileName)
f, err := os.Create(fileName)
if err != nil {
logutil.BgLogger().Error(fmt.Sprintf("create %v profile file fail", item.name), zap.Error(err))
return
}
defer func() {
err := f.Close()
if err != nil {
logutil.BgLogger().Error(fmt.Sprintf("close %v profile file fail", item.name), zap.Error(err))
}
}()
p := rpprof.Lookup(item.name)
err = p.WriteTo(f, item.debug)
if err != nil {
logutil.BgLogger().Error(fmt.Sprintf("write %v profile file fail", item.name), zap.Error(err))
return
}
}
```
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: b1bd2856cabe55b90ef0009fe6bae943a25c5f72
Contributor guide
No contributing guide indexed for this repository
Research direction
Read util/expensivequery/memory_usage_alarm.go around lines 232-252 and inspect the deferred closure, the range-loop variable item, and the file handle f. Compare the analyzer warning with the shown behavior for each item, then classify the finding as Bug, Mitigated, or Desirable Behavior; done means leaving the appropriate reaction on the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100