Skip to content

Commit

Permalink
fix potential UB in chunk engine (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
SF-Zhou authored Mar 2, 2025
1 parent d79b7db commit cd564a2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/storage/chunk_engine/src/cxx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ use std::sync::Arc;
use crate::*;
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 {
path: PathBuf::from(path),
create,
prefix_len,
};
match Engine::open(&config) {
Ok(engine) => Box::new(engine),
Ok(engine) => Box::into_raw(Box::new(engine)),
Err(e) => {
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)]
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(
path,
rolling_file::RollingConditionBasic::new().max_size(Size::mebibyte(500).into()),
Expand All @@ -38,11 +40,11 @@ fn init_log(path: &str, error: Pin<&mut CxxString>) -> Box<LogGuard> {
.with_writer(non_blocking)
.with_ansi(false)
.init();
Box::new(LogGuard(guard))
Box::into_raw(Box::new(LogGuard(guard)))
}
Err(e) => {
error.push_str(&e.to_string());
unsafe { Box::from_raw(std::ptr::null_mut()) }
std::ptr::null_mut()
}
}
}
Expand Down Expand Up @@ -456,7 +458,8 @@ pub mod ffi {
create: bool,
prefix_len: usize,
error: Pin<&mut CxxString>,
) -> Box<Engine>;
) -> *mut Engine;
fn release(engine: Box<Engine>);

fn raw_used_size(&self) -> RawUsedSize;
fn allocate_groups(&self, min_remain: usize, max_remain: usize, batch_size: usize)
Expand Down Expand Up @@ -546,7 +549,7 @@ pub mod ffi {

extern "Rust" {
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" {
Expand Down
2 changes: 1 addition & 1 deletion src/storage/store/StorageTargets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Result<Void> StorageTargets::init(CPUExecutorGroup &executor) {
if (!error.empty()) {
co_return makeError(StorageCode::kStorageStatFailed, std::move(error));
}
co_return engine;
co_return rust::Box<chunk_engine::Engine>::from_raw(engine);
}).scheduleOn(&executor.pickNext()));
}

Expand Down

0 comments on commit cd564a2

Please sign in to comment.