Safer CryptoBufferBuilder::build

This commit is contained in:
Kornel 2025-10-01 11:12:40 +01:00 committed by Kornel
parent 5957ce94cc
commit 75ef523230
1 changed files with 3 additions and 7 deletions

View File

@ -16,7 +16,7 @@ use foreign_types::ForeignType;
use foreign_types::ForeignTypeRef; use foreign_types::ForeignTypeRef;
use libc::{c_char, c_int, c_uchar, c_uint, c_void}; use libc::{c_char, c_int, c_uchar, c_uint, c_void};
use std::ffi::CStr; use std::ffi::CStr;
use std::mem::MaybeUninit; use std::mem::{self, MaybeUninit};
use std::ptr; use std::ptr;
use std::slice; use std::slice;
use std::str; use std::str;
@ -769,12 +769,8 @@ impl<'a> CryptoBufferBuilder<'a> {
// Make sure all bytes in buffer initialized as required by Boring SSL. // Make sure all bytes in buffer initialized as required by Boring SSL.
return Err(ErrorStack::internal_error_str("invalid len")); return Err(ErrorStack::internal_error_str("invalid len"));
} }
unsafe { // Drop is no-op if the buffer is null
let mut result = ptr::null_mut(); Ok(mem::replace(&mut self.buffer, ptr::null_mut()))
ptr::swap(&mut self.buffer, &mut result);
std::mem::forget(self);
Ok(result)
}
} }
} }