From a088f91541ea18c637c368706443793739acdc46 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 14 Dec 2024 11:17:29 +0800 Subject: [PATCH] Add comments for string truncation --- Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs | 1 + Flow.Launcher/Helper/WindowsInteropHelper.cs | 2 ++ Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs | 1 + .../Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs | 2 ++ 4 files changed, 6 insertions(+) diff --git a/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs b/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs index 191a76309..9639f1b1a 100644 --- a/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs +++ b/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs @@ -62,6 +62,7 @@ namespace Flow.Launcher.Plugin.SharedCommands return string.Empty; } + // Truncate the buffer to the actual length of the string int validLength = Array.IndexOf(buffer, '\0'); if (validLength < 0) validLength = capacity; return new string(buffer, 0, validLength); diff --git a/Flow.Launcher/Helper/WindowsInteropHelper.cs b/Flow.Launcher/Helper/WindowsInteropHelper.cs index ae48034e8..6656b0c5b 100644 --- a/Flow.Launcher/Helper/WindowsInteropHelper.cs +++ b/Flow.Launcher/Helper/WindowsInteropHelper.cs @@ -61,6 +61,8 @@ public class WindowsInteropHelper fixed (char* pBuffer = buffer) { PInvoke.GetClassName(hWnd, pBuffer, capacity); + + // Truncate the buffer to the actual length of the string int validLength = Array.IndexOf(buffer, '\0'); if (validLength < 0) validLength = capacity; windowClass = new string(buffer, 0, validLength); diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs index 6df12fae9..abc254d86 100644 --- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs @@ -106,6 +106,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller return string.Empty; } + // Truncate the buffer to the actual length of the string int validLength = Array.IndexOf(buffer, '\0'); if (validLength < 0) validLength = (int)capacity; return new string(buffer, 0, validLength); diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs index fae9c84c9..05cbfd2b1 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/ShellLinkHelper.cs @@ -38,6 +38,8 @@ namespace Flow.Launcher.Plugin.Program.Programs fixed (char* bufferChar = buffer) { ((IShellLinkW)link).GetPath((PWSTR)bufferChar, MAX_PATH, &data, (uint)SLGP_FLAGS.SLGP_SHORTPATH); + + // Truncate the buffer to the actual length of the string int validLength = Array.IndexOf(buffer, '\0'); if (validLength < 0) validLength = MAX_PATH; target = new string(buffer, 0, validLength);