Review report

bitcoin/bitcoin: PR #36031

1 minor finding · 1 suggestion

Findings

Link

[minor] · Passphrase APIs dereference an empty encryption key on unencrypted wallets

Direct passphrase API calls on unencrypted wallets now dereference an empty optional instead of returning an error, but no current caller producing this input was found.

Location: src/wallet/wallet.cpp:615 (head).

Details and suggested fix

CWallet::Unlock and ChangeWalletPassphrase pass *m_encryption_key to DecryptMasterKey without checking engagement. On an unencrypted wallet this is undefined behavior and may crash; the previous empty-map loops returned UnlockPassphraseError. The earlier IsLocked() and Lock() calls in ChangeWalletPassphrase only return false in this state and do not stop execution. WalletImpl forwards both operations directly, and interfaces::Wallet documents no encrypted-wallet precondition. The inspected RPC and GUI paths check encryption state first. Add an empty-key check under cs_wallet in both methods and return an error before dereferencing, preserving the previous defined failure behavior.


Optional improvements

Link

[suggestion] · Nondefault mkey IDs and multiple-record rejection lack regression coverage

The new ID-restoration and multiple-record rejection paths lack regression tests, so losing a nondefault ID during a passphrase change could go unnoticed until the wallet fails to reload.

Location: src/wallet/walletdb.cpp:422 (head).

Details and suggested fix

m_id defaults to 1 and is not serialized, so the assignment here preserves a non-1 database record key. ChangeWalletPassphrase copies it and WriteMasterKey uses it to overwrite the existing record. The current code is correct, but change_passphrase_master_key_write_failure starts with EncryptWallet and ID 1, then never reloads. The functional fixture injects only an ID-1 key that load-time cleanup removes. Omitting this assignment would escape those tests and make a passphrase change add an ID-1 record beside the original, causing CORRUPT on reload. Add a round-trip fixture with exactly one non-1 mkey and matching encrypted private keys: unlock, change the passphrase, reload, and assert that only the original ID remains and only the new passphrase works. Also test that two distinct mkey records return CORRUPT without changing either record.


Coverage details