Skip to content

Commit d95e102

Browse files
Add SAFETY comment
1 parent d809ce7 commit d95e102

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎crates/capi/src/bytearrayobject.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use rustpython_vm::byte::bytes_from_object;
77

88
define_py_check!(fn PyByteArray_Check, types.bytearray_type);
99

10+
/// # Safety
11+
///
12+
/// If `bytes` is `NULL`, the returned bytearray may contain uninitialized
13+
/// bytes. The caller is responsible for initializing all bytes before any read.
1014
#[unsafe(no_mangle)]
1115
#[allow(clippy::uninit_vec)]
1216
pub unsafe extern "C" fn PyByteArray_FromStringAndSize(
@@ -20,6 +24,9 @@ pub unsafe extern "C" fn PyByteArray_FromStringAndSize(
2024

2125
let data = if bytes.is_null() {
2226
let mut data = Vec::with_capacity(len);
27+
// SAFETY: `bytes == NULL` follows CPython semantics here; caller must
28+
// initialize all bytes before any read. We keep this behavior for C-API
29+
// compatibility and to avoid unnecessary zero-initialization overhead.
2330
unsafe { data.set_len(len) };
2431
data
2532
} else {

0 commit comments

Comments
 (0)