Skip to content

fix the leaks of the wallet and its queries, and the range of get_blocks_by_range - #184

Open
mycomputercrashsalot wants to merge 9 commits into
woodser:masterfrom
mycomputercrashsalot:todo-fixes
Open

mycomputercrashsalot wants to merge 9 commits into
woodser:masterfrom
mycomputercrashsalot:todo-fixes

Conversation

@mycomputercrashsalot

@mycomputercrashsalot mycomputercrashsalot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Six fixes for problems that monero-c's TODO documents against monero-cpp, each measured before and after. The leak evidence is malloc-zone accounting per call on macOS (LeakSanitizer does not exist there); the Linux CI can confirm with LSan.

utils: limit the json nesting that deserialize accepts (aa3b2c4)
gen_utils::deserialize() parses with boost's read_json(), which recurses once per level: a daemon that answers with 100000 levels of [[[[... overflows the stack and ends the process (20000 levels pass, 100000 crash; a thread with the 1 MB stack that is the Windows default crashes at 8000). The fix scans the nesting before parsing and throws Invalid JSON above 64 levels, the same limit monero-c checks at its boundary.

wallet: free the wallet when a factory throws (8ea5ff8)
open_wallet(), the create_wallet*() functions of monero_wallet_full and the factories of monero_wallet_keys allocate with a raw new, so an exception after the allocation leaks the wallet and its wallet2: every open with a wrong password leaks about 200 KB (measured 246,511 bytes; 257,439 for a create over an existing file). The factories hold the wallet in a std::unique_ptr and release() on return: both shapes leak 0 now. The // TODO: delete wallet pointer if exception comment at monero_wallet_full.cpp:1159 is done.

wallet: free the txs that get_txs filters out (068e12f)
get_txs_aux() erases the txs that fail a transfer, input or output query without releasing them (9.7 KB for 5 txs, 243 KB for 100, per call). The erased tx detaches from its block first — monero_utils::free(tx) would reset the links of the sibling txs that stay in the result — then drops its reference, which frees it when nothing else holds it.

utils: free the miner tx, the io of a plain tx and the input query of a tx query (f07f686)
monero_utils::free(block) left the miner tx of the block, the inputs and outputs of a plain monero_tx as the daemon builds it, and the input query of a tx query unfreed: 4,976 bytes per block and 1,600 per query. A common free_tx_links() releases them; on the live wallet matrix the query shapes that kept 28,080 to 35,520 bytes per call keep 0. Only shared_ptr::reset() is used, so a link that a caller already reset is an idempotent no-op — nothing is freed twice.

wallet: free the copied output query when the call throws too (11d809a)
get_outputs_aux() copies the tx query and frees the copy on return, but a throw between the copy and the return leaked it, and the copy of an input query points back at the copy of the tx query (3,200 bytes per call on {"txQuery":{"inputQuery":{"isSpent":true}}}). The free runs in a scope-leave handler right after the copy, the idiom get_txs_aux() uses.

daemon: clamp and chunk the range of get_blocks_by_range (c30a46a)
The loop that lists every height never ends when end is UINT64_MAX, and it fills a vector with each height before the first request. The end is clamped to the top of the chain, and the heights are fetched in chunks of 1000, so no vector of the whole range is built. Probed on a regtest chain of 1051 blocks: range(0, UINT64_MAX) returns all 1051 (two chunks, no hang), a normal range returns the same blocks as before, an inverted range is empty.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant