make the buffer slicing checked instead of assumed

This commit is contained in:
2026-09-21 01:27:56 +03:00
parent e6e0d5b2b4
commit 9e23a5b7d3
6 changed files with 40 additions and 15 deletions
+4 -3
View File
@@ -91,9 +91,10 @@ unsafe fn class_of(hwnd: HWND) -> String {
if n <= 0 {
return String::new();
}
// n came back from GetClassNameW as a positive count of units written, and
// buf is 128 units, so this cannot exceed the slice either way.
String::from_utf16_lossy(&buf[..usize::try_from(n).unwrap_or(0)])
// n is a positive count of UTF-16 units written by the call above; getting
// it wrong means an empty class name, never a panic.
let written = usize::try_from(n).ok().and_then(|n| buf.get(..n));
String::from_utf16_lossy(written.unwrap_or_default())
}
/// `EnumWindows` callback: records the first top-level window containing the