From 4096c086bcfd67d611264dd569587549c9201c40 Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Thu, 21 Apr 2022 13:15:55 -0700 Subject: [PATCH] Add more defensive checks around nil derefs --- .../integration/transaction_preconditions_test.go | 6 +++++- .../horizon/internal/resourceadapter/transaction.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/services/horizon/internal/integration/transaction_preconditions_test.go b/services/horizon/internal/integration/transaction_preconditions_test.go index 9f08e70be2..abee68a107 100644 --- a/services/horizon/internal/integration/transaction_preconditions_test.go +++ b/services/horizon/internal/integration/transaction_preconditions_test.go @@ -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) } diff --git a/services/horizon/internal/resourceadapter/transaction.go b/services/horizon/internal/resourceadapter/transaction.go index f27a819696..28ead49df6 100644 --- a/services/horizon/internal/resourceadapter/transaction.go +++ b/services/horizon/internal/resourceadapter/transaction.go @@ -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 @@ -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 } @@ -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 }