subprocess: fix command line memory leak on Windows #36581

Manually merged
stickies-v merged 1 commit from 2026-10/win-cmdline-leak into master 2026-10-22 15:34:36 +00:00 AGit
stickies-v commented 2026-10-20 10:04:17 +00:00 (Migrated from github.com)

Popen::execute_process() copied the command line into a buffer from new[], but did not delete it. This leaked memory each time a process started on Windows.

CreateProcessA needs a writable command line buffer. Since C++17, std::string::data() supplies one, so give the std::string buffer 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.

`Popen::execute_process()` copied the command line into a buffer from `new[]`, but did not delete it. This leaked memory each time a process started on Windows. `CreateProcessA` [needs](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#:~:text=characters%2E-,The,violation) a writable command line buffer. Since C++17, `std::string::data()` supplies one, so give the `std::string` buffer 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._
DrahtBot commented 2026-10-20 10:04:24 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK hebasto, winterrdog, jpk68
Stale ACK hodlinator

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36581 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36581.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36581#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [hebasto](https://github.com/bitcoin/bitcoin/pull/36581#pullrequestreview-5616057705), [winterrdog](https://github.com/bitcoin/bitcoin/pull/36581#pullrequestreview-5616100928), [jpk68](https://github.com/bitcoin/bitcoin/pull/36581#issuecomment-6329222272) | | Stale ACK | [hodlinator](https://github.com/bitcoin/bitcoin/pull/36581#pullrequestreview-5600843293) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: f655a8df755330911c6c109c8b1d04ef5dd4ea09
Head: cf30d0a4123bb4237495f04e723ce9f706c2edf2

No verified findings are available from this review.

Review coverage was incomplete.

Review limitations
  • adversarial_deepseek: Stage did not complete (ValueError).

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:a20a0ba90fecee7892027f478dfd14b6e701bab79c156c67c68eec0c29ba9be4:f359184720b19d8f4199eb38e750c73a24dbe6263b5428fbe549b1858030cb9b --> Base: `f655a8df755330911c6c109c8b1d04ef5dd4ea09` Head: `cf30d0a4123bb4237495f04e723ce9f706c2edf2` No verified findings are available from this review. Review coverage was incomplete. <details> <summary>Review limitations</summary> - adversarial_deepseek: Stage did not complete (ValueError). </details> [Full review report](<https://review.fish.foo/traces/36581-cf30d0a4123bb4237495f04e723ce9f706c2edf2-1126-6.html>)
hebasto (Migrated from github.com) reviewed 2026-10-20 10:35:32 +00:00
hebasto (Migrated from github.com) left a comment

Concept ACK.

Thank you!

Concept ACK. Thank you!
hodlinator (Migrated from github.com) approved these changes 2026-10-21 13:56:06 +00:00
hodlinator (Migrated from github.com) left a comment
ACK fe6608590c624042fa4a77a6c9f3a46b70bc646c
hodlinator (Migrated from github.com) commented 2026-10-21 13:54:42 +00:00

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa states:

The system adds a terminating null character to the command-line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.

But that should be fine as I interpret it as the replacement of a char-value, and this is the last place command_line is used.

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa states: > The system adds a terminating null character to the command-line string to separate the file name from the arguments. This divides the original string into two strings for internal processing. But that should be fine as I interpret it as the *replacement* of a `char`-value, and this is the last place `command_line` is used.
jpk68 commented 2026-10-21 23:55:32 +00:00 (Migrated from github.com)
ACK cf30d0a4123bb4237495f04e723ce9f706c2edf2
sedited commented 2026-10-22 07:56:57 +00:00 (Migrated from github.com)

Has this patch been checked on a windows box? I find the null terminator behaviour hard to reason about.

Has this patch been checked on a windows box? I find the null terminator behaviour hard to reason about.
maflcko commented 2026-10-22 08:07:48 +00:00 (Migrated from github.com)
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=address` nightly build with leak sanitizer enabled as well?
hodlinator commented 2026-10-22 08:48:14 +00:00 (Migrated from github.com)

Took it for a spin in a Windows debug build now (had not previously done so).

Overriding through:

command_line = "C:\\Users\\hodlinator\\bitcoin\\build\\bin\\Debug\\bitcoind.exe arg";

There was no null terminator inserted/overwritten for that string:
image

Took it for a spin in a Windows debug build now (had not previously done so). Overriding through: ````c++ command_line = "C:\\Users\\hodlinator\\bitcoin\\build\\bin\\Debug\\bitcoind.exe arg"; ```` There was no null terminator inserted/overwritten for that string: <img width="1342" height="750" alt="image" src="https://git.fish.foo/attachments/290c2b14-d410-485c-848d-a297d2dae1de" />
hodlinator commented 2026-10-22 09:05:28 +00:00 (Migrated from github.com)

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).

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).
stickies-v commented 2026-10-22 09:18:30 +00:00 (Migrated from github.com)

Thanks for the empirical data, @hodlinator! Also, command_line is a local variable and unused after the CreateProcessA call, and the buffer CreateProcessA receives is the same as before - just with a different owner.

Thanks for the empirical data, @hodlinator! Also, `command_line` is a local variable and unused after the `CreateProcessA` call, and the buffer `CreateProcessA` receives is the same as before - just with a different owner.
hebasto (Migrated from github.com) reviewed 2026-10-22 12:16:56 +00:00
hebasto (Migrated from github.com) left a comment

Reproduced on Windows with the following harness:

#include <crtdbg.h>
#include <util/subprocess.h>

std::string SysErrorString(int) { return ""; }

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);

    subprocess::Popen p({"cmd.exe", "/c", "exit", "0"},
                        subprocess::input{subprocess::PIPE},
                        subprocess::output{subprocess::PIPE},
                        subprocess::error{subprocess::PIPE});
    p.wait();
}

On the master branch, it prints:

Detected memory leaks!
Dumping objects ->
{204} normal block at 0x0147E740, 18 bytes long.
 Data: <cmd.exe /c exit > 63 6D 64 2E 65 78 65 20 2F 63 20 65 78 69 74 20
Object dump complete.

With this PR, it prints nothing.

Reproduced on Windows with the following harness: ```c++ #include <crtdbg.h> #include <util/subprocess.h> std::string SysErrorString(int) { return ""; } int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); subprocess::Popen p({"cmd.exe", "/c", "exit", "0"}, subprocess::input{subprocess::PIPE}, subprocess::output{subprocess::PIPE}, subprocess::error{subprocess::PIPE}); p.wait(); } ``` On the master branch, it prints: ``` Detected memory leaks! Dumping objects -> {204} normal block at 0x0147E740, 18 bytes long. Data: <cmd.exe /c exit > 63 6D 64 2E 65 78 65 20 2F 63 20 65 78 69 74 20 Object dump complete. ``` With this PR, it prints nothing.
hebasto (Migrated from github.com) reviewed 2026-10-22 13:16:18 +00:00
hebasto (Migrated from github.com) commented 2026-10-22 13:16:18 +00:00

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?

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?
hebasto (Migrated from github.com) approved these changes 2026-10-22 13:18:38 +00:00
hebasto (Migrated from github.com) left a comment
ACK fe6608590c624042fa4a77a6c9f3a46b70bc646c. Consider https://github.com/bitcoin/bitcoin/pull/36581#discussion_r4348776404 as a nit.
winterrdog (Migrated from github.com) reviewed 2026-10-22 13:19:20 +00:00
winterrdog (Migrated from github.com) left a comment
ACK fe6608590c624042fa4a77a6c9f3a46b70bc646c
hodlinator (Migrated from github.com) reviewed 2026-10-22 13:22:00 +00:00
hodlinator (Migrated from github.com) commented 2026-10-22 13:22:00 +00:00

Ah, it's further up:

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

Ah, it's further up: > The Unicode version of this function, **CreateProcessW**, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.
stickies-v (Migrated from github.com) reviewed 2026-10-22 14:40:56 +00:00
stickies-v (Migrated from github.com) commented 2026-10-22 14:40:56 +00:00

CreateProcessA requires a mutable string (LPSTR) as part of its function signature, and it seems like the implementation then passes a copy to CreateProcessW. 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.

`CreateProcessA` requires a mutable string (`LPSTR`) as part of its function signature, and it [seems](https://devblogs.microsoft.com/oldnewthing/20090601-00/?p=18083/#:~:text=Why%20is,string%21) like the implementation then passes a copy to `CreateProcessW`. 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.
stickies-v commented 2026-10-22 14:41:20 +00:00 (Migrated from github.com)

Force-pushed to remove an unnecessary docstring change, no other changes.

Force-pushed to remove [an unnecessary docstring change](https://github.com/bitcoin/bitcoin/pull/36581#discussion_r4349723923), no other changes.
hebasto (Migrated from github.com) approved these changes 2026-10-22 14:44:00 +00:00
hebasto (Migrated from github.com) left a comment

re-ACK cf30d0a412.

re-ACK cf30d0a4123bb4237495f04e723ce9f706c2edf2.
winterrdog (Migrated from github.com) reviewed 2026-10-22 14:47:05 +00:00
winterrdog (Migrated from github.com) left a comment

reACK cf30d0a412

reACK cf30d0a4123bb4237495f04e723ce9f706c2edf2
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36581
No description provided.