From 3517e6d752634fda2c047c721ccce3625e54b636 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 20 Jan 2023 03:45:16 +0800 Subject: [PATCH] Handle nullptr char* in Rust --- native/src/base/misc.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/native/src/base/misc.rs b/native/src/base/misc.rs index e809339ff..521acb651 100644 --- a/native/src/base/misc.rs +++ b/native/src/base/misc.rs @@ -108,7 +108,11 @@ macro_rules! cstr { } pub fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str { - unsafe { CStr::from_ptr(ptr.cast()) }.to_str().unwrap_or("") + if ptr.is_null() { + "(null)" + } else { + unsafe { CStr::from_ptr(ptr.cast()) }.to_str().unwrap_or("") + } } pub fn errno() -> &'static mut i32 {