Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure slice::from_raw_parts is not called with null pointer or 0 len in merge_operator #804

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/merge_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub unsafe extern "C" fn full_merge_callback(
) -> *const c_char {
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
let oldval: &[u8] = slice::from_raw_parts(existing_value as *const u8, existing_value_len);
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
let oldval: &[u8] = from_raw_parts(existing_value as *const u8, existing_value_len);
let mut result = (cb.merge_fn)(key, Some(oldval), operands);
result.shrink_to_fit();
// TODO(tan) investigate zero-copy techniques to improve performance
Expand All @@ -77,7 +77,7 @@ pub unsafe extern "C" fn partial_merge_callback(
) -> *const c_char {
let cb: &mut MergeOperatorCallback = &mut *(raw_cb as *mut MergeOperatorCallback);
let operands = &mut MergeOperands::new(operands_list, operands_list_len, num_operands);
let key: &[u8] = slice::from_raw_parts(raw_key as *const u8, key_len);
let key: &[u8] = from_raw_parts(raw_key as *const u8, key_len);
let mut result = (cb.merge_fn)(key, None, operands);
result.shrink_to_fit();
// TODO(tan) investigate zero-copy techniques to improve performance
Expand All @@ -90,6 +90,14 @@ pub unsafe extern "C" fn partial_merge_callback(
buf as *const c_char
}

unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
if data.is_null() || len == 0 {
&[]
} else {
slice::from_raw_parts(data, len)
}
}

pub struct MergeOperands {
operands_list: *const *const c_char,
operands_list_len: *const size_t,
Expand Down