convert the last lossy casts to checked ones
This commit is contained in:
@@ -152,7 +152,9 @@ impl TonemapPipeline {
|
||||
-1.0, 3.0, 0.0, -1.0, // top-left (off-screen)
|
||||
];
|
||||
let vb_desc = D3D11_BUFFER_DESC {
|
||||
ByteWidth: size_of_val(&verts) as u32,
|
||||
// verts is a 12-float array, so the size is a compile-time constant
|
||||
// well inside u32.
|
||||
ByteWidth: u32::try_from(size_of_val(&verts)).unwrap_or(0),
|
||||
Usage: D3D11_USAGE_IMMUTABLE,
|
||||
BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32,
|
||||
CPUAccessFlags: 0,
|
||||
|
||||
+13
-1
@@ -44,6 +44,18 @@ pub(crate) fn round_u64(v: f64) -> u64 {
|
||||
out
|
||||
}
|
||||
|
||||
/// Round a logical-pixel measurement to the signed int a Win32 call wants.
|
||||
/// DPI scales here are at most a handful, so the result is nothing like i32
|
||||
/// range; clamping just makes that explicit.
|
||||
pub(crate) fn round_i32(v: f64) -> i32 {
|
||||
#[allow(
|
||||
clippy::cast_possible_truncation,
|
||||
reason = "value is clamped into i32 range on the line above the cast"
|
||||
)]
|
||||
let out = v.round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32;
|
||||
out
|
||||
}
|
||||
|
||||
/// Progress update sent during encoding.
|
||||
/// pass: 1-based pass number (1 or 2 for two-pass video, 1 for GIF).
|
||||
/// `total_passes`: total number of passes.
|
||||
@@ -614,7 +626,7 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
let pass_start = std::time::Instant::now();
|
||||
encode_gif_pass(
|
||||
ffmpeg, req, scale_width, effective_fps,
|
||||
(attempt as u8) + 1, max_attempts,
|
||||
(u8::try_from(attempt).unwrap_or(0)) + 1, max_attempts,
|
||||
)?;
|
||||
tracing::info!(
|
||||
attempt = attempt + 1,
|
||||
|
||||
+23
-19
@@ -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();
|
||||
|
||||
@@ -28,7 +28,7 @@ unsafe fn visible_window_rect(hwnd: HWND) -> Option<RECT> {
|
||||
hwnd,
|
||||
DWMWA_EXTENDED_FRAME_BOUNDS,
|
||||
(&raw mut rect).cast::<std::ffi::c_void>(),
|
||||
size_of::<RECT>() as u32,
|
||||
u32::try_from(size_of::<RECT>()).unwrap_or(0),
|
||||
)
|
||||
};
|
||||
if r.is_ok() && rect.right > rect.left && rect.bottom > rect.top {
|
||||
@@ -91,7 +91,9 @@ unsafe fn class_of(hwnd: HWND) -> String {
|
||||
if n <= 0 {
|
||||
return String::new();
|
||||
}
|
||||
String::from_utf16_lossy(&buf[..n as usize])
|
||||
// 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)])
|
||||
}
|
||||
|
||||
/// `EnumWindows` callback: records the first top-level window containing the
|
||||
|
||||
@@ -62,14 +62,16 @@ unsafe extern "system" fn enum_cb(hwnd: HWND, lparam: LPARAM) -> BOOL {
|
||||
if title_len <= 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let mut buf = vec![0u16; (title_len as usize) + 1];
|
||||
// title_len is a positive UTF-16 count from the call above.
|
||||
let mut buf = vec![0u16; usize::try_from(title_len).unwrap_or(0) + 1];
|
||||
// SAFETY: `buf` has room for title_len + 1 UTF-16 units, which is what the
|
||||
// call is documented to write at most; the result is bounds-checked below.
|
||||
let copied = unsafe { GetWindowTextW(hwnd, &mut buf) };
|
||||
if copied <= 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let title = String::from_utf16_lossy(&buf[..copied as usize]);
|
||||
// copied is a positive count bounded by buf.len(), checked above.
|
||||
let title = String::from_utf16_lossy(&buf[..usize::try_from(copied).unwrap_or(0)]);
|
||||
if title.trim().is_empty() {
|
||||
return BOOL(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user