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

Avoid calling slice::from_raw_parts with a null pointer #2687

Merged
merged 1 commit into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2687.fixed.md
@@ -0,0 +1 @@
Fix UB in `FunctionDescription::extract_arguments_fastcall` due to creating slices from a null pointer.
8 changes: 6 additions & 2 deletions src/impl_/extract_argument.rs
Expand Up @@ -221,7 +221,7 @@ impl FunctionDescription {
/// Equivalent of `extract_arguments_tuple_dict` which uses the Python C-API "fastcall" convention.
///
/// # Safety
/// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers.
/// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers, or NULL.
/// - `kwnames` must be a pointer to a PyTuple, or NULL.
/// - `nargs + kwnames.len()` is the total length of the `args` array.
#[cfg(not(Py_LIMITED_API))]
Expand All @@ -240,7 +240,11 @@ impl FunctionDescription {
// Safety: Option<&PyAny> has the same memory layout as `*mut ffi::PyObject`
let args = args as *const Option<&PyAny>;
let positional_args_provided = nargs as usize;
let args_slice = std::slice::from_raw_parts(args, positional_args_provided);
let args_slice = if args.is_null() {
&[]
} else {
std::slice::from_raw_parts(args, positional_args_provided)
};

let num_positional_parameters = self.positional_parameter_names.len();

Expand Down