From bde72aa9835c12577db9a2acface76ad1aaf58a1 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 11 Nov 2024 13:29:28 +0700 Subject: [PATCH] Fix crash when the mangled name is null (#38) Fixes #37. --- src/function.rs | 23 +++++++++++++++-------- src/symbol.rs | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/function.rs b/src/function.rs index a56d2b04..5fca0f6c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -10,6 +10,7 @@ use crate::{ }; use std::ffi::{CStr, CString}; use std::fmt; +use std::os::raw::c_char; use std::ptr; /// A generic function, which can be inlined or not. @@ -59,13 +60,8 @@ impl SBFunction { } /// The mangled (linkage) name for this function. - pub fn mangled_name(&self) -> &str { - unsafe { - match CStr::from_ptr(sys::SBFunctionGetMangledName(self.raw)).to_str() { - Ok(s) => s, - _ => panic!("Invalid string?"), - } - } + pub fn mangled_name(&self) -> Option<&str> { + unsafe { self.check_null_ptr(sys::SBFunctionGetMangledName(self.raw)) } } #[allow(missing_docs)] @@ -128,6 +124,17 @@ impl SBFunction { pub fn is_optimized(&self) -> bool { unsafe { sys::SBFunctionGetIsOptimized(self.raw) } } + + unsafe fn check_null_ptr(&self, ptr: *const c_char) -> Option<&str> { + if !ptr.is_null() { + match CStr::from_ptr(ptr).to_str() { + Ok(s) => Some(s), + _ => panic!("Invalid string?"), + } + } else { + None + } + } } impl Clone for SBFunction { @@ -166,7 +173,7 @@ impl SBFunction { self.display_name() } - fn mangled_name() -> &str { + fn mangled_name() -> Option<&str> { self.mangled_name() } diff --git a/src/symbol.rs b/src/symbol.rs index bc4b8486..0b81ea46 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -7,6 +7,7 @@ use crate::{sys, DisassemblyFlavor, SBAddress, SBInstructionList, SBStream, SBTarget, SymbolType}; use std::ffi::{CStr, CString}; use std::fmt; +use std::os::raw::c_char; use std::ptr; /// The symbol possibly associated with a stack frame. @@ -56,13 +57,8 @@ impl SBSymbol { } /// The mangled (linkage) name for this function. - pub fn mangled_name(&self) -> &str { - unsafe { - match CStr::from_ptr(sys::SBSymbolGetMangledName(self.raw)).to_str() { - Ok(s) => s, - _ => panic!("Invalid string?"), - } - } + pub fn mangled_name(&self) -> Option<&str> { + unsafe { self.check_null_ptr(sys::SBSymbolGetMangledName(self.raw)) } } #[allow(missing_docs)] @@ -122,6 +118,17 @@ impl SBSymbol { pub fn is_synthetic(&self) -> bool { unsafe { sys::SBSymbolIsSynthetic(self.raw) } } + + unsafe fn check_null_ptr(&self, ptr: *const c_char) -> Option<&str> { + if !ptr.is_null() { + match CStr::from_ptr(ptr).to_str() { + Ok(s) => Some(s), + _ => panic!("Invalid string?"), + } + } else { + None + } + } } impl Clone for SBSymbol { @@ -160,7 +167,7 @@ impl SBSymbol { self.display_name() } - fn mangled_name() -> &str { + fn mangled_name() -> Option<&str> { self.mangled_name() }