mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:44:38 +00:00
subprocess: fix command line memory leak on Windows #36581
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36581
Loading…
Reference in a new issue
No description provided.
Delete branch "2026-10/win-cmdline-leak"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Popen::execute_process()copied the command line into a buffer fromnew[], but did not delete it. This leaked memory each time a process started on Windows.CreateProcessAneeds a writable command line buffer. Since C++17,std::string::data()supplies one, so give thestd::stringbuffer directly and remove the copy.This only affects Windows. It is only reachable if
-signer=<cmd>is set, through authenticated RPC or the GUI. Each call leaks only the startup-defined<cmd>, and the rate is limited by process spawn time, so the impact is limited.edit: this was fixed upstream in https://github.com/arun11299/cpp-subprocess/pull/131, but since this codebase is C++20 it seems better to avoid the copy (and the const cast). A direct backport isn't possible anyway, since #35704 switched us to CreateProcessA.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Base:
f655a8df755330911c6c109c8b1d04ef5dd4ea09Head:
cf30d0a4123bb4237495f04e723ce9f706c2edf2No verified findings are available from this review.
Review coverage was incomplete.
Review limitations
Full review report
Concept ACK.
Thank you!
ACK
fe6608590chttps://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa states:
But that should be fine as I interpret it as the replacement of a
char-value, and this is the last placecommand_lineis used.ACK
cf30d0a412Has this patch been checked on a windows box? I find the null terminator behaviour hard to reason about.
You mean outside the Windows CI, which covers
rpc_singer.py?https://github.com/bitcoin/bitcoin/actions/runs/39405290402/job/118569619457?pr=36581#step:14:1185 and https://github.com/bitcoin/bitcoin/actions/runs/39405290402/job/118571318163?pr=36581#step:11:418
Maybe there could be a Windows
/fsanitize=addressnightly build with leak sanitizer enabled as well?Took it for a spin in a Windows debug build now (had not previously done so).
Overriding through:
There was no null terminator inserted/overwritten for that string:

Ran it again and set a memory breakpoint on the space-char before "arg", but it didn't fire, so it wasn't temporarily replaced with NUL and then set back to space (as far as the debugger could observe, I confirmed it would break when our code modified memory).
Thanks for the empirical data, @hodlinator! Also,
command_lineis a local variable and unused after theCreateProcessAcall, and the bufferCreateProcessAreceives is the same as before - just with a different owner.Reproduced on Windows with the following harness:
On the master branch, it prints:
With this PR, it prints nothing.
The docs only state that CreateProcessW can modify the string. They don’t say either way for CreateProcessA.
So maybe no need to amend the code comment?
ACK
fe6608590c.Consider https://github.com/bitcoin/bitcoin/pull/36581#discussion_r4348776404 as a nit.
ACK
fe6608590cAh, it's further up:
CreateProcessArequires a mutable string (LPSTR) as part of its function signature, and it seems like the implementation then passes a copy toCreateProcessW. But I agree that perhaps the docstring change is confusing,CreateProcessA's function signature is enough, what it does under the hood is not our concern. Force-pushed.Force-pushed to remove an unnecessary docstring change, no other changes.
re-ACK
cf30d0a412.reACK
cf30d0a412