Skip to content

Commit 63ae7f5

Browse files
Fix crash when the mangled name is null
Fixes #37.
1 parent 4efe9cd commit 63ae7f5

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/function.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
};
1111
use std::ffi::{CStr, CString};
1212
use std::fmt;
13+
use std::os::raw::c_char;
1314
use std::ptr;
1415

1516
/// A generic function, which can be inlined or not.
@@ -59,13 +60,8 @@ impl SBFunction {
5960
}
6061

6162
/// The mangled (linkage) name for this function.
62-
pub fn mangled_name(&self) -> &str {
63-
unsafe {
64-
match CStr::from_ptr(sys::SBFunctionGetMangledName(self.raw)).to_str() {
65-
Ok(s) => s,
66-
_ => panic!("Invalid string?"),
67-
}
68-
}
63+
pub fn mangled_name(&self) -> Option<&str> {
64+
unsafe { self.check_null_ptr(sys::SBFunctionGetMangledName(self.raw)) }
6965
}
7066

7167
#[allow(missing_docs)]
@@ -128,6 +124,17 @@ impl SBFunction {
128124
pub fn is_optimized(&self) -> bool {
129125
unsafe { sys::SBFunctionGetIsOptimized(self.raw) }
130126
}
127+
128+
unsafe fn check_null_ptr(&self, ptr: *const c_char) -> Option<&str> {
129+
if !ptr.is_null() {
130+
match CStr::from_ptr(ptr).to_str() {
131+
Ok(s) => Some(s),
132+
_ => panic!("Invalid string?"),
133+
}
134+
} else {
135+
None
136+
}
137+
}
131138
}
132139

133140
impl Clone for SBFunction {
@@ -166,7 +173,7 @@ impl SBFunction {
166173
self.display_name()
167174
}
168175

169-
fn mangled_name() -> &str {
176+
fn mangled_name() -> Option<&str> {
170177
self.mangled_name()
171178
}
172179

src/symbol.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use crate::{sys, DisassemblyFlavor, SBAddress, SBInstructionList, SBStream, SBTarget, SymbolType};
88
use std::ffi::{CStr, CString};
99
use std::fmt;
10+
use std::os::raw::c_char;
1011
use std::ptr;
1112

1213
/// The symbol possibly associated with a stack frame.
@@ -56,13 +57,8 @@ impl SBSymbol {
5657
}
5758

5859
/// The mangled (linkage) name for this function.
59-
pub fn mangled_name(&self) -> &str {
60-
unsafe {
61-
match CStr::from_ptr(sys::SBSymbolGetMangledName(self.raw)).to_str() {
62-
Ok(s) => s,
63-
_ => panic!("Invalid string?"),
64-
}
65-
}
60+
pub fn mangled_name(&self) -> Option<&str> {
61+
unsafe { self.check_null_ptr(sys::SBSymbolGetMangledName(self.raw)) }
6662
}
6763

6864
#[allow(missing_docs)]
@@ -122,6 +118,17 @@ impl SBSymbol {
122118
pub fn is_synthetic(&self) -> bool {
123119
unsafe { sys::SBSymbolIsSynthetic(self.raw) }
124120
}
121+
122+
unsafe fn check_null_ptr(&self, ptr: *const c_char) -> Option<&str> {
123+
if !ptr.is_null() {
124+
match CStr::from_ptr(ptr).to_str() {
125+
Ok(s) => Some(s),
126+
_ => panic!("Invalid string?"),
127+
}
128+
} else {
129+
None
130+
}
131+
}
125132
}
126133

127134
impl Clone for SBSymbol {
@@ -160,7 +167,7 @@ impl SBSymbol {
160167
self.display_name()
161168
}
162169

163-
fn mangled_name() -> &str {
170+
fn mangled_name() -> Option<&str> {
164171
self.mangled_name()
165172
}
166173

0 commit comments

Comments
 (0)