allow passing a null through a cstr arg/return

This commit is contained in:
Skgland
2025-08-08 22:45:24 +02:00
committed by Bennet Bleßmann
parent 7b960a7d63
commit e1a52a4dde
3 changed files with 36 additions and 12 deletions

View File

@@ -257,20 +257,29 @@ fn ffi_cstr() {
use std::ffi::CStr;
#[unsafe(no_mangle)]
extern "C" fn ffi_cstr_len(c_str: *const core::ffi::c_char) -> u64 {
unsafe { CStr::from_ptr(c_str) }.count_bytes() as u64
extern "C" fn ffi_cstr_len(c_str: Option<std::ptr::NonNull<core::ffi::c_char>>) -> u64 {
if let Some(c_str) = c_str {
unsafe { CStr::from_ptr(c_str.as_ptr()) }.count_bytes() as u64
} else {
u64::MAX
}
}
#[unsafe(no_mangle)]
extern "C" fn ffi_example_cstr() -> *const core::ffi::c_char {
c"Rust Lang".as_ptr()
}
#[unsafe(no_mangle)]
extern "C" fn ffi_null_cstr() -> *const core::ffi::c_char {
std::ptr::null()
}
"##,
);
load_module_test_with_input(
"tests-pl/ffi_cstr.pl",
format!("LIB={dynlib_path:?}."),
r#"13-[R,u,s,t, ,L,a,n,g]"#,
format!(r#"13-[R,u,s,t, ,L,a,n,g]-0-{}"#, u64::MAX).as_str(),
);
}