Skip to content

Commit 96631ad

Browse files
authored
Merge pull request #517 from blinklabs-io/fix/nilaway-fixes
fix: nil pointers identified by nilaway (part 2)
2 parents 87aae65 + 5bfb423 commit 96631ad

File tree

10 files changed

+48
-32
lines changed

10 files changed

+48
-32
lines changed

cbor/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
6363
case CborTagCbor:
6464
v.value = tmpTag.Content
6565
case CborTagRational:
66-
var tmpRat []int64
66+
tmpRat := []int64{}
6767
if _, err := Decode(tmpTag.Content, &tmpRat); err != nil {
6868
return err
6969
}

ledger/block.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ func DetermineBlockType(data []byte) (uint, error) {
105105
}
106106

107107
func generateBlockHeaderHash(data []byte, prefix []byte) string {
108-
// We can ignore the error return here because our fixed size/key arguments will
109-
// never trigger an error
110-
tmpHash, _ := blake2b.New256(nil)
108+
tmpHash, err := blake2b.New256(nil)
109+
if err != nil {
110+
panic(
111+
fmt.Sprintf("unexpected error generating empty blake2b hash: %s", err),
112+
)
113+
}
111114
if prefix != nil {
112115
tmpHash.Write(prefix)
113116
}

ledger/common.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint {
161161
}
162162

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

377380
func (i IssuerVkey) Hash() Blake2b224 {
378-
// We can ignore the error return here because our fixed size/key arguments will
379-
// never trigger an error
380-
hash, _ := blake2b.New(28, nil)
381+
hash, err := blake2b.New(28, nil)
382+
if err != nil {
383+
panic(
384+
fmt.Sprintf("unexpected error creating empty blake2b hash: %s", err),
385+
)
386+
}
381387
hash.Write(i[:])
382388
return Blake2b224(hash.Sum(nil))
383389
}

ledger/era.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,17 @@ var eras = map[uint8]Era{
5050
},
5151
}
5252

53-
func GetEraById(eraId uint8) *Era {
53+
var EraInvalid = Era{
54+
Id: 0,
55+
Name: "invalid",
56+
}
57+
58+
func GetEraById(eraId uint8) Era {
5459
era, ok := eras[eraId]
5560
if !ok {
56-
return nil
61+
return EraInvalid
5762
}
58-
return &era
63+
return era
5964
}
6065

6166
// BlockHeaderToBlockTypeMap is a mapping of NtN chainsync block header types

ledger/era_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020
)
2121

2222
type getEraByIdTestDefinition struct {
23-
Id uint8
24-
Name string
25-
ExpectNil bool
23+
Id uint8
24+
Name string
2625
}
2726

2827
var getEraByIdTests = []getEraByIdTestDefinition{
@@ -51,17 +50,17 @@ var getEraByIdTests = []getEraByIdTestDefinition{
5150
Name: "Babbage",
5251
},
5352
{
54-
Id: 99,
55-
ExpectNil: true,
53+
Id: 99,
54+
Name: "invalid",
5655
},
5756
}
5857

5958
func TestGetEraById(t *testing.T) {
6059
for _, test := range getEraByIdTests {
6160
era := ledger.GetEraById(test.Id)
62-
if era == nil {
63-
if !test.ExpectNil {
64-
t.Fatalf("got unexpected nil, wanted %s", test.Name)
61+
if era == ledger.EraInvalid {
62+
if test.Name != "invalid" {
63+
t.Fatalf("got unexpected EraInvalid, wanted %s", test.Name)
6564
}
6665
} else {
6766
if era.Name != test.Name {

ledger/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (e *ApplyTxError) UnmarshalCBOR(data []byte) error {
167167
return err
168168
}
169169
for _, failure := range tmpData {
170-
var tmpFailure []cbor.RawMessage
170+
tmpFailure := []cbor.RawMessage{}
171171
if _, err := cbor.Decode(failure, &tmpFailure); err != nil {
172172
return err
173173
}
@@ -212,7 +212,7 @@ type UtxowFailure struct {
212212
}
213213

214214
func (e *UtxowFailure) UnmarshalCBOR(data []byte) error {
215-
var tmpFailure []cbor.RawMessage
215+
tmpFailure := []cbor.RawMessage{}
216216
if _, err := cbor.Decode(data, &tmpFailure); err != nil {
217217
return err
218218
}

ledger/shelley.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (b *ShelleyTransactionBody) Utxorpc() *utxorpc.Tx {
233233
if err != nil {
234234
return &utxorpc.Tx{}
235235
}
236-
tx := &utxorpc.Tx {
236+
tx := &utxorpc.Tx{
237237
Inputs: txi,
238238
Outputs: txo,
239239
Fee: b.Fee(),

ledger/tx.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ func DetermineTransactionType(data []byte) (uint, error) {
147147
}
148148

149149
func generateTransactionHash(data []byte, prefix []byte) string {
150-
// We can ignore the error return here because our fixed size/key arguments will
151-
// never trigger an error
152-
tmpHash, _ := blake2b.New256(nil)
150+
tmpHash, err := blake2b.New256(nil)
151+
if err != nil {
152+
panic(
153+
fmt.Sprintf("unexpected error generating empty blake2b hash: %s", err),
154+
)
155+
}
153156
if prefix != nil {
154157
tmpHash.Write(prefix)
155158
}

protocol/localstatequery/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (c *Client) GetChainBlockNo() (int64, error) {
248248
query := buildQuery(
249249
QueryTypeChainBlockNo,
250250
)
251-
var result []int64
251+
result := []int64{}
252252
if err := c.runQuery(query, &result); err != nil {
253253
return 0, err
254254
}
@@ -293,7 +293,7 @@ func (c *Client) GetEpochNo() (int, error) {
293293
currentEra,
294294
QueryTypeShelleyEpochNo,
295295
)
296-
var result []int
296+
result := []int{}
297297
if err := c.runQuery(query, &result); err != nil {
298298
return 0, err
299299
}
@@ -331,7 +331,7 @@ func (c *Client) GetCurrentProtocolParams() (*CurrentProtocolParamsResult, error
331331
currentEra,
332332
QueryTypeShelleyCurrentProtocolParams,
333333
)
334-
var result []CurrentProtocolParamsResult
334+
result := []CurrentProtocolParamsResult{}
335335
if err := c.runQuery(query, &result); err != nil {
336336
return nil, err
337337
}
@@ -470,7 +470,7 @@ func (c *Client) GetGenesisConfig() (*GenesisConfigResult, error) {
470470
currentEra,
471471
QueryTypeShelleyGenesisConfig,
472472
)
473-
var result []GenesisConfigResult
473+
result := []GenesisConfigResult{}
474474
if err := c.runQuery(query, &result); err != nil {
475475
return nil, err
476476
}

protocol/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (p *Protocol) recvLoop() {
322322
// Decode message into generic list until we can determine what type of message it is.
323323
// This also lets us determine how many bytes the message is. We use RawMessage here to
324324
// avoid parsing things that we may not be able to parse
325-
var tmpMsg []cbor.RawMessage
325+
tmpMsg := []cbor.RawMessage{}
326326
numBytesRead, err := cbor.Decode(recvBuffer.Bytes(), &tmpMsg)
327327
if err != nil {
328328
if err == io.ErrUnexpectedEOF && recvBuffer.Len() > 0 {

0 commit comments

Comments
 (0)