refactor(windows): improve Unicode handling in `queryVersionString`

* Added a check for zero length in `puLen` to prevent unnecessary slice creation.
* Adjusted the slice length for UTF16 conversion to match the actual length needed.
* Enhanced clarity and safety in handling UTF16 strings.
This commit is contained in:
qianlifeng 2025-07-12 23:40:04 +08:00
parent 871182c029
commit 2f43afedd4
1 changed files with 12 additions and 2 deletions

View File

@ -41,6 +41,7 @@ var (
// Windows constants for icon extraction // Windows constants for icon extraction
const ( const (
SHGFI_ICON = 0x000000100 SHGFI_ICON = 0x000000100
SHGFI_DISPLAYNAME = 0x000000200
SHGFI_LARGEICON = 0x000000000 SHGFI_LARGEICON = 0x000000000
SHGFI_SMALLICON = 0x000000001 SHGFI_SMALLICON = 0x000000001
SHGFI_SYSICONINDEX = 0x000004000 SHGFI_SYSICONINDEX = 0x000004000
@ -480,8 +481,17 @@ func (a *WindowsRetriever) queryVersionString(ctx context.Context, buffer []byte
} }
// Convert UTF16 string to Go string // Convert UTF16 string to Go string
utf16Slice := (*[256]uint16)(unsafe.Pointer(lpBuffer))[:puLen/2] // puLen is already the number of UTF16 characters (not bytes)
return syscall.UTF16ToString(utf16Slice) utf16Length := puLen
if utf16Length == 0 {
return ""
}
// Create a slice with the exact length needed
utf16Slice := (*[1024]uint16)(unsafe.Pointer(lpBuffer))[:utf16Length]
result := syscall.UTF16ToString(utf16Slice)
return result
} }
func (a *WindowsRetriever) GetExtraApps(ctx context.Context) ([]appInfo, error) { func (a *WindowsRetriever) GetExtraApps(ctx context.Context) ([]appInfo, error) {