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

braveheart12/Ren-Hyperdrive: block/commit.go; 89 LoC

Open
#6,455 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 [braveheart12/Ren-Hyperdrive](https://www.github.com/braveheart12/Ren-Hyperdrive) at [block/commit.go](https://github.com/braveheart12/Ren-Hyperdrive/blob/443a6567fa2094e6497df240febc26be0dfdae4c/block/commit.go#L170-L258)

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.

> reference to round is reassigned at line 182

[Click here to see the code in its original context.](https://github.com/braveheart12/Ren-Hyperdrive/blob/443a6567fa2094e6497df240febc26be0dfdae4c/block/commit.go#L170-L258)

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

```go
for round, preCommits := range preCommitsByRound {

numNilPreCommits := 0
nilPreCommitSignatures := []sig.Signature{}
nilPreCommitSignatories := []sig.Signatory{}

if commit != nil && round <= commit.Polka.Round {
continue
}
if len(preCommits) < consensusThreshold {
continue
}
preCommitsRound = &round

// Build a mapping of the pre-commits for each block
preCommitsForBlock := map[sig.Hash]int{}
for _, preCommit := range preCommits {
// Invariant check
if preCommit.Polka.Height != height {
panic(fmt.Errorf("expected pre-commit height (%v) to equal %v", preCommit.Polka.Height, height))
}
if preCommit.Polka.Round != round {
panic(fmt.Errorf("expected pre-commit round (%v) to equal %v", preCommit.Polka.Round, round))
}
if preCommit.Polka.Block == nil {
numNilPreCommits++
nilPreCommitSignatures = append(nilPreCommitSignatures, preCommit.Signature)
nilPreCommitSignatories = append(nilPreCommitSignatories, preCommit.Signatory)
continue
}

// Invariant check
if preCommit.Polka.Block.Height != height {
panic(fmt.Errorf("expected pre-commit block height (%v) to equal %v", preCommit.Polka.Block.Height, height))
}
if preCommit.Polka.Round != round {
panic(fmt.Errorf("expected pre-commit round (%v) to equal %v", preCommit.Polka.Round, round))
}
preCommitsForBlock[preCommit.Polka.Block.Header]++
}

// Search for a commit of pre-commits for non-nil block
for blockHeader, numPreVotes := range preCommitsForBlock {
if numPreVotes >= consensusThreshold {
commit = &Commit{
Signatures: make(sig.Signatures, 0, consensusThreshold),
Signatories: make(sig.Signatories, 0, consensusThreshold),
}
for _, preCommit := range preCommits {
if preCommit.Polka.Block != nil && preCommit.Polka.Block.Header.Equal(blockHeader) {
if commit.Polka.Block != nil {
// Invariant check
if commit.Polka.Round != preCommit.Polka.Round {
panic(fmt.Errorf("expected commit round (%v) to equal pre-commit round (%v)", commit.Polka.Round, preCommit.Polka.Round))
}
if commit.Polka.Height != preCommit.Polka.Height {
panic(fmt.Errorf("expected commit height (%v) to equal pre-commit height (%v)", commit.Polka.Height, preCommit.Polka.Height))
}
} else {
// Invariant check
if preCommit.Polka.Height != height {
panic(fmt.Errorf("expected pre-commit height (%v) to equal %v", preCommit.Polka.Height, height))
}
if preCommit.Polka.Round != round {
panic(fmt.Errorf("expected pre-commit round (%v) to equal %v", preCommit.Polka.Round, round))
}
commit.Polka = preCommit.Polka
}
commit.Signatures = append(commit.Signatures, preCommit.Signature)
commit.Signatories = append(commit.Signatories, preCommit.Signatory)
}
}
break
}
}

if numNilPreCommits >= consensusThreshold {
// Return a nil-Commit
commit = &Commit{
Polka: Polka{
Block: nil,
Height: height,
Round: round,
},
Signatures: nilPreCommitSignatures,
Signatories: nilPreCommitSignatories,
}
}
}

```

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: 443a6567fa2094e6497df240febc26be0dfdae4c

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.