Validate a C string pointer: checks for null and valid UTF-8. Returns `Ok(String)` on success, or `Err(*mut paimon_error)` if the pointer is null or the string contains invalid UTF-8. # Safety If `ptr` is non-null, it must point to a valid null-terminated C string.
(ptr: *const c_char, name: &str)
| 89 | /// # Safety |
| 90 | /// If `ptr` is non-null, it must point to a valid null-terminated C string. |
| 91 | pub unsafe fn validate_cstr(ptr: *const c_char, name: &str) -> Result<String, *mut paimon_error> { |
| 92 | if ptr.is_null() { |
| 93 | return Err(paimon_error::new( |
| 94 | PaimonErrorCode::InvalidInput, |
| 95 | format!("null pointer passed for `{name}`"), |
| 96 | )); |
| 97 | } |
| 98 | CStr::from_ptr(ptr) |
| 99 | .to_str() |
| 100 | .map(|s| s.to_owned()) |
| 101 | .map_err(|e| { |
| 102 | paimon_error::new( |
| 103 | PaimonErrorCode::InvalidInput, |
| 104 | format!("`{name}` is not valid UTF-8: {e}"), |
| 105 | ) |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | /// Check that a pointer is non-null, returning an error if it is. |
| 110 | pub fn check_non_null<T>(ptr: *const T, name: &str) -> Result<(), *mut paimon_error> { |
no test coverage detected