fix potential UB in chunk engine (#38)

This commit is contained in:
SF-Zhou
2025-03-02 15:47:25 +08:00
committed by GitHub
parent d79b7dbea4
commit cd564a239a
2 changed files with 12 additions and 9 deletions

View File

@@ -7,25 +7,27 @@ use std::sync::Arc;
use crate::*; use crate::*;
pub use ::cxx::CxxString; pub use ::cxx::CxxString;
fn create(path: &str, create: bool, prefix_len: usize, error: Pin<&mut CxxString>) -> Box<Engine> { fn create(path: &str, create: bool, prefix_len: usize, error: Pin<&mut CxxString>) -> *mut Engine {
let config = EngineConfig { let config = EngineConfig {
path: PathBuf::from(path), path: PathBuf::from(path),
create, create,
prefix_len, prefix_len,
}; };
match Engine::open(&config) { match Engine::open(&config) {
Ok(engine) => Box::new(engine), Ok(engine) => Box::into_raw(Box::new(engine)),
Err(e) => { Err(e) => {
error.push_str(&e.to_string()); error.push_str(&e.to_string());
unsafe { Box::from_raw(std::ptr::null_mut()) } std::ptr::null_mut()
} }
} }
} }
fn release(_engine: Box<Engine>) {}
#[allow(dead_code)] #[allow(dead_code)]
struct LogGuard(tracing_appender::non_blocking::WorkerGuard); struct LogGuard(tracing_appender::non_blocking::WorkerGuard);
fn init_log(path: &str, error: Pin<&mut CxxString>) -> Box<LogGuard> { fn init_log(path: &str, error: Pin<&mut CxxString>) -> *mut LogGuard {
match rolling_file::BasicRollingFileAppender::new( match rolling_file::BasicRollingFileAppender::new(
path, path,
rolling_file::RollingConditionBasic::new().max_size(Size::mebibyte(500).into()), rolling_file::RollingConditionBasic::new().max_size(Size::mebibyte(500).into()),
@@ -38,11 +40,11 @@ fn init_log(path: &str, error: Pin<&mut CxxString>) -> Box<LogGuard> {
.with_writer(non_blocking) .with_writer(non_blocking)
.with_ansi(false) .with_ansi(false)
.init(); .init();
Box::new(LogGuard(guard)) Box::into_raw(Box::new(LogGuard(guard)))
} }
Err(e) => { Err(e) => {
error.push_str(&e.to_string()); error.push_str(&e.to_string());
unsafe { Box::from_raw(std::ptr::null_mut()) } std::ptr::null_mut()
} }
} }
} }
@@ -456,7 +458,8 @@ pub mod ffi {
create: bool, create: bool,
prefix_len: usize, prefix_len: usize,
error: Pin<&mut CxxString>, error: Pin<&mut CxxString>,
) -> Box<Engine>; ) -> *mut Engine;
fn release(engine: Box<Engine>);
fn raw_used_size(&self) -> RawUsedSize; fn raw_used_size(&self) -> RawUsedSize;
fn allocate_groups(&self, min_remain: usize, max_remain: usize, batch_size: usize) fn allocate_groups(&self, min_remain: usize, max_remain: usize, batch_size: usize)
@@ -546,7 +549,7 @@ pub mod ffi {
extern "Rust" { extern "Rust" {
type LogGuard; type LogGuard;
fn init_log(path: &str, error: Pin<&mut CxxString>) -> Box<LogGuard>; fn init_log(path: &str, error: Pin<&mut CxxString>) -> *mut LogGuard;
} }
extern "Rust" { extern "Rust" {

View File

@@ -60,7 +60,7 @@ Result<Void> StorageTargets::init(CPUExecutorGroup &executor) {
if (!error.empty()) { if (!error.empty()) {
co_return makeError(StorageCode::kStorageStatFailed, std::move(error)); co_return makeError(StorageCode::kStorageStatFailed, std::move(error));
} }
co_return engine; co_return rust::Box<chunk_engine::Engine>::from_raw(engine);
}).scheduleOn(&executor.pickNext())); }).scheduleOn(&executor.pickNext()));
} }