Fix 8.22.0 HD Wallet Import Crash Bug #282
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Crash on Inactive HD Seed Key Top-Up When
meta.key_origin.pathis Too ShortSummary
This pull request fixes a crash in the DigiByte Core wallet (v8.22.0) that occurrs during rescanning and reindexing of older HD wallets. The issue was identified in the
LegacyScriptPubKeyMan::MarkUnusedAddressesmethod, where the wallet attempted to access elements[1]and[2]ofmeta.key_origin.pathwithout verifying that the vector contained at least three elements. This oversight resulted in an out-of-range memory access, causing the crash.Over recent weeks, we isolated a bug affecting a particular wallet with over 5,000 transactions (originally created in v7.17.2/3). Investigation revealed that older wallet metadata did not always include the complete three-component derivation path.
Notably, similar issues were encountered by BTC in their versions v21 and v22 (wallets originally created with v0.17). Although BTC provided a more comprehensive fix in v23 (Bitcoin PR #23304), we are unable to integrate that solution fully until the broader changes planned for v8.23 are implemented.
This bug was reproduced on Windows and Mac v8.22.0 core wallets multiple times.
This temporary fix has been thoroughly tested on our test case wallet and was also previously reviewed by BTC developers (see Bitcoin PR #23277 for context). We plan to upgrade to v8.23 as soon as possible after taproot activation for more robust HD wallet handling.
The Crash Issue
Debug Backtrace
Debug Backtrace
Below is the relevant backtrace excerpt observed in LLDB:
In this scenario, the code assumed that meta.key_origin.path contained at least three elements, which was not the case for older HD wallets.
Why It Was Bad
Out-of-Range Access: When meta.key_origin.path.size() < 3, accessing elements [1] and [2] resulted in reading beyond allocated memory.
Legacy Metadata Vulnerability: Wallets created with older DigiByte versions or those partially upgraded had incomplete derivation paths, leading to crashes during rescanning or reindexing.
How We Fixed It
The fix adds a conditional check to verify that meta.key_origin.path contains at least three elements before accessing indices [1] and [2]. If the path is too short, the code logs a warning and skips the call to TopUpInactiveHDChain, thereby avoiding the crash.
Code Diff
Original Code:
digibyte/src/wallet/scriptpubkeyman.cpp
Lines 372 to 382 in 074fd08
Testing & Future Considerations
Rescanning or Reindexing: We have verified that the crash no longer occurs on older wallets with incomplete derivation paths. Instead, a warning is logged when an insufficient path size is detected.
Future Enhancements: In the future, it may be beneficial to convert or augment legacy seed metadata to ensure all necessary path components are present. For now, this patch prevents a crash by failing gracefully.
All 470 Unit Tests Pass:
All 215 Functional Tests Pass:
Acknowledgements
Thank you to everyone who assisted in debugging this issue and testing the fix, especially Brian. Your contributions have been essential in resolving this critical crash bug.
Please review and merge this pull request. We plan to integrate a more comprehensive upgrade in v8.23 following taproot activation to further stabilize HD wallets.