Noticed while auditing cosmos/gaia that if we examine the code inside https://github.com/cosmos/cosmos-sdk/blob/8cce7480917c90326c0c8a3bc2d26d1d3c29a33f/types/coin.go#L782-L803 we can see that the first for loop of code tries to check if at least one coin is zero then exit else if at the end return. However in the next loop, it performs the iteration and checks for coins that are non-zero which thereby renders the prior loop useless.
Suggestion
Simply remove the first loop and the other unnecessary checks then only add coins that are non-zero
func removeZeroCoins(coins Coins) Coins {
nonZeros := make([]Coin, 0, len(coins))
for _, coin := range coins {
if !coin.IsZero() {
nonZeros = append(nonZeros, coin)
}
}
return nonZeros
}
Summary of Bug
Noticed while auditing cosmos/gaia that if we examine the code inside https://github.com/cosmos/cosmos-sdk/blob/8cce7480917c90326c0c8a3bc2d26d1d3c29a33f/types/coin.go#L782-L803 we can see that the first for loop of code tries to check if at least one coin is zero then exit else if at the end return. However in the next loop, it performs the iteration and checks for coins that are non-zero which thereby renders the prior loop useless.
Suggestion
Simply remove the first loop and the other unnecessary checks then only add coins that are non-zero
/cc @elias-orijtech