The Wayback Machine - https://web.archive.org/web/20220525062450/https://github.com/microsoft/terminal/pull/12340
Skip to content
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

Merged
merged 1 commit into from Feb 15, 2022

Conversation

zadjii-msft
Copy link
Member

@zadjii-msft zadjii-msft commented Feb 3, 2022

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:

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.

  • Closes #6759
  • I work here.
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.
@msftbot msftbot bot added Area-CodeHealth Issue-Bug Priority-2 Product-Conhost labels Feb 3, 2022
lhecker
lhecker approved these changes Feb 7, 2022
NTSTATUS Status = STATUS_SUCCESS;

// Port handle and name.
HANDLE PortHandle;
UNICODE_STRING PortName;
static UNICODE_STRING PortName = RTL_CONSTANT_STRING(CIS_ALPC_PORT_NAME);
Copy link
Member

@lhecker lhecker Feb 7, 2022

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.

Copy link
Member

@DHowett DHowett Feb 8, 2022

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

Copy link
Member

@DHowett DHowett Feb 8, 2022

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,
Copy link
Member

@lhecker lhecker Feb 8, 2022

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?

Copy link
Member

@miniksa miniksa left a comment

I'd rather it just stay the static at this point too.

@lhecker lhecker added the AutoMerge label Feb 15, 2022
@msftbot
Copy link
Contributor

@msftbot msftbot bot commented Feb 15, 2022

Hello @lhecker!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

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 (@msftbot) and give me an instruction to get started! Learn more here.
@msftbot msftbot bot merged commit 349b767 into main Feb 15, 2022
11 checks passed
@msftbot msftbot bot deleted the dev/migrie/b/6759-this-doesnt-even-build-in-oss branch Feb 15, 2022
DHowett pushed a commit that referenced this issue Mar 24, 2022
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)
@msftbot
Copy link
Contributor

@msftbot msftbot bot commented May 24, 2022

🎉Windows Terminal v1.13.1143 has been released which incorporates this pull request.🎉

Handy links:

@msftbot
Copy link
Contributor

@msftbot msftbot bot commented May 24, 2022

🎉Windows Terminal Preview v1.14.143 has been released which incorporates this pull request.🎉

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CodeHealth AutoMerge Issue-Bug Priority-2 Product-Conhost
4 participants