Merge bitcoin/bitcoin#30691: Fix maybe-uninitialized warning in IsSpentKey

17707db939 Fix maybe-uninitialized warning in IsSpentKey (Ava Chow)

Pull request description:

  After 6ed424f2db, I started seeing a maybe-unitialized warning in `CWallet::IsSpentKey`:

  ```
  In destructor ‘constexpr std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’,
      inlined from ‘constexpr std::vector<_Tp, _Alloc>::~vector() [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/stl_vector.h:738:7,
      inlined from ‘constexpr WitnessUnknown::~WitnessUnknown()’ at ./addresstype.h:95:8,
      inlined from ‘constexpr void std::destroy_at(_Tp*) [with _Tp = WitnessUnknown]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/stl_construct.h:88:18,
      inlined from ‘constexpr void std::_Destroy(_Tp*) [with _Tp = WitnessUnknown]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/stl_construct.h:149:22,
      inlined from ‘std::__detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_reset()::<lambda(auto:20&&)> mutable [with auto:20 = WitnessUnknown&]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:498:19,
      inlined from ‘constexpr _Res std::__invoke_impl(__invoke_other, _Fn&&, _Args&& ...) [with _Res = void; _Fn = __detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_reset()::<lambda(auto:20&&)>; _Args = {WitnessUnknown&}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/invoke.h:61:36,
      inlined from ‘constexpr std::enable_if_t<is_invocable_r_v<_Res, _Callable, _Args ...>, _Res> std::__invoke_r(_Callable&&, _Args&& ...) [with _Res = void; _Callable = __detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_reset()::<lambda(auto:20&&)>; _Args = {WitnessUnknown&}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/invoke.h:111:28,
      inlined from ‘static constexpr decltype(auto) std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...)>, std::integer_sequence<long unsigned int, __indices ...> >::__visit_invoke(_Visitor&&, _Variants ...) [with _Result_type = void; _Visitor = std::__detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_reset()::<lambda(auto:20&&)>&&; _Variants = {std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>&}; long unsigned int ...__indices = {8}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:1064:40,
      inlined from ‘constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = void; _Visitor = __detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_reset()::<lambda(auto:20&&)>; _Variants = {variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>&}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:1823:5,
      inlined from ‘constexpr void std::__detail::__variant::_Variant_storage<false, _Types ...>::_M_reset() [with _Types = {CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:496:23,
      inlined from ‘constexpr std::__detail::__variant::_Variant_storage<false, _Types ...>::~_Variant_storage() [with _Types = {CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:506:17,
      inlined from ‘constexpr std::__detail::__variant::_Copy_ctor_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::~_Copy_ctor_base()’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:581:12,
      inlined from ‘constexpr std::__detail::__variant::_Move_ctor_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::~_Move_ctor_base()’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:618:12,
      inlined from ‘constexpr std::__detail::__variant::_Copy_assign_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::~_Copy_assign_base()’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:656:12,
      inlined from ‘constexpr std::__detail::__variant::_Move_assign_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::~_Move_assign_base()’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:708:12,
      inlined from ‘constexpr std::__detail::__variant::_Variant_base<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::~_Variant_base()’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:762:12,
      inlined from ‘constexpr std::variant<_Types>::~variant() [with _Types = {CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown}]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/variant:1432:28,
      inlined from ‘bool wallet::CWallet::IsSpentKey(const CScript&) const’ at wallet/wallet.cpp:1055:37:
  /usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include/c++/bits/stl_vector.h:370:31: error: ‘*(std::_Vector_base<unsigned char, std::allocator<unsigned char> >*)((char*)&<unnamed> + offsetof(const std::CTxDestination, std::variant<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Variant_base<CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Move_assign_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Copy_assign_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Move_ctor_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Copy_ctor_base<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::<unnamed>.std::__detail::__variant::_Variant_storage<false, CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown>::_M_u) + 8).std::_Vector_base<unsigned char, std::allocator<unsigned char> >::_M_impl.std::_Vector_base<unsigned char, std::allocator<unsigned char> >::_Vector_impl::<anonymous>.std::_Vector_base<unsigned char, std::allocator<unsigned char> >::_Vector_impl_data::_M_end_of_storage’ may be used uninitialized [-Werror=maybe-uninitialized]
    370 |                       _M_impl._M_end_of_storage - _M_impl._M_start);
        |                       ~~~~~~~~^~~~~~~~~~~~~~~~~
  wallet/wallet.cpp: In member function ‘bool wallet::CWallet::IsSpentKey(const CScript&) const’:
  wallet/wallet.cpp:1055:37: note: ‘<anonymous>’ declared here
   1055 |         if (IsAddressPreviouslySpent(pkh_dest)) {
        |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
  ```

  Refactoring the function so that the `WitnessV0KeyHash`, `ScriptHash`, and `PKHash` are constructed within an `if` statement seems to make it go away without changing behavior.

ACKs for top commit:
  maflcko:
    lgtm ACK 17707db939
  l0rinc:
    utACK 17707db939
  hebasto:
    ACK 17707db939, tested on Ubuntu 24.04. The warning has been silenced for both GCC 13.2.0 and 14.0.1.
  tdb3:
    ACK 17707db939
  BrandonOdiwuor:
    Code Review ACK 17707db939

Tree-SHA512: df10a494adfaecb29060f3936d2910aa4291e8caa2a64cbd48e57ed67208f16a02441a14a599a092e0e8de3b338866f666f042b9c6109d0003c0d3e7af4daf48
This commit is contained in:
glozow 2024-08-22 15:06:02 +01:00
commit 5ce2285b87
No known key found for this signature in database
GPG Key ID: BA03F4DBE0C63FB4

View File

@ -1039,21 +1039,20 @@ bool CWallet::IsSpentKey(const CScript& scriptPubKey) const
return true;
}
LegacyScriptPubKeyMan* spk_man = GetLegacyScriptPubKeyMan();
if (!spk_man) return false;
for (const auto& keyid : GetAffectedKeys(scriptPubKey, *spk_man)) {
WitnessV0KeyHash wpkh_dest(keyid);
if (IsAddressPreviouslySpent(wpkh_dest)) {
return true;
}
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
if (IsAddressPreviouslySpent(sh_wpkh_dest)) {
return true;
}
PKHash pkh_dest(keyid);
if (IsAddressPreviouslySpent(pkh_dest)) {
return true;
if (LegacyScriptPubKeyMan* spk_man = GetLegacyScriptPubKeyMan()) {
for (const auto& keyid : GetAffectedKeys(scriptPubKey, *spk_man)) {
WitnessV0KeyHash wpkh_dest(keyid);
if (IsAddressPreviouslySpent(wpkh_dest)) {
return true;
}
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
if (IsAddressPreviouslySpent(sh_wpkh_dest)) {
return true;
}
PKHash pkh_dest(keyid);
if (IsAddressPreviouslySpent(pkh_dest)) {
return true;
}
}
}
return false;