### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current behaviour

crash

### Expected behaviour

no crash

### Steps to reproduce

```
./src/qt/bitcoin-qt -datadir=/tmp/ -regtest -printtoconsole=1 -nolisten  # Create fresh datadir (empty), stop
mv /tmp/reg_scratch/.bitcoin/regtest/wallet.1491854176.bak /tmp/regtest/wallets/  # Move over a wallet file
(Start qt with the same args as above)
(Call migratewallet)
```



### Relevant log output

```
#5  0x0000555555bd2bda in wallet::CWallet::GetTxDepthInMainChain(wallet::CWalletTx const&) const (this=<optimized out>, wtx=<optimized out>) at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/std_function.h:290
#6  wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0::operator()(wallet::CWalletTx&) const (this=<optimized out>, wtx=<optimized out>) at wallet/wallet.cpp:1351
#7  std::__invoke_impl<wallet::CWallet::TxUpdate, wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0&, wallet::CWalletTx&>(std::__invoke_other, wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0&, wallet::CWalletTx&) (__f=<optimized out>, __args=<optimized out>) at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/invoke.h:61
#8  std::__invoke_r<wallet::CWallet::TxUpdate, wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0&, wallet::CWalletTx&>(wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0&, wallet::CWalletTx&) (__fn=<optimized out>, __args=<optimized out>) at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/invoke.h:114
#9  std::_Function_handler<wallet::CWallet::TxUpdate (wallet::CWalletTx&), wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&)::$_0>::_M_invoke(std::_Any_data const&, wallet::CWalletTx&)
    (__functor=<optimized out>, __args=<optimized out>) at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/std_function.h:290
#10 0x0000555555bbb73b in std::function<wallet::CWallet::TxUpdate (wallet::CWalletTx&)>::operator()(wallet::CWalletTx&) const (this=0x7fff417f6bc0, __args=...)
    at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/std_function.h:591
#11 wallet::CWallet::RecursiveUpdateTxState(uint256 const&, std::function<wallet::CWallet::TxUpdate (wallet::CWalletTx&)> const&) (this=0x7fff2db509f0, tx_hash=..., try_updating_state=...) at wallet/wallet.cpp:1382
#12 0x0000555555bb9c4b in wallet::CWallet::MarkConflicted(uint256 const&, int, uint256 const&) (this=0x7fff2db509f0, hashBlock=..., conflicting_height=-1, hashTx=...) at wallet/wallet.cpp:1361
#13 0x0000555555bb9b55 in wallet::CWallet::LoadToWallet(uint256 const&, std::function<bool (wallet::CWalletTx&, bool)> const&) (this=0x7fff2db509f0, hash=<optimized out>, fill_wtx=<optimized out>) at wallet/wallet.cpp:1213
#14 0x0000555555bfc8c6 in wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0::operator()(wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) const (this=0x7fff417f6ed0, pwallet=0x7fff2db509f0, key=<optimized out>, value=..., err="") at wallet/walletdb.cpp:1065
#15 std::__invoke_impl<wallet::DBErrors, wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0&, wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(std::__invoke_other, wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0&, wallet::CWallet*&&, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (__f=..., __args=<optimized out>, __args=<optimized out>, __args=..., __args="")
    at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/invoke.h:61
#16 std::__invoke_r<wallet::DBErrors, wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0&, wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0&, wallet::CWallet*&&, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (__fn=..., __args=<optimized out>, __args=<optimized out>, __args=..., __args="")
    at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/invoke.h:114
#17 std::_Function_handler<wallet::DBErrors (wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&), wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)::$_0>::_M_invoke(std::_Any_data const&, wallet::CWallet*&&, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)
    (__functor=..., __args=<optimized out>, __args=<optimized out>, __args=..., __args="") at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/std_function.h:290
#18 0x0000555555bf87dc in std::function<wallet::DBErrors (wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)>::operator()(wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) const (this=0x7fff417f6ed0, __args=0x7fff2db509f0, __args=..., __args=..., __args="")
    at /usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/bits/std_function.h:591
#19 wallet::LoadRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, DataStream&, std::function<wallet::DBErrors (wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)>) (pwallet=pwallet@entry=0x7fff2db509f0, batch=..., key="tx", prefix=..., load_func=...) at wallet/walletdb.cpp:509
#20 0x0000555555bf8542 in wallet::LoadRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<wallet::DBErrors (wallet::CWallet*, DataStream&, CDataStream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)>) (pwallet=pwallet@entry=0x7fff2db509f0, batch=..., key="tx", load_func=...) at wallet/walletdb.cpp:523
#21 0x0000555555bf40a2 in wallet::LoadTxRecords(wallet::CWallet*, wallet::DatabaseBatch&, std::vector<uint256, std::allocator<uint256> >&, bool&)
    (pwallet=0x7fff2db509f0, batch=..., upgraded_txs=std::vector of length 0, capacity 0, any_unordered=@0x7fff417f6f9f: false) at wallet/walletdb.cpp:1021
#22 wallet::WalletBatch::LoadWallet(wallet::CWallet*) (this=0x7fff417f7208, pwallet=0x7fff2db509f0) at wallet/walletdb.cpp:1187
#23 0x0000555555bc29fd in wallet::CWallet::LoadWallet() (this=0x7fff2db509f0) at wallet/wallet.cpp:2304
#24 0x0000555555bad386 in wallet::CWallet::Create(wallet::WalletContext&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::unique_ptr<wallet::WalletDatabase, std::default_delete<wallet::WalletDatabase> >, unsigned long, bilingual_str&, std::vector<bilingual_str, std::allocator<bilingual_str> >&)
    (context=<optimized out>, name="wallet.1491854156.bak", database=std::unique_ptr<wallet::WalletDatabase> = {...}, wallet_creation_flags=0, error=..., warnings=std::vector of length 0, capacity 0) at wallet/wallet.cpp:2868
#25 0x0000555555bcf830 in wallet::MigrateLegacyToDescriptor(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, secure_allocator<char> > const&, wallet::WalletContext&) (wallet_name="wallet.1491854156.bak", passphrase="", context=...) at wallet/wallet.cpp:4169
```

### How did you obtain Bitcoin Core

Compiled from source

### What version of Bitcoin Core are you using?

current master

### Operating system and version

Linux

### Machine specifications

yes
return;
        // find first block that affects those keys, if there are any left
        for (const auto& entry  mapWal) {
            // iterate 
            const CWalTx &wtx = entry.second;
            if (auto* conf = wtx.state<TxStateConfirmed>()) {
                // ... whi
                for (const CTxOut &txout  wtx.tx->vout) {
                    // iterate 
                    for (const auto &keyid  GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
                        // ... and 
                        auto rit = mapKeyFirstBlock.find(keyid);
                        if (rit != mapKeyFirstBlock.end() && conf->confirmed_block_height < rit->second->confirmed_block_height) {
                            rit->second = conf;
                        }
                    }
                }
            }
        }
    }
    // Extract block timestamps for those keys
    for (const auto& entry  mapKeyFirstBlock) {
        int64_t block_time;
        CHECK_NONFATAL(chain().findBlock(entry.second->confirmed_block_hash, FoundBlock().time(block_time)));
        mapKeyBirth[entry.first] = block_time - TIMESTAMP_WINDOW; // block times can be 2h off
    }
}
/**
 * Compute smart timestamp for a transaction being added to th.
 *