convert the last lossy casts to checked ones

This commit is contained in:
2026-09-21 01:22:16 +03:00
parent cd583ba02b
commit d297ec4271
5 changed files with 47 additions and 25 deletions
+23 -19
View File
@@ -366,9 +366,11 @@ fn adjust_audio_length(path: &Path, sample_rate: u32, channels: u16, target_secs
let zeros = vec![0u8; 64 * 1024];
let mut remaining = target_bytes - current_bytes;
while remaining > 0 {
let to_write = remaining.min(zeros.len() as u64) as usize;
// zeros.len() is 64KiB, so both directions are lossless here.
let chunk = u64::try_from(zeros.len()).unwrap_or(u64::MAX);
let to_write = usize::try_from(remaining.min(chunk)).unwrap_or(zeros.len());
file.write_all(&zeros[..to_write])?;
remaining -= to_write as u64;
remaining -= u64::try_from(to_write).unwrap_or(0);
}
} else {
let file = std::fs::OpenOptions::new().write(true).open(path)?;
@@ -399,9 +401,11 @@ fn pad_audio_head(path: &Path, sample_rate: u32, channels: u16, head_secs: f64)
let zeros = vec![0u8; 64 * 1024];
let mut remaining = pad_bytes;
while remaining > 0 {
let to_write = remaining.min(zeros.len() as u64) as usize;
// zeros.len() is 64KiB, so both directions are lossless here.
let chunk = u64::try_from(zeros.len()).unwrap_or(u64::MAX);
let to_write = usize::try_from(remaining.min(chunk)).unwrap_or(zeros.len());
out.write_all(&zeros[..to_write])?;
remaining -= to_write as u64;
remaining -= u64::try_from(to_write).unwrap_or(0);
}
out.flush()?;
// Append the original PCM in a streaming copy - never holds the file.
@@ -1228,8 +1232,8 @@ fn start_overlay_clickthrough_poll(
// SAFETY: `raw` is the overlay's own live HWND from Tauri.
let dpi = unsafe { GetDpiForWindow(HWND(raw.0.cast::<std::ffi::c_void>())) };
let scale = if dpi > 0 { f64::from(dpi) / 96.0 } else { 1.0 };
let border = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let border = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
let titlebar = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
let inner_left = vl + border;
let inner_top = vt + border + titlebar;
@@ -1304,7 +1308,7 @@ fn show_overlay(app: AppHandle) -> Result<(), String> {
let primary = unsafe { MonitorFromPoint(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY) };
// cbSize is required by GetMonitorInfoW and is set from the
// actual struct size, so the write below stays in bounds.
let mut mi = MONITORINFO { cbSize: size_of::<MONITORINFO>() as u32, ..Default::default() };
let mut mi = MONITORINFO { cbSize: u32::try_from(size_of::<MONITORINFO>()).unwrap_or(0), ..Default::default() };
// SAFETY: `mi` is a live local of the declared size, and the
// result is checked with as_bool() before mi is read.
if unsafe { GetMonitorInfoW(primary, &raw mut mi) }.as_bool() {
@@ -1413,10 +1417,10 @@ fn enforce_overlay_aspect(app: AppHandle, state: State<'_, AppState>) -> Result<
let raw = win.hwnd().map_err(|e| format!("hwnd: {e}"))?;
let hwnd = HWND(raw.0.cast::<std::ffi::c_void>());
// Compute outer size including borders
let border = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let border = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
let titlebar = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
let outer_w = sz.width.cast_signed().max(2);
let outer_h = (target_h * scale + f64::from(titlebar) + f64::from(border) * 2.0).round() as i32;
let outer_h = encode::round_i32(target_h * scale + f64::from(titlebar) + f64::from(border) * 2.0);
// SAFETY: `hwnd` is the overlay's live handle; the flags keep the resize
// non-activating and non-reordering, and no pointers are involved.
unsafe {
@@ -1484,7 +1488,7 @@ fn open_window_picker(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
(&raw mut dr).cast::<RECT>().cast::<std::ffi::c_void>(),
size_of::<RECT>() as u32,
u32::try_from(size_of::<RECT>()).unwrap_or(0),
)
});
tracing::debug!(
@@ -1531,7 +1535,7 @@ fn visible_window_bounds(hwnd_raw: isize) -> Option<(i32, i32, i32, i32)> {
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
(&raw mut dwm).cast::<std::ffi::c_void>(),
size_of::<RECT>() as u32,
u32::try_from(size_of::<RECT>()).unwrap_or(0),
)
}
.is_ok()
@@ -1567,7 +1571,7 @@ fn dwm_insets(hwnd_raw: isize) -> (i32, i32, i32, i32) {
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
(&raw mut dwm).cast::<std::ffi::c_void>(),
size_of::<RECT>() as u32,
u32::try_from(size_of::<RECT>()).unwrap_or(0),
)
};
if r.is_err() || dwm.right <= dwm.left || dwm.bottom <= dwm.top {
@@ -1633,8 +1637,8 @@ fn set_overlay_to_window(
let cx = x.saturating_add(width.cast_signed() / 2);
let cy = y.saturating_add(height.cast_signed() / 2);
let scale = scale_factor_at_point(cx, cy);
let border_phys = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar_phys = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let border_phys = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
let titlebar_phys = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
// Desired visible bounds of the overlay window (the green frame).
// The capture region is inside the border + below the titlebar.
@@ -1688,8 +1692,8 @@ fn get_overlay_rect(app: AppHandle) -> Result<CaptureRect, String> {
let (vl, vt, vr, vb) = visible_window_bounds(raw.0 as isize)
.ok_or_else(|| "GetWindowRect failed".to_string())?;
let border_phys = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar_phys = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let border_phys = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
let titlebar_phys = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
let inset_top = border_phys + titlebar_phys;
let inset_sides = border_phys;
@@ -1905,8 +1909,8 @@ async fn export(app: AppHandle, state: State<'_, AppState>, request: ExportReque
.iter()
.map(|c| clicks::Click {
time_secs: c.time_secs,
x: (f64::from(c.x - src_origin.0) * scale).round() as i32,
y: (f64::from(c.y - src_origin.1) * scale).round() as i32,
x: encode::round_i32(f64::from(c.x - src_origin.0) * scale),
y: encode::round_i32(f64::from(c.y - src_origin.1) * scale),
button: c.button,
})
.collect();