-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add winpty_get_console_process_list #130
Conversation
I'll try to get to this in a week or two. |
@rprichard friendly ping 😃 |
Looking at this code: auto processCount = GetConsoleProcessList(&processList[0], processList.size());
if (processList.size() < processCount) {
processList.resize(processCount);
processCount = GetConsoleProcessList(&processList[0], processList.size());
} The process list can change while we're trying to read it, so I'd prefer that Otherwise, I'm happy with the PR. |
Thanks, I didn't think of that. It should be fixed now. |
src/agent/Agent.cc
Outdated
while (processList.size() < processCount) { | ||
// Multiplying by two caps the number of iterations | ||
const int newSize = processList.size() * 2; | ||
if (newSize <= processList.size()) { // Ensure we fail when new size overflows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this code were to overflow, processList.size()
would be 0x40000000, and newSize
would be -0x80000000 (i.e. INT_MIN
). newSize
would be greater than processList.size()
because it would be converted from int to size_t for the comparison.
I'd prefer to replace "const int newSize = ... ; ... if { ... }" with:
const auto newSize = std::max<DWORD>(processList.size() * 2, processCount);
If you do, also add #include <algorithm>
to the top.
Overflow shouldn't ever happen. If it somehow does, then there is one code path handling it -- std::vector::resize
throws std::bad_alloc
.
I used std::max as you said... |
ok, thanks! |
🎉 |
For a better implementation of microsoft/vscode#30152 and microsoft/vscode#29926 we need a list of processes attached to the console (instead of using a full process tree).
GetConsoleProcessList
needs to be called from a process attached to the console. Since winpty-agent is already attached to it, it seems to be a good place to call it from.