Skip to content

Commit 490b5ab

Browse files
authored
Merge pull request #512 from blinklabs-io/fix/nilaway-fixes
fix: fix nil pointers identified by nilaway (part 1)
2 parents c504a7b + 14ab969 commit 490b5ab

File tree

13 files changed

+136
-86
lines changed

13 files changed

+136
-86
lines changed

bech32/bech32.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func toBytes(chars string) ([]byte, error) {
2727
for i := 0; i < len(chars); i++ {
2828
index := strings.IndexByte(charset, chars[i])
2929
if index < 0 {
30-
return nil, ErrNonCharsetChar(chars[i])
30+
// In the original code, this returns nil instead
31+
return []byte{}, ErrNonCharsetChar(chars[i])
3132
}
3233
decoded = append(decoded, byte(index))
3334
}

cmd/common/cmdline.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ type GlobalFlags struct {
3333
}
3434

3535
func NewGlobalFlags() *GlobalFlags {
36+
var name string
37+
if os.Args == nil {
38+
name = "gouroboros"
39+
} else {
40+
name = os.Args[0]
41+
}
3642
f := &GlobalFlags{
37-
Flagset: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
43+
Flagset: flag.NewFlagSet(name, flag.ExitOnError),
3844
}
3945
f.Flagset.StringVar(
4046
&f.Socket,
@@ -71,6 +77,10 @@ func NewGlobalFlags() *GlobalFlags {
7177
}
7278

7379
func (f *GlobalFlags) Parse() {
80+
if os.Args == nil {
81+
fmt.Printf("failed to parse command line\n")
82+
os.Exit(1)
83+
}
7484
if err := f.Flagset.Parse(os.Args[1:]); err != nil {
7585
fmt.Printf("failed to parse command args: %s\n", err)
7686
os.Exit(1)

cmd/gouroboros/chainsync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ func chainSyncRollForwardHandler(
294294
byronBlock.Hash(),
295295
)
296296
default:
297+
if block == nil {
298+
return fmt.Errorf("block is nil")
299+
}
297300
fmt.Printf(
298301
"era = %s, slot = %d, block_no = %d, id = %s\n",
299302
block.Era().Name,

cmd/gouroboros/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ type globalFlags struct {
3535
}
3636

3737
func newGlobalFlags() *globalFlags {
38+
var name string
39+
if os.Args == nil {
40+
name = "gouroboros"
41+
} else {
42+
name = os.Args[0]
43+
}
3844
f := &globalFlags{
39-
flagset: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
45+
flagset: flag.NewFlagSet(name, flag.ExitOnError),
4046
}
4147
f.flagset.StringVar(
4248
&f.socket,
@@ -73,6 +79,10 @@ func newGlobalFlags() *globalFlags {
7379
}
7480

7581
func main() {
82+
if os.Args == nil {
83+
fmt.Printf("failed parsing command line\n")
84+
os.Exit(1)
85+
}
7686
f := newGlobalFlags()
7787
err := f.flagset.Parse(os.Args[1:])
7888
if err != nil {

cmd/tx-submission/main.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ import (
2222
"time"
2323

2424
ouroboros "github.com/blinklabs-io/gouroboros"
25-
"github.com/blinklabs-io/gouroboros/cbor"
2625
"github.com/blinklabs-io/gouroboros/cmd/common"
26+
"github.com/blinklabs-io/gouroboros/ledger"
2727
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"
28-
29-
"golang.org/x/crypto/blake2b"
3028
)
3129

3230
type txSubmissionFlags struct {
@@ -117,19 +115,17 @@ func main() {
117115
os.Exit(1)
118116
}
119117

120-
// Generate TX hash
121-
// Unwrap raw transaction bytes into a CBOR array
122-
var txUnwrap []cbor.RawMessage
123-
if _, err := cbor.Decode(txBytes, &txUnwrap); err != nil {
124-
fmt.Printf("ERROR: failed to unwrap transaction CBOR: %s", err)
118+
// convert to tx
119+
txType, err := ledger.DetermineTransactionType(txBytes)
120+
if err != nil {
121+
fmt.Printf("ERROR: could not parse transaction to determine type: %s", err)
122+
os.Exit(1)
123+
}
124+
tx, err := ledger.NewTransactionFromCbor(txType, txBytes)
125+
if err != nil {
126+
fmt.Printf("failed to parse transaction CBOR: %s", err)
125127
os.Exit(1)
126128
}
127-
// index 0 is the transaction body
128-
// Store index 0 (transaction body) as byte array
129-
txBody := txUnwrap[0]
130-
131-
// Convert the body into a blake2b256 hash string
132-
txHash = blake2b.Sum256(txBody)
133129

134130
// Create our "done" channel
135131
doneChan = make(chan any)
@@ -140,7 +136,7 @@ func main() {
140136
// Wait until we're done
141137
<-doneChan
142138

143-
fmt.Printf("Successfully sent transaction %x\n", txHash)
139+
fmt.Printf("Successfully sent transaction %x\n", tx.Hash())
144140

145141
if err := o.Close(); err != nil {
146142
fmt.Printf("ERROR: failed to close connection: %s\n", err)

ledger/allegra.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,24 @@ func (b *AllegraBlock) Transactions() []Transaction {
8484
}
8585

8686
func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
87-
var block *utxorpc.Block
88-
var body *utxorpc.BlockBody
89-
var header *utxorpc.BlockHeader
9087
var txs []*utxorpc.Tx
91-
header.Slot = b.SlotNumber()
9288
tmpHash, _ := hex.DecodeString(b.Hash())
93-
header.Hash = tmpHash
94-
header.Height = b.BlockNumber()
9589
for _, t := range b.Transactions() {
9690
tx := t.Utxorpc()
9791
txs = append(txs, tx)
9892
}
99-
body.Tx = txs
100-
block.Body = body
101-
block.Header = header
93+
body := &utxorpc.BlockBody{
94+
Tx: txs,
95+
}
96+
header := &utxorpc.BlockHeader{
97+
Hash: tmpHash,
98+
Height: b.BlockNumber(),
99+
Slot: b.SlotNumber(),
100+
}
101+
block := &utxorpc.Block{
102+
Body: body,
103+
Header: header,
104+
}
102105
return block
103106
}
104107

ledger/alonzo.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,24 @@ func (b *AlonzoBlock) Transactions() []Transaction {
9494
}
9595

9696
func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
97-
var block *utxorpc.Block
98-
var body *utxorpc.BlockBody
99-
var header *utxorpc.BlockHeader
10097
var txs []*utxorpc.Tx
101-
header.Slot = b.SlotNumber()
10298
tmpHash, _ := hex.DecodeString(b.Hash())
103-
header.Hash = tmpHash
104-
header.Height = b.BlockNumber()
10599
for _, t := range b.Transactions() {
106100
tx := t.Utxorpc()
107101
txs = append(txs, tx)
108102
}
109-
body.Tx = txs
110-
block.Body = body
111-
block.Header = header
103+
body := &utxorpc.BlockBody{
104+
Tx: txs,
105+
}
106+
header := &utxorpc.BlockHeader{
107+
Hash: tmpHash,
108+
Height: b.BlockNumber(),
109+
Slot: b.SlotNumber(),
110+
}
111+
block := &utxorpc.Block{
112+
Body: body,
113+
Header: header,
114+
}
112115
return block
113116
}
114117

ledger/babbage.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,24 @@ func (b *BabbageBlock) Transactions() []Transaction {
9494
}
9595

9696
func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
97-
var block *utxorpc.Block
98-
var body *utxorpc.BlockBody
99-
var header *utxorpc.BlockHeader
10097
var txs []*utxorpc.Tx
101-
header.Slot = b.SlotNumber()
10298
tmpHash, _ := hex.DecodeString(b.Hash())
103-
header.Hash = tmpHash
104-
header.Height = b.BlockNumber()
10599
for _, t := range b.Transactions() {
106100
tx := t.Utxorpc()
107101
txs = append(txs, tx)
108102
}
109-
body.Tx = txs
110-
block.Body = body
111-
block.Header = header
103+
body := &utxorpc.BlockBody{
104+
Tx: txs,
105+
}
106+
header := &utxorpc.BlockHeader{
107+
Hash: tmpHash,
108+
Height: b.BlockNumber(),
109+
Slot: b.SlotNumber(),
110+
}
111+
block := &utxorpc.Block{
112+
Body: body,
113+
Header: header,
114+
}
112115
return block
113116
}
114117

@@ -316,7 +319,7 @@ func (o BabbageTransactionOutput) DatumHash() *Blake2b256 {
316319
if o.DatumOption != nil {
317320
return o.DatumOption.hash
318321
}
319-
return nil
322+
return &Blake2b256{}
320323
}
321324

322325
func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {

ledger/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ func (a Address) MarshalJSON() ([]byte, error) {
375375
type IssuerVkey [32]byte
376376

377377
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
378380
hash, _ := blake2b.New(28, nil)
379381
hash.Write(i[:])
380382
return Blake2b224(hash.Sum(nil))

ledger/common_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func TestAddressPaymentAddress(t *testing.T) {
237237
if err != nil {
238238
t.Fatalf("failed to decode address: %s", err)
239239
}
240+
if addr.PaymentAddress() == nil {
241+
t.Fatalf("payment address is nil")
242+
}
240243
if addr.PaymentAddress().String() != testDef.expectedPaymentAddress {
241244
t.Fatalf(
242245
"payment address did not match expected value, got: %s, wanted: %s",
@@ -275,6 +278,9 @@ func TestAddressStakeAddress(t *testing.T) {
275278
if err != nil {
276279
t.Fatalf("failed to decode address: %s", err)
277280
}
281+
if addr.StakeAddress() == nil {
282+
t.Fatalf("stake address is nil")
283+
}
278284
if addr.StakeAddress().String() != testDef.expectedStakeAddress {
279285
t.Fatalf(
280286
"stake address did not match expected value, got: %s, wanted: %s",

0 commit comments

Comments
 (0)