Fix - Uses `std::mem::transmute` and `std::ptr::write` in unsafe code in append_vec.rs (#32711)

Uses std::mem::transmute and std::ptr::write in unsafe code in append_vec.rs
This commit is contained in:
Alexander Meißner 2023-08-04 17:18:13 +02:00 committed by GitHub
parent 1844c423a7
commit a310dd776c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -702,11 +702,13 @@ pub mod tests {
} }
impl AppendVecStoredAccountMeta<'_> { impl AppendVecStoredAccountMeta<'_> {
#[allow(clippy::cast_ref_to_mut)]
fn set_data_len_unsafe(&self, new_data_len: u64) { fn set_data_len_unsafe(&self, new_data_len: u64) {
// UNSAFE: cast away & (= const ref) to &mut to force to mutate append-only (=read-only) AppendVec // UNSAFE: cast away & (= const ref) to &mut to force to mutate append-only (=read-only) AppendVec
unsafe { unsafe {
*(&self.meta.data_len as *const u64 as *mut u64) = new_data_len; std::ptr::write(
std::mem::transmute::<*const u64, *mut u64>(&self.meta.data_len),
new_data_len,
);
} }
} }
@ -717,11 +719,13 @@ pub mod tests {
executable_byte executable_byte
} }
#[allow(clippy::cast_ref_to_mut)]
fn set_executable_as_byte(&self, new_executable_byte: u8) { fn set_executable_as_byte(&self, new_executable_byte: u8) {
// UNSAFE: Force to interpret mmap-backed &bool as &u8 to write some crafted value; // UNSAFE: Force to interpret mmap-backed &bool as &u8 to write some crafted value;
unsafe { unsafe {
*(&self.account_meta.executable as *const bool as *mut u8) = new_executable_byte; std::ptr::write(
std::mem::transmute::<*const bool, *mut u8>(&self.account_meta.executable),
new_executable_byte,
);
} }
} }
} }