make the buffer slicing checked instead of assumed
This commit is contained in:
@@ -417,14 +417,22 @@ impl TonemapPipeline {
|
||||
mapped.pData.cast::<u8>(),
|
||||
(mapped.RowPitch as usize) * (self.height as usize),
|
||||
);
|
||||
// RowPitch may exceed width*4 (alignment); copy row by row.
|
||||
// RowPitch may exceed width*4 (alignment); copy row by row. Every
|
||||
// slice is built from the mapped pitch and this pipeline's own
|
||||
// height, and a short buffer yields an error rather than a panic.
|
||||
let packed = (row_bytes as usize) * (self.height as usize);
|
||||
if mapped.RowPitch == row_bytes {
|
||||
out.write_all(&src_slice[..(row_bytes as usize) * (self.height as usize)])
|
||||
let Some(frame) = src_slice.get(..packed) else {
|
||||
return Err(anyhow!("mapped frame smaller than expected"));
|
||||
};
|
||||
out.write_all(frame)
|
||||
.map_err(|e| anyhow!("writing tonemapped frame: {e}"))?;
|
||||
} else {
|
||||
let pitch = mapped.RowPitch as usize;
|
||||
for y in 0..(self.height as usize) {
|
||||
let row = &src_slice[y * pitch..y * pitch + row_bytes as usize];
|
||||
let Some(row) = src_slice.get(y * pitch..y * pitch + row_bytes as usize) else {
|
||||
return Err(anyhow!("mapped row {y} outside the mapped buffer"));
|
||||
};
|
||||
out.write_all(row)
|
||||
.map_err(|e| anyhow!("writing tonemapped frame: {e}"))?;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,13 @@ pub(crate) fn start() -> ClickRecorder {
|
||||
let mut last_pressed = [false; 3];
|
||||
|
||||
while running_clone.load(Ordering::Relaxed) {
|
||||
for (i, &(vk, button)) in buttons.iter().enumerate() {
|
||||
// The buttons table and this flag array are both three long and are
|
||||
// built together, so the index is in range by construction.
|
||||
#[allow(
|
||||
clippy::indexing_slicing,
|
||||
reason = "buttons and last_pressed are both fixed at three entries"
|
||||
)]
|
||||
for (i, &(vk, button)) in buttons.iter().enumerate() {
|
||||
// SAFETY: both calls are self-contained Win32 queries. `pt` is
|
||||
// a local that GetCursorPos writes, and it is only read after
|
||||
// the call reports success.
|
||||
|
||||
@@ -152,7 +152,9 @@ fn click_filters(clicks: &[Click], clip_w: u32, clip_h: u32) -> Option<String> {
|
||||
MAX_CLICK_HIGHLIGHTS
|
||||
);
|
||||
}
|
||||
for click in &clicks[..clicks_to_render] {
|
||||
// clicks_to_render is clicks.len().min(MAX_CLICK_HIGHLIGHTS), and get()
|
||||
// falls back to the whole slice rather than panicking.
|
||||
for click in clicks.get(..clicks_to_render).unwrap_or(clicks) {
|
||||
// Clicks recorded outside the capture region (or outside the crop) land
|
||||
// behind the origin. Clamping here keeps the box inside the frame;
|
||||
// without it the offsets went negative and drawbox dropped the box.
|
||||
@@ -1098,8 +1100,8 @@ pub(crate) fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path
|
||||
if segments.is_empty() {
|
||||
bail!("no segments to concatenate");
|
||||
}
|
||||
if segments.len() == 1 {
|
||||
std::fs::copy(&segments[0], output).context("copying single segment")?;
|
||||
if let [only] = segments {
|
||||
std::fs::copy(only, output).context("copying single segment")?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
@@ -377,7 +377,9 @@ fn adjust_audio_length(path: &Path, sample_rate: u32, channels: u16, target_secs
|
||||
while remaining > 0 {
|
||||
// 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());
|
||||
let to_write = usize::try_from(remaining.min(chunk))
|
||||
.unwrap_or(zeros.len())
|
||||
.min(zeros.len());
|
||||
file.write_all(&zeros[..to_write])?;
|
||||
remaining -= u64::try_from(to_write).unwrap_or(0);
|
||||
}
|
||||
@@ -412,7 +414,9 @@ fn pad_audio_head(path: &Path, sample_rate: u32, channels: u16, head_secs: f64)
|
||||
while remaining > 0 {
|
||||
// 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());
|
||||
let to_write = usize::try_from(remaining.min(chunk))
|
||||
.unwrap_or(zeros.len())
|
||||
.min(zeros.len());
|
||||
out.write_all(&zeros[..to_write])?;
|
||||
remaining -= u64::try_from(to_write).unwrap_or(0);
|
||||
}
|
||||
@@ -1794,8 +1798,10 @@ fn read_log_tail(log_dir: &Path) -> String {
|
||||
}
|
||||
let Some((_, path)) = newest else { return String::new() };
|
||||
let Ok(data) = std::fs::read(&path) else { return String::new() };
|
||||
// saturating_sub keeps start at or below data.len(), so the tail slice
|
||||
// always exists; get() just makes that explicit.
|
||||
let start = data.len().saturating_sub(TAIL_BYTES);
|
||||
String::from_utf8_lossy(&data[start..]).into_owned()
|
||||
String::from_utf8_lossy(data.get(start..).unwrap_or_default()).into_owned()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -70,8 +70,10 @@ unsafe extern "system" fn enum_cb(hwnd: HWND, lparam: LPARAM) -> BOOL {
|
||||
if copied <= 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
// 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)]);
|
||||
// copied is a positive count from the call above; an out-of-range value
|
||||
// would mean an empty title, which the next check drops anyway.
|
||||
let written = usize::try_from(copied).ok().and_then(|n| buf.get(..n));
|
||||
let title = String::from_utf16_lossy(written.unwrap_or_default());
|
||||
if title.trim().is_empty() {
|
||||
return BOOL(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user