fix: undefined behavior from unaligned reference to packed struct field in system tray
need triaging
### Bug Description
In system_tray/windows.rs, the notify_icon_data function creates a reference to a field of a packed struct `NOTIFYICONDATAW`, which fails to compile.
`NOTIFYICONDATAW` is `#[repr(C, packed)]` (1-byte aligned), so its field szTip ([u16; 128]) may be unaligned. The current code accesses it via slice indexing:
```rust
let n = tip.len().min(data.szTip.len() - 1);
data.szTip[..n].copy_from_slice(&tip[..n]);
```
This implicitly creates &[u16] / &mut [u16] references to the unaligned field. Rust rejects this as a compilation error:
```shell
error[E0793]: reference to field of packed struct is unaligned
--> internal/core/items/system_tray/windows.rs:465:27
|
465 | let n = tip.len().min(data.szTip.len() - 1);
| ^^^^^^^^^^
|
= note: this struct is 1-byte aligned, but the type of this field may require higher alignment
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
```
Fix: use `addr_of_mut!` to obtain a raw pointer without creating a reference, then copy via `copy_nonoverlapping`:
let sz_tip = std::ptr::addr_of_mut!(data.szTip);
let n = tip.len().min(unsafe { (*sz_tip).len() } - 1);
### Reproducible Code (if applicable)
```slint
N/A — this is a compilation error in Slint internals, not user code. Triggered whenever system tray is enabled.
```
### Environment Details
- Slint Version: master (commit c13d8ca91)
- Platform/OS: Windows (all targets — affects both Win32 and Win64)
- Programming Language: Rust
- Backend/Renderer: Skia
### Product Impact
The system tray feature is enabled via SLINT_ENABLE_EXPERIMENTAL_FEATURES=1. This blocks compilation on all both Win32 and Win64 targets.
3 条评论