Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safely handle nullptr for SBValue (fixes #23) #30

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions src/value.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;

/// The value of a variable, register or expression.
pub struct SBValue {
Expand Down Expand Up @@ -53,33 +54,18 @@ impl SBValue {
}

#[allow(missing_docs)]
pub fn name(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBValueGetName(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
pub fn name(&self) -> Option<&str> {
unsafe { self.check_null_ptr(sys::SBValueGetName(self.raw)) }
}

#[allow(missing_docs)]
pub fn type_name(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBValueGetTypeName(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
pub fn type_name(&self) -> Option<&str> {
unsafe { self.check_null_ptr(sys::SBValueGetTypeName(self.raw)) }
}

#[allow(missing_docs)]
pub fn display_type_name(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBValueGetDisplayTypeName(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
pub fn display_type_name(&self) -> Option<&str> {
unsafe { self.check_null_ptr(sys::SBValueGetDisplayTypeName(self.raw)) }
}

#[allow(missing_docs)]
Expand All @@ -103,13 +89,8 @@ impl SBValue {
}

#[allow(missing_docs)]
pub fn value(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBValueGetValue(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
pub fn value(&self) -> Option<&str> {
unsafe { self.check_null_ptr(sys::SBValueGetValue(self.raw)) }
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -257,6 +238,17 @@ impl SBValue {
pub fn address(&self) -> Option<SBAddress> {
SBAddress::maybe_wrap(unsafe { sys::SBValueGetAddress(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 SBValue {
Expand Down Expand Up @@ -324,15 +316,15 @@ impl SBValue {
self.id() as i32
}

fn name() -> &str {
fn name() -> Option<&str> {
self.name()
}

fn type_name() -> &str {
fn type_name() -> Option<&str> {
self.type_name()
}

fn display_type_name() -> &str {
fn display_type_name() -> Option<&str> {
self.display_type_name()
}

Expand Down