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 1) #512

Merged
merged 1 commit into from
Mar 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion bech32/bech32.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func toBytes(chars string) ([]byte, error) {
for i := 0; i < len(chars); i++ {
index := strings.IndexByte(charset, chars[i])
if index < 0 {
return nil, ErrNonCharsetChar(chars[i])
// In the original code, this returns nil instead
return []byte{}, ErrNonCharsetChar(chars[i])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is upstream vendored code. We should probably add a comment here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this vendored instead of being imported?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was to avoid the flood of deps from importing https://github.com/btcsuite/btcd

}
decoded = append(decoded, byte(index))
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/common/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ type GlobalFlags struct {
}

func NewGlobalFlags() *GlobalFlags {
var name string
if os.Args == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually possible for os.Args to be nil? It can technically be nil because it's a byte slice, but the docs are fairly definitive: Args hold the command-line arguments, starting with the program name. This doesn't hurt anything, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it is possible, but I don't think it can ever happen in practice.

name = "gouroboros"
} else {
name = os.Args[0]
}
f := &GlobalFlags{
Flagset: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
Flagset: flag.NewFlagSet(name, flag.ExitOnError),
}
f.Flagset.StringVar(
&f.Socket,
Expand Down Expand Up @@ -71,6 +77,10 @@ func NewGlobalFlags() *GlobalFlags {
}

func (f *GlobalFlags) Parse() {
if os.Args == nil {
fmt.Printf("failed to parse command line\n")
os.Exit(1)
}
if err := f.Flagset.Parse(os.Args[1:]); err != nil {
fmt.Printf("failed to parse command args: %s\n", err)
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions cmd/gouroboros/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func chainSyncRollForwardHandler(
byronBlock.Hash(),
)
default:
if block == nil {
return fmt.Errorf("block is nil")
}
fmt.Printf(
"era = %s, slot = %d, block_no = %d, id = %s\n",
block.Era().Name,
Expand Down
12 changes: 11 additions & 1 deletion cmd/gouroboros/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ type globalFlags struct {
}

func newGlobalFlags() *globalFlags {
var name string
if os.Args == nil {
name = "gouroboros"
} else {
name = os.Args[0]
}
f := &globalFlags{
flagset: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
flagset: flag.NewFlagSet(name, flag.ExitOnError),
}
f.flagset.StringVar(
&f.socket,
Expand Down Expand Up @@ -73,6 +79,10 @@ func newGlobalFlags() *globalFlags {
}

func main() {
if os.Args == nil {
fmt.Printf("failed parsing command line\n")
os.Exit(1)
}
f := newGlobalFlags()
err := f.flagset.Parse(os.Args[1:])
if err != nil {
Expand Down
26 changes: 11 additions & 15 deletions cmd/tx-submission/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
"time"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/cmd/common"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"

"golang.org/x/crypto/blake2b"
)

type txSubmissionFlags struct {
Expand Down Expand Up @@ -117,19 +115,17 @@ func main() {
os.Exit(1)
}

// Generate TX hash
// Unwrap raw transaction bytes into a CBOR array
var txUnwrap []cbor.RawMessage
if _, err := cbor.Decode(txBytes, &txUnwrap); err != nil {
fmt.Printf("ERROR: failed to unwrap transaction CBOR: %s", err)
// convert to tx
txType, err := ledger.DetermineTransactionType(txBytes)
if err != nil {
fmt.Printf("ERROR: could not parse transaction to determine type: %s", err)
os.Exit(1)
}
tx, err := ledger.NewTransactionFromCbor(txType, txBytes)
if err != nil {
fmt.Printf("failed to parse transaction CBOR: %s", err)
os.Exit(1)
}
// index 0 is the transaction body
// Store index 0 (transaction body) as byte array
txBody := txUnwrap[0]

// Convert the body into a blake2b256 hash string
txHash = blake2b.Sum256(txBody)

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

fmt.Printf("Successfully sent transaction %x\n", txHash)
fmt.Printf("Successfully sent transaction %x\n", tx.Hash())

if err := o.Close(); err != nil {
fmt.Printf("ERROR: failed to close connection: %s\n", err)
Expand Down
21 changes: 12 additions & 9 deletions ledger/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,24 @@ func (b *AllegraBlock) Transactions() []Transaction {
}

func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
var block *utxorpc.Block
var body *utxorpc.BlockBody
var header *utxorpc.BlockHeader
var txs []*utxorpc.Tx
header.Slot = b.SlotNumber()
tmpHash, _ := hex.DecodeString(b.Hash())
header.Hash = tmpHash
header.Height = b.BlockNumber()
for _, t := range b.Transactions() {
tx := t.Utxorpc()
txs = append(txs, tx)
}
body.Tx = txs
block.Body = body
block.Header = header
body := &utxorpc.BlockBody{
Tx: txs,
}
header := &utxorpc.BlockHeader{
Hash: tmpHash,
Height: b.BlockNumber(),
Slot: b.SlotNumber(),
}
block := &utxorpc.Block{
Body: body,
Header: header,
}
return block
}

Expand Down
21 changes: 12 additions & 9 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,24 @@ func (b *AlonzoBlock) Transactions() []Transaction {
}

func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
var block *utxorpc.Block
var body *utxorpc.BlockBody
var header *utxorpc.BlockHeader
var txs []*utxorpc.Tx
header.Slot = b.SlotNumber()
tmpHash, _ := hex.DecodeString(b.Hash())
header.Hash = tmpHash
header.Height = b.BlockNumber()
for _, t := range b.Transactions() {
tx := t.Utxorpc()
txs = append(txs, tx)
}
body.Tx = txs
block.Body = body
block.Header = header
body := &utxorpc.BlockBody{
Tx: txs,
}
header := &utxorpc.BlockHeader{
Hash: tmpHash,
Height: b.BlockNumber(),
Slot: b.SlotNumber(),
}
block := &utxorpc.Block{
Body: body,
Header: header,
}
return block
}

Expand Down
23 changes: 13 additions & 10 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,24 @@ func (b *BabbageBlock) Transactions() []Transaction {
}

func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
var block *utxorpc.Block
var body *utxorpc.BlockBody
var header *utxorpc.BlockHeader
var txs []*utxorpc.Tx
header.Slot = b.SlotNumber()
tmpHash, _ := hex.DecodeString(b.Hash())
header.Hash = tmpHash
header.Height = b.BlockNumber()
for _, t := range b.Transactions() {
tx := t.Utxorpc()
txs = append(txs, tx)
}
body.Tx = txs
block.Body = body
block.Header = header
body := &utxorpc.BlockBody{
Tx: txs,
}
header := &utxorpc.BlockHeader{
Hash: tmpHash,
Height: b.BlockNumber(),
Slot: b.SlotNumber(),
}
block := &utxorpc.Block{
Body: body,
Header: header,
}
return block
}

Expand Down Expand Up @@ -316,7 +319,7 @@ func (o BabbageTransactionOutput) DatumHash() *Blake2b256 {
if o.DatumOption != nil {
return o.DatumOption.hash
}
return nil
return &Blake2b256{}
}

func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
Expand Down
2 changes: 2 additions & 0 deletions ledger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ 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.Write(i[:])
return Blake2b224(hash.Sum(nil))
Expand Down
6 changes: 6 additions & 0 deletions ledger/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func TestAddressPaymentAddress(t *testing.T) {
if err != nil {
t.Fatalf("failed to decode address: %s", err)
}
if addr.PaymentAddress() == nil {
t.Fatalf("payment address is nil")
}
if addr.PaymentAddress().String() != testDef.expectedPaymentAddress {
t.Fatalf(
"payment address did not match expected value, got: %s, wanted: %s",
Expand Down Expand Up @@ -275,6 +278,9 @@ func TestAddressStakeAddress(t *testing.T) {
if err != nil {
t.Fatalf("failed to decode address: %s", err)
}
if addr.StakeAddress() == nil {
t.Fatalf("stake address is nil")
}
if addr.StakeAddress().String() != testDef.expectedStakeAddress {
t.Fatalf(
"stake address did not match expected value, got: %s, wanted: %s",
Expand Down
21 changes: 12 additions & 9 deletions ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,24 @@ func (b *ConwayBlock) Transactions() []Transaction {
}

func (b *ConwayBlock) Utxorpc() *utxorpc.Block {
var block *utxorpc.Block
var body *utxorpc.BlockBody
var header *utxorpc.BlockHeader
var txs []*utxorpc.Tx
header.Slot = b.SlotNumber()
tmpHash, _ := hex.DecodeString(b.Hash())
header.Hash = tmpHash
header.Height = b.BlockNumber()
for _, t := range b.Transactions() {
tx := t.Utxorpc()
txs = append(txs, tx)
}
body.Tx = txs
block.Body = body
block.Header = header
body := &utxorpc.BlockBody{
Tx: txs,
}
header := &utxorpc.BlockHeader{
Hash: tmpHash,
Height: b.BlockNumber(),
Slot: b.SlotNumber(),
}
block := &utxorpc.Block{
Body: body,
Header: header,
}
return block
}

Expand Down
21 changes: 12 additions & 9 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,24 @@ func (b *MaryBlock) Transactions() []Transaction {
}

func (b *MaryBlock) Utxorpc() *utxorpc.Block {
var block *utxorpc.Block
var body *utxorpc.BlockBody
var header *utxorpc.BlockHeader
var txs []*utxorpc.Tx
header.Slot = b.SlotNumber()
tmpHash, _ := hex.DecodeString(b.Hash())
header.Hash = tmpHash
header.Height = b.BlockNumber()
for _, t := range b.Transactions() {
tx := t.Utxorpc()
txs = append(txs, tx)
}
body.Tx = txs
block.Body = body
block.Header = header
body := &utxorpc.BlockBody{
Tx: txs,
}
header := &utxorpc.BlockHeader{
Hash: tmpHash,
Height: b.BlockNumber(),
Slot: b.SlotNumber(),
}
block := &utxorpc.Block{
Body: body,
Header: header,
}
return block
}

Expand Down