Skip to content

Commit

Permalink
Fix crash when the mangled name is null
Browse files Browse the repository at this point in the history
Fixes #37.
  • Loading branch information
waywardmonkeys committed Nov 11, 2024
1 parent 4efe9cd commit 26dfc80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 14 additions & 7 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 26dfc80

Please sign in to comment.