Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nil pointers identified by nilaway (part 2) #517

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cbor/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
case CborTagCbor:
v.value = tmpTag.Content
case CborTagRational:
var tmpRat []int64
tmpRat := []int64{}
if _, err := Decode(tmpTag.Content, &tmpRat); err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions ledger/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ func DetermineBlockType(data []byte) (uint, error) {
}

func generateBlockHeaderHash(data []byte, prefix []byte) string {
// We can ignore the error return here because our fixed size/key arguments will
// never trigger an error
tmpHash, _ := blake2b.New256(nil)
tmpHash, err := blake2b.New256(nil)
if err != nil {
panic(
fmt.Sprintf("unexpected error generating empty blake2b hash: %s", err),
)
}
if prefix != nil {
tmpHash.Write(prefix)
}
Expand Down
18 changes: 12 additions & 6 deletions ledger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint {
}

func (a AssetFingerprint) Hash() Blake2b160 {
// We can ignore the error return here because our fixed size/key arguments will
// never trigger an error
tmpHash, _ := blake2b.New(20, nil)
tmpHash, err := blake2b.New(20, nil)
if err != nil {
panic(
fmt.Sprintf("unexpected error creating empty blake2b hash: %s", err),
)
}
tmpHash.Write(a.policyId)
tmpHash.Write(a.assetName)
return NewBlake2b160(tmpHash.Sum(nil))
Expand Down Expand Up @@ -375,9 +378,12 @@ func (a Address) MarshalJSON() ([]byte, error) {
type IssuerVkey [32]byte

func (i IssuerVkey) Hash() Blake2b224 {
// We can ignore the error return here because our fixed size/key arguments will
// never trigger an error
hash, _ := blake2b.New(28, nil)
hash, err := blake2b.New(28, nil)
if err != nil {
panic(
fmt.Sprintf("unexpected error creating empty blake2b hash: %s", err),
)
}
hash.Write(i[:])
return Blake2b224(hash.Sum(nil))
}
Expand Down
11 changes: 8 additions & 3 deletions ledger/era.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ var eras = map[uint8]Era{
},
}

func GetEraById(eraId uint8) *Era {
var EraInvalid = Era{
Id: 0,
Name: "invalid",
}

func GetEraById(eraId uint8) Era {
era, ok := eras[eraId]
if !ok {
return nil
return EraInvalid
}
return &era
return era
}
wolf31o2 marked this conversation as resolved.
Show resolved Hide resolved

// BlockHeaderToBlockTypeMap is a mapping of NtN chainsync block header types
Expand Down
15 changes: 7 additions & 8 deletions ledger/era_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import (
)

type getEraByIdTestDefinition struct {
Id uint8
Name string
ExpectNil bool
Id uint8
Name string
}

var getEraByIdTests = []getEraByIdTestDefinition{
Expand Down Expand Up @@ -51,17 +50,17 @@ var getEraByIdTests = []getEraByIdTestDefinition{
Name: "Babbage",
},
{
Id: 99,
ExpectNil: true,
Id: 99,
Name: "invalid",
},
}

func TestGetEraById(t *testing.T) {
for _, test := range getEraByIdTests {
era := ledger.GetEraById(test.Id)
if era == nil {
if !test.ExpectNil {
t.Fatalf("got unexpected nil, wanted %s", test.Name)
if era == ledger.EraInvalid {
if test.Name != "invalid" {
t.Fatalf("got unexpected EraInvalid, wanted %s", test.Name)
}
} else {
if era.Name != test.Name {
Expand Down
4 changes: 2 additions & 2 deletions ledger/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (e *ApplyTxError) UnmarshalCBOR(data []byte) error {
return err
}
for _, failure := range tmpData {
var tmpFailure []cbor.RawMessage
tmpFailure := []cbor.RawMessage{}
if _, err := cbor.Decode(failure, &tmpFailure); err != nil {
return err
}
Expand Down Expand Up @@ -212,7 +212,7 @@ type UtxowFailure struct {
}

func (e *UtxowFailure) UnmarshalCBOR(data []byte) error {
var tmpFailure []cbor.RawMessage
tmpFailure := []cbor.RawMessage{}
if _, err := cbor.Decode(data, &tmpFailure); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (b *ShelleyTransactionBody) Utxorpc() *utxorpc.Tx {
if err != nil {
return &utxorpc.Tx{}
}
tx := &utxorpc.Tx {
tx := &utxorpc.Tx{
Inputs: txi,
Outputs: txo,
Fee: b.Fee(),
Expand Down
9 changes: 6 additions & 3 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ func DetermineTransactionType(data []byte) (uint, error) {
}

func generateTransactionHash(data []byte, prefix []byte) string {
// We can ignore the error return here because our fixed size/key arguments will
// never trigger an error
tmpHash, _ := blake2b.New256(nil)
tmpHash, err := blake2b.New256(nil)
if err != nil {
panic(
fmt.Sprintf("unexpected error generating empty blake2b hash: %s", err),
)
}
if prefix != nil {
tmpHash.Write(prefix)
}
Expand Down
8 changes: 4 additions & 4 deletions protocol/localstatequery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (c *Client) GetChainBlockNo() (int64, error) {
query := buildQuery(
QueryTypeChainBlockNo,
)
var result []int64
result := []int64{}
if err := c.runQuery(query, &result); err != nil {
return 0, err
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *Client) GetEpochNo() (int, error) {
currentEra,
QueryTypeShelleyEpochNo,
)
var result []int
result := []int{}
if err := c.runQuery(query, &result); err != nil {
return 0, err
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func (c *Client) GetCurrentProtocolParams() (*CurrentProtocolParamsResult, error
currentEra,
QueryTypeShelleyCurrentProtocolParams,
)
var result []CurrentProtocolParamsResult
result := []CurrentProtocolParamsResult{}
if err := c.runQuery(query, &result); err != nil {
return nil, err
}
Expand Down Expand Up @@ -470,7 +470,7 @@ func (c *Client) GetGenesisConfig() (*GenesisConfigResult, error) {
currentEra,
QueryTypeShelleyGenesisConfig,
)
var result []GenesisConfigResult
result := []GenesisConfigResult{}
if err := c.runQuery(query, &result); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (p *Protocol) recvLoop() {
// Decode message into generic list until we can determine what type of message it is.
// This also lets us determine how many bytes the message is. We use RawMessage here to
// avoid parsing things that we may not be able to parse
var tmpMsg []cbor.RawMessage
tmpMsg := []cbor.RawMessage{}
numBytesRead, err := cbor.Decode(recvBuffer.Bytes(), &tmpMsg)
if err != nil {
if err == io.ErrUnexpectedEOF && recvBuffer.Len() > 0 {
Expand Down