gstreamer: memory: Simplify and correct offset/size calculations in mem_share()

This is all supposed to do unsigned wrapping arithmetic to calculate the
new offsets and sizes, despite input parameters being signed integers.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1363>
This commit is contained in:
Sebastian Dröge 2023-12-17 13:15:55 +02:00
parent 5c88bd0b5f
commit 8b9862052b

View file

@ -61,24 +61,18 @@ unsafe extern "C" fn mem_share(
(*mem).mem.parent as *mut WrappedMemory<()>
};
// Actually an usize
// Offset and size are actually usizes and the API assumes that negative values simply wrap
// around, so let's cast to usizes here and do wrapping arithmetic.
let offset = offset as usize;
let mut size = size as usize;
let new_offset = (*mem).mem.offset.wrapping_add(offset);
debug_assert!(new_offset < (*mem).mem.maxsize);
if size == usize::MAX {
if offset < 0 {
size = (*mem).mem.size + (-offset as usize);
} else {
debug_assert!((*mem).mem.size >= offset as usize);
size = (*mem).mem.size - offset as usize;
}
size = (*mem).mem.size.wrapping_sub(offset);
}
let new_offset = if offset < 0 {
debug_assert!((*mem).mem.offset >= (-offset as usize));
(*mem).mem.offset - (-offset as usize)
} else {
(*mem).mem.offset + offset as usize
};
debug_assert!(new_offset <= usize::MAX - size);
debug_assert!(new_offset + size <= (*mem).mem.maxsize);
let layout = alloc::Layout::new::<WrappedMemory<()>>();