-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Fix a memory leak in onecore interactivity #12340
Conversation
As noted in #6759: > `RtlCreateUnicodeString` creates a copy of the string on the process heap and the `PortName` variable has local-scope. The string doesn't get freed with `RtlFreeUnicodeString` before the function returns creating a memory leak. > `CIS_ALPC_PORT_NAME` is a constant string and the `PortName` variable should instead be initialized using the `RTL_CONSTANT_STRING` macro: > > ```c++ > static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME); > ``` I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows. * [x] Closes #6759 * I work here.
NTSTATUS Status = STATUS_SUCCESS; | ||
|
||
// Port handle and name. | ||
HANDLE PortHandle; | ||
UNICODE_STRING PortName; | ||
static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME); |
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.
static constexpr
? If not then static const
.
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 constexpr is possible, let's do it. I can validate this in a quick razzle build (if @zadjii-msft wants that!)
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.
This requires a deadly const_cast -- if that is okay, let's do constexpr!
diff --git a/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp b/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
index cdeebed38c365..6c1b4ffc465b0 100644
--- a/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
+++ b/onecore/windows/core/console/open/src/interactivity/onecore/ConIoSrvComm.cpp
@@ -153,7 +145,7 @@
// Request to connect to the server.
ConnectionMessageLength = sizeof(CIS_MSG);
Status = NtAlpcConnectPort(&PortHandle,
- &PortName,
+ const_cast<PUNICODE_STRING>(&PortName),
NULL,
&PortAttributes,
ALPC_MSGFLG_SYNC_REQUEST,
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.
In that case I'd personally use a regular static... Better safe than sorry, since it's possible it might do reference counting on it, right?
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.
I'd rather it just stay the static at this point too.
Hello @lhecker! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
As noted in #6759: > `RtlCreateUnicodeString` creates a copy of the string on the process heap and the `PortName` variable has local-scope. The string doesn't get freed with `RtlFreeUnicodeString` before the function returns creating a memory leak. > `CIS_ALPC_PORT_NAME` is a constant string and the `PortName` variable should instead be initialized using the `RTL_CONSTANT_STRING` macro: > > ```c++ > static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME); > ``` I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows. * [x] Closes #6759 * I work here. (cherry picked from commit 349b767)
🎉 Handy links: |
🎉 Handy links: |
As noted in #6759:
I actually built this in the OS repo to make sure it'll still build, because this code doesn't even build outside Windows.