You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// removeZeroCoins removes all zero coins from the given coin set in-place.
funcremoveZeroCoins(coinsCoins) Coins {
i, l:=0, len(coins)
fori<l {
ifcoins[i].IsZero() {
// remove coin
coins=append(coins[:i], coins[i+1:]...)
l--
} else {
i++
}
}
returncoins[:i]
}
that's a bug!
That code mutates the underlying slice, but really we should create a copy first, and then mutate that one or simply remove the extra state and make it direct to use as
We really perhaps should be aiming for correctness than cleverness in reducing memory. The code with the extra state has more state, isn't readable. The suggestion to keep it simple is more legible, correct and efficient.
This is an interesting and subtle bug. Easily fixed by adding a couple unit tests that correctly show the fault, then the changes outlined above to resolve. I can submit a PR for this.
Noticed via code audit, there is a bunch of code that mutates slices in place, but really does mutate underlying slices for example
cosmos-sdk/types/coin.go
Lines 547 to 561 in 13b5a8d
that's a bug!
That code mutates the underlying slice, but really we should create a copy first, and then mutate that one or simply remove the extra state and make it direct to use as
and here is a demonstration of the bug https://play.golang.org/p/F8TnUkqqs_Z
or inlined below
which prints out
For Admin Use
The text was updated successfully, but these errors were encountered: