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

SmartMeshFoundation/SmartPlasma: service/block.go; 157 LoC

Open
#11,223 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 [SmartMeshFoundation/SmartPlasma](https://www.github.com/SmartMeshFoundation/SmartPlasma) at [service/block.go](https://github.com/SmartMeshFoundation/SmartPlasma/blob/dae20686e7e1afcb9b8c31ddf7db25c867c9d932/service/block.go#L112-L268)

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 tx used in defer or goroutine at line 188

[Click here to see the code in its original context.](https://github.com/SmartMeshFoundation/SmartPlasma/blob/dae20686e7e1afcb9b8c31ddf7db25c867c9d932/service/block.go#L112-L268)

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

```go
for tx := range ch {
storedAmount, err := s.session.Wallet(common.BigToHash(tx.UID()))
if err != nil {
return err
}

// if storedAmount = 0 then the deposit does not exist
if storedAmount.Int64() == 0 {
continue
}

if lastBlock.Uint64() == 0 {
sender, err := transaction.Sender(tx)
if err != nil {
continue
}

if tx.NewOwner().String() != sender.String() {
continue
}

txs[tx.UID().String()] = tx
continue
}

startBlock, err := s.session.Wallet2(tx.UID())
if err != nil {
return err
}

sender, err := transaction.Sender(tx)
if err != nil {
continue
}

// if first transaction
if tx.Nonce().Int64() == 0 {
if tx.NewOwner().String() != sender.String() {
continue
}

if startBlock.Uint64() > lastBlock.Uint64() {
continue
}

type testTx struct {
found bool
err error
}

testTxChan := make(chan *testTx)

start := int(startBlock.Int64())
finish := int(lastBlock.Int64())

go func() {
for i := start; i <= finish; i++ {
rawBlock, err := s.RawBlockFromDB(uint64(i))
if err != nil {
testTxChan <- &testTx{err: err}
return
}

block := transactions.NewBlock()
err = block.Unmarshal(rawBlock)
if err != nil {
testTxChan <- &testTx{err: err}
return
}

_, err = block.Build()
if err != nil {
testTxChan <- &testTx{err: err}
return
}

proof := block.CreateProof(tx.UID())

found, err := s.VerifyTxProof(
tx.UID(), tx.Hash(), uint64(i), proof)
if err != nil {
testTxChan <- &testTx{err: err}
return
}

if found {
testTxChan <- &testTx{found: true}
return
}
}
testTxChan <- &testTx{}
}()

select {
case result := <-testTxChan:
if result.err != nil {
return err
}
if !result.found {
txs[tx.UID().String()] = tx
}
case <-ctx.Done():
close(testTxChan)
return errors.New("timeout")
}
} else if tx.Nonce().Int64() > 0 {
if tx.NewOwner().String() == sender.String() {
continue
}

rawBlock, err := s.RawBlockFromDB(tx.PrevBlock().Uint64())
if err != nil {
return err
}

prevBlock := transactions.NewBlock()
err = prevBlock.Unmarshal(rawBlock)
if err != nil {
return err
}

numTx := prevBlock.NumberOfTX()
if numTx == 0 {
continue
}

_, err = prevBlock.Build()
if err != nil {
return err
}

proof := prevBlock.CreateProof(tx.UID())
prevTx, err := prevBlock.GetTx(tx.UID())
if err != nil {
continue
}

if tx.Nonce().Uint64() != prevTx.Nonce().Uint64()+1 {
continue
}

if !bytes.Equal(tx.Amount().Bytes(), prevTx.Amount().Bytes()) {
continue
}

found, err := s.VerifyTxProof(
tx.UID(), prevTx.Hash(), tx.PrevBlock().Uint64(), proof)
if err != nil {
return err
}

if !found {
continue
}
txs[tx.UID().String()] = tx
}
}

```

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: dae20686e7e1afcb9b8c31ddf7db25c867c9d932

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.