make the buffer slicing checked instead of assumed
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user