Skip to content

Commit

Permalink
Add more defensive checks around nil derefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Apr 21, 2022
1 parent 17a88bd commit 4096c08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ func TestTransactionWithoutPreconditions(t *testing.T) {
tt.NoError(err)
b64 := base64.StdEncoding.EncodeToString(txBytes.Bytes())

_, err = itest.Client().SubmitTransactionXDR(b64)
txResp, err := itest.Client().SubmitTransactionXDR(b64)
tt.NoError(err)

txResp2, err := itest.Client().TransactionDetail(txResp.Hash)
tt.NoError(err)
tt.Nil(txResp2.Preconditions)
}
13 changes: 9 additions & 4 deletions services/horizon/internal/resourceadapter/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func PopulateTransaction(
}
dest.Signatures = row.Signatures

if row.HasPreconditions() {
dest.Preconditions = &protocol.TransactionPreconditions{}
}
// Prepare it unconditionally to be defensive against accidental nil
// dereferences further down.
dest.Preconditions = &protocol.TransactionPreconditions{}

if !row.TimeBounds.Null {
// Action needed in release: horizon-v3.0.0: remove ValidBefore and ValidAfter
Expand Down Expand Up @@ -95,7 +95,7 @@ func PopulateTransaction(
dest.Preconditions.MinAccountSequenceLedgerGap = uint32(row.MinAccountSequenceLedgerGap.Int64)
}

if row.ExtraSigners != nil {
if len(row.ExtraSigners) > 0 {
dest.Preconditions.ExtraSigners = row.ExtraSigners
}

Expand Down Expand Up @@ -136,6 +136,11 @@ func PopulateTransaction(
dest.Links.Succeeds = lb.Linkf("/transactions?order=desc&cursor=%s", dest.PT)
dest.Links.Precedes = lb.Linkf("/transactions?order=asc&cursor=%s", dest.PT)

// If we didn't need the structure, drop it.
if !row.HasPreconditions() {
dest.Preconditions = nil
}

return nil
}

Expand Down

0 comments on commit 4096c08

Please sign in to comment.