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

goex-top/cryptoassets: exchange.go; 170 LoC

Open
#8,267 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [goex-top/cryptoassets](https://www.github.com/goex-top/cryptoassets) at [exchange.go](https://github.com/goex-top/cryptoassets/blob/66ff55f96c2e8eb9065c041d41c91e77922e6d07/exchange.go#L164-L333)

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 ex used in defer or goroutine at line 169

[Click here to see the code in its original context.](https://github.com/goex-top/cryptoassets/blob/66ff55f96c2e8eb9065c041d41c91e77922e6d07/exchange.go#L164-L333)

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

```go
for _, ex := range exs {
wg.Add(1)
go func() {
defer wg.Done()
coins := make(map[string]CoinAsset)
if ex.spot != nil {
acc, err := ex.spot.GetAccount()
if err != nil {
return
}
for _, sub := range acc.SubAccounts {
total := sub.Amount + sub.ForzenAmount
if total == 0 {
continue
}
coin := CoinAsset{
CoinName: sub.Currency.String(),
Amount: sub.Amount,
FrozenAmount: sub.ForzenAmount,
}
if sub.Currency == goex.USDT {
if btcusdt != 0 {
coin.Btc = total / btcusdt
}

coin.Usdt = total
if usdtusd != 0 {
coin.Usd = total / usdtusd
}

if usdtcny != 0 {
coin.Cny = total * usdtcny
}

} else if sub.Currency == goex.BTC {
coin.Btc = total
coin.Usdt = total * btcusdt
coin.Usd = total * btcusd
coin.Cny = total * btccny
} else {
usdt_pair := goex.NewCurrencyPair(sub.Currency, goex.USDT)
btc_pair := goex.NewCurrencyPair(sub.Currency, goex.BTC)
usdt_ticker, err := ex.spot.GetTicker(usdt_pair)

if err != nil {
btc_ticker, err := ex.spot.GetTicker(btc_pair)
if err == nil {
btc := total * btc_ticker.Last
coin.Btc = btc
coin.Usdt = btc * btcusdt
coin.Usd = btc * btcusd
coin.Cny = btc * btccny
} else {
logger.Printf("coin: [%s] not ticker for value caculate", sub.Currency.String())
}
} else {
usdt := total * usdt_ticker.Last
if btcusdt != 0 {
coin.Btc = usdt / btcusdt
}

coin.Usdt = usdt

if usdtusd != 0 {
coin.Usd = usdt / usdtusd
}

if usdtcny != 0 {
coin.Cny = usdt * usdtcny
}
}
}
coins[sub.Currency.String()] = coin
}
}

if len(ex.future) > 0 {
for _, future := range ex.future {
if future == nil {
continue
}
acc, err := future.GetFutureUserinfo()
if err != nil {
continue
}
for _, sub := range acc.FutureSubAccounts {
total := sub.AccountRights // todo: pnl, unpnl
coin := CoinAsset{
CoinName: sub.Currency.String(),
Amount: total,
FrozenAmount: 0,
}
if sub.Currency == goex.USDT {
coin.Btc = total / btcusdt
coin.Usdt = total
coin.Usd = total / usdtusd
coin.Cny = total * usdtcny
} else if sub.Currency == goex.BTC {
coin.Btc = total
coin.Usdt = total * btcusdt
coin.Usd = total * btcusd
coin.Cny = total * btccny
} else {
usdt_pair := goex.NewCurrencyPair(sub.Currency, goex.USDT)
btc_pair := goex.NewCurrencyPair(sub.Currency, goex.BTC)
usdt_ticker, err := ex.spot.GetTicker(usdt_pair)

if err != nil {
btc_ticker, err := ex.spot.GetTicker(btc_pair)
if err == nil {
btc := total * btc_ticker.Last
coin.Btc = btc
coin.Usdt = btc * btcusdt
coin.Usd = btc * btcusd
coin.Cny = btc * btccny
} else {
logger.Printf("coin: [%s] not ticker for value caculate", sub.Currency.String())
}
} else {
usdt := total * usdt_ticker.Last
coin.Btc = usdt / btcusdt
coin.Usdt = usdt
coin.Usd = usdt / usdtusd
coin.Cny = usdt * usdtcny
}
}
asset, ok := coins[sub.Currency.String()]
if ok {
asset.Amount += coin.Amount
asset.FrozenAmount += coin.FrozenAmount
asset.Btc += coin.Btc
asset.Usdt += coin.Usdt
asset.Usd += coin.Usd
asset.Cny += coin.Cny
coins[sub.Currency.String()] = asset
} else {
coins[sub.Currency.String()] = coin
}
}
}
}

asset := Asset{
AccountID: ex.accountId,
Btc: 0,
Usdt: 0,
Usd: 0,
Cny: 0,
Btc_Usdt: btcusdt,
Btc_Usd: btcusd,
Btc_Cny: btccny,
Usdt_Usd: usdtusd,
Usdt_Cny: usdtcny,
Usd_Cny: usdcny,
}
coinassets := make([]CoinAsset, 0)
for _, c := range coins {
asset.Btc += c.Btc
asset.Usdt += c.Usdt
asset.Usd += c.Usd
asset.Cny += c.Cny
coinassets = append(coinassets, c)
}
asset = orm.AddAsset(asset)
for k := range coinassets {
coinassets[k].AssetID = asset.ID
}
orm.AddCoinAssets(coinassets)
}()
}

```

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: 66ff55f96c2e8eb9065c041d41c91e77922e6d07

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.