Skip to content

Commit 7ff5ab7

Browse files
committed
Avoid calling slice::from_raw_parts with a null pointer
1 parent 125af9b commit 7ff5ab7

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

newsfragments/2687.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix UB in `FunctionDescription::extract_arguments_fastcall` due to creating slices from a null pointer.

src/impl_/extract_argument.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl FunctionDescription {
221221
/// Equivalent of `extract_arguments_tuple_dict` which uses the Python C-API "fastcall" convention.
222222
///
223223
/// # Safety
224-
/// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers.
224+
/// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers, or NULL.
225225
/// - `kwnames` must be a pointer to a PyTuple, or NULL.
226226
/// - `nargs + kwnames.len()` is the total length of the `args` array.
227227
#[cfg(not(Py_LIMITED_API))]
@@ -240,7 +240,11 @@ impl FunctionDescription {
240240
// Safety: Option<&PyAny> has the same memory layout as `*mut ffi::PyObject`
241241
let args = args as *const Option<&PyAny>;
242242
let positional_args_provided = nargs as usize;
243-
let args_slice = std::slice::from_raw_parts(args, positional_args_provided);
243+
let args_slice = if args.is_null() {
244+
&[]
245+
} else {
246+
std::slice::from_raw_parts(args, positional_args_provided)
247+
};
244248

245249
let num_positional_parameters = self.positional_parameter_names.len();
246250

0 commit comments

Comments
 (0)