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

maksim77/tinkoff_exporter: tinkoff/model.go; 97 LoC

Open
#14,463 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 [maksim77/tinkoff_exporter](https://www.github.com/maksim77/tinkoff_exporter) at [tinkoff/model.go](https://github.com/maksim77/tinkoff_exporter/blob/6d54d2ef1b88b888f0958ba2c99c734cc6d33bdd/tinkoff/model.go#L113-L209)

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 accountName used in defer or goroutine at line 164

[Click here to see the code in its original context.](https://github.com/maksim77/tinkoff_exporter/blob/6d54d2ef1b88b888f0958ba2c99c734cc6d33bdd/tinkoff/model.go#L113-L209)

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

```go
for accountName, accountID := range c.accountIDs {
portfolio, err := getPortfolio(accountID)
if err != nil {
log.Errorf("Cannot get portfolio: %s", err)
return
}

total, err := getTotal(portfolio)

if err != nil {
log.Errorf("Get total error: %s", err)
return
}

ch <- prometheus.MustNewConstMetric(c.totalAmountDesc, prometheus.GaugeValue, total, string(accountName))

for _, p := range portfolio.Positions {
var value float64

lastPrice, err := getLastPrice(p.FIGI)
if err != nil {
log.Errorf("Get last price error: %s", err)
return
}

switch p.InstrumentType {
case "Bond":
value = lastPrice
default:
value = lastPrice
}

ch <- prometheus.MustNewConstMetric(c.stockPriceDesc,
prometheus.GaugeValue,
value,
string(p.InstrumentType), p.Ticker, string(p.ExpectedYield.Currency), "1", string(accountName))
ch <- prometheus.MustNewConstMetric(c.stockCountDesc,
prometheus.GaugeValue,
p.Balance,
string(p.InstrumentType), p.Ticker, string(accountName))
ch <- prometheus.MustNewConstMetric(c.stockExpectedYieldDesc,
prometheus.GaugeValue,
p.ExpectedYield.Value,
string(p.InstrumentType), p.Ticker, string(p.ExpectedYield.Currency), string(accountName)) //TODO Обработать разные валюты
}

for _, currency := range portfolio.Currencies {
wg.Add(1)

go func(cb sdk.CurrencyBalance, ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.currencyDesc,
prometheus.GaugeValue, cb.Balance, string(cb.Currency), string(accountName))
ch <- prometheus.MustNewConstMetric(c.currencyBlockedDesc,
prometheus.GaugeValue, cb.Blocked, string(cb.Currency), string(accountName))
wg.Done()
}(currency, ch)
}

hist, err := getHistory(accountID)

if err != nil {
log.Errorf("Get history error: %s", err)
return
}

ch <- prometheus.MustNewConstMetric(c.totalPayInDesc, prometheus.GaugeValue, getPayIn(hist), string(accountName))
ch <- prometheus.MustNewConstMetric(c.totalPayOutDesc, prometheus.GaugeValue, getPayOut(hist), string(accountName))

xirr := getXirr(hist, total)

ch <- prometheus.MustNewConstMetric(c.xirrDesc, prometheus.GaugeValue, xirr, string(accountName))

tickers := viper.GetStringSlice("tickers")
for _, t := range tickers {
wg.Add(1)

go func(t string, ch chan<- prometheus.Metric) {
f, err := getFigi(t)
if err != nil {
log.Errorf("Get FIGI error: %s", err)
return
}

price, err := getLastPrice(f.FIGI)
if err != nil {
log.Errorf("Get last price error: %s", err)
return
}

ch <- prometheus.MustNewConstMetric(c.stockPriceDesc, prometheus.GaugeValue, price, "Stock", t, string(f.Currency), "0", string(accountName))

wg.Done()
}(t, ch)
}

wg.Wait()
}

```

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: 6d54d2ef1b88b888f0958ba2c99c734cc6d33bdd

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.