From 1e79a98b2eea4470f6279268dacee9668a901a03 Mon Sep 17 00:00:00 2001 From: leileizhang Date: Fri, 20 Jun 2025 17:07:41 +0800 Subject: [PATCH] [CmdPal] Fix apps using incorrect AsSpan usage after CsWin32 upgrade (#40156) ## Summary of the Pull Request Root Cause: After the CsWin32 upgrade, the code switched from using manually allocated unmanaged buffers (Marshal.AllocHGlobal) to using Span via outBuffer.AsSpan(). However, the Span length passed to SHLoadIndirectString was not correctly calculated. ## PR Checklist - [ ] **Closes:** #xxx - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../Programs/UWPApplication.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs index 0915ea05dc..be973f3431 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs @@ -218,13 +218,12 @@ public class UWPApplication : IProgram return string.Empty; } - var capacity = 1024U; - PWSTR outBuffer = new PWSTR((char*)(void*)Marshal.AllocHGlobal((int)capacity * sizeof(char))); + Span outBuffer = stackalloc char[1024]; var source = $"@{{{packageFullName}? {parsed}}}"; try { - PInvoke.SHLoadIndirectString(source, outBuffer.AsSpan()).ThrowOnFailure(); + PInvoke.SHLoadIndirectString(source, outBuffer).ThrowOnFailure(); var loaded = outBuffer.ToString(); return string.IsNullOrEmpty(loaded) ? string.Empty : loaded; @@ -234,7 +233,7 @@ public class UWPApplication : IProgram try { var sourceFallback = $"@{{{packageFullName}?{parsedFallback}}}"; - PInvoke.SHLoadIndirectString(sourceFallback, outBuffer.AsSpan()).ThrowOnFailure(); + PInvoke.SHLoadIndirectString(sourceFallback, outBuffer).ThrowOnFailure(); var loaded = outBuffer.ToString(); return string.IsNullOrEmpty(loaded) ? string.Empty : loaded; } @@ -243,13 +242,6 @@ public class UWPApplication : IProgram // ProgramLogger.Exception($"Unable to load resource {resourceReference} from {packageFullName}", new InvalidOperationException(), GetType(), packageFullName); return string.Empty; } - finally - { - } - } - finally - { - Marshal.FreeHGlobal((IntPtr)outBuffer.Value); } } else