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}"))?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user