From 6476137d1725386d2a31c2ee05de627dc465f784 Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Thu, 28 Sep 2023 18:54:19 +0200 Subject: [PATCH] tapfreighter+tapdb: fix some loop memory aliasing --- tapdb/addrs.go | 3 ++- tapdb/assets_store.go | 6 ++++-- tapfreighter/chain_porter.go | 5 +++-- tapfreighter/wallet.go | 5 +++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tapdb/addrs.go b/tapdb/addrs.go index 2e8cbfb59..9dbcd758f 100644 --- a/tapdb/addrs.go +++ b/tapdb/addrs.go @@ -211,10 +211,11 @@ func (t *TapAddressBook) InsertAddrs(ctx context.Context, // For each of the addresses listed, we'll insert the two new // internal keys, then use those returned primary key IDs to // returned to insert the address itself. - for _, addr := range addrs { + for idx := range addrs { // The asset genesis should already be known at this // point, so we'll just fetch it so we can obtain the // genAssetID. + addr := addrs[idx] assetGen, err := db.FetchGenesisByAssetID( ctx, addr.AssetID[:], ) diff --git a/tapdb/assets_store.go b/tapdb/assets_store.go index af6e6c105..deb48a0c3 100644 --- a/tapdb/assets_store.go +++ b/tapdb/assets_store.go @@ -1295,7 +1295,8 @@ func (a *AssetStore) insertAssetWitnesses(ctx context.Context, db ActiveAssetsStore, assetID int32, inputs []asset.Witness) error { var buf [8]byte - for _, input := range inputs { + for idx := range inputs { + input := inputs[idx] prevID := input.PrevID prevOutpoint, err := encodeOutpoint(prevID.OutPoint) @@ -2285,9 +2286,10 @@ func logPendingPassiveAssets(ctx context.Context, q ActiveAssetsStore, transferID, newUtxoID int32, passiveAssets []*tapfreighter.PassiveAssetReAnchor) error { - for _, passiveAsset := range passiveAssets { + for idx := range passiveAssets { // Encode new witness data. var ( + passiveAsset = passiveAssets[idx] newWitnessBuf bytes.Buffer buf [8]byte ) diff --git a/tapfreighter/chain_porter.go b/tapfreighter/chain_porter.go index cb3c03e7b..8680784c0 100644 --- a/tapfreighter/chain_porter.go +++ b/tapfreighter/chain_porter.go @@ -689,7 +689,8 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error { // Load passive asset proof files from archive. passiveAssetProofFiles := map[[32]byte]proof.Blob{} - for _, passiveAsset := range pkg.OutboundPkg.PassiveAssets { + for idx := range pkg.OutboundPkg.PassiveAssets { + passiveAsset := pkg.OutboundPkg.PassiveAssets[idx] proofLocator := proof.Locator{ AssetID: &passiveAsset.GenesisID, ScriptKey: *passiveAsset.ScriptKey.PubKey, @@ -767,7 +768,7 @@ func (p *ChainPorter) importLocalAddresses(ctx context.Context, case strings.Contains(err.Error(), "already exists"): break - case err != nil: + default: return err } } diff --git a/tapfreighter/wallet.go b/tapfreighter/wallet.go index cfc82f3ee..bd91a7759 100644 --- a/tapfreighter/wallet.go +++ b/tapfreighter/wallet.go @@ -586,7 +586,7 @@ func (f *AssetWallet) fundPacketWithInputs(ctx context.Context, // it in the database. continue - case err != nil: + default: return nil, fmt.Errorf("cannot fetch script key: %w", err) } @@ -714,7 +714,7 @@ func (f *AssetWallet) setVPacketInputs(ctx context.Context, vPkt.Inputs = make([]*tappsbt.VInput, len(eligibleCommitments)) inputCommitments := make(tappsbt.InputCommitments) - for idx, assetInput := range eligibleCommitments { + for idx := range eligibleCommitments { // If the key found for the input UTXO cannot be identified as // belonging to the lnd wallet, we won't be able to sign for it. // This would happen if a user manually imported an asset that @@ -722,6 +722,7 @@ func (f *AssetWallet) setVPacketInputs(ctx context.Context, // not create asset entries for such imported assets in the // first place, as we won't be able to spend it anyway. But for // now we just put this check in place. + assetInput := eligibleCommitments[idx] internalKey := assetInput.InternalKey if !f.cfg.KeyRing.IsLocalKey(ctx, internalKey) { return nil, fmt.Errorf("invalid internal key family "+