From 0db7924add74c0e113bfe5e3904c802412926522 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 6 May 2024 14:24:32 -0700 Subject: [PATCH] ffi: Make `map_reserve!` zero-initialize reserved bytes Valgrind was complaining about this. I don't think this should make a difference for actual soundness, but this seems reasonable enough to do. This reduces the number of Valgrind errors on the `properties` example from `95265 errors from 172 contexts` to `78 errors from 2 contexts`. --- drm-ffi/src/utils.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drm-ffi/src/utils.rs b/drm-ffi/src/utils.rs index 40553651..8788880b 100644 --- a/drm-ffi/src/utils.rs +++ b/drm-ffi/src/utils.rs @@ -18,15 +18,30 @@ macro_rules! map_len { }; } -/// Takes an `Option<&mut Vec>` style buffer and shrinks it. +/// Takes an `Option<&mut Vec>` style buffer and reserves space. macro_rules! map_reserve { ($buffer:expr, $size:expr) => { match $buffer { - Some(ref mut b) => b.reserve_exact($size - b.len()), + Some(ref mut b) => crate::utils::map_reserve_inner(b, $size), _ => (), } }; } + +pub(crate) fn map_reserve_inner(b: &mut Vec, size: usize) { + let old_len = b.len(); + if size <= old_len { + return; + } + b.reserve_exact(size - old_len); + + // `memset` to 0, at least so Valgrind doesn't complain + unsafe { + let ptr = b.as_mut_ptr().add(old_len) as *mut u8; + ptr.write_bytes(0, (size - old_len) * std::mem::size_of::()); + } +} + /// Takes an `Option<&mut Vec>` style buffer and shrinks it. macro_rules! map_set { ($buffer:expr, $min:expr) => {