Skip to content

Commit 9f937ea

Browse files
committed
Return rust String instead of BnString for cases where lossy conversion can be tolerated
Still need to go and audit all usage, but realistically the most important places to give the user control are with symbols, where the data can come from non utf8 sources This is still incomplete, I just looked for usage of -> BnString so any other variant was omitted.
1 parent e767296 commit 9f937ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+349
-369
lines changed

plugins/dwarf/dwarf_export/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use binaryninja::{
1818
interaction,
1919
interaction::{FormResponses, FormResponses::Index},
2020
rc::Ref,
21-
string::BnString,
2221
symbol::SymbolType,
2322
types::{MemberAccess, StructureType, Type, TypeClass},
2423
};
@@ -422,7 +421,7 @@ fn export_functions(
422421
// Set subprogram DIE attributes
423422
dwarf.unit.get_mut(function_die_uid).set(
424423
gimli::DW_AT_name,
425-
AttributeValue::String(function.symbol().short_name().as_bytes().to_vec()),
424+
AttributeValue::String(function.symbol().short_name().to_bytes().to_vec()),
426425
);
427426

428427
// TODO : (DW_AT_main_subprogram VS DW_TAG_entry_point)
@@ -557,7 +556,7 @@ fn export_data_vars(
557556
if let Some(symbol) = data_var_sym {
558557
dwarf.unit.get_mut(var_die_uid).set(
559558
gimli::DW_AT_name,
560-
AttributeValue::String(symbol.full_name().as_bytes().to_vec()),
559+
AttributeValue::String(symbol.full_name().to_bytes().to_vec()),
561560
);
562561

563562
if symbol.external() {
@@ -756,7 +755,7 @@ fn export_dwarf(bv: &BinaryView) {
756755
let arch_name = if let Some(arch) = bv.default_arch() {
757756
arch.name()
758757
} else {
759-
BnString::new("Unknown")
758+
String::from("Unknown")
760759
};
761760
let responses = present_form(arch_name.as_str());
762761

plugins/dwarf/shared/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn create_section_reader<'a, Endian: 'a + Endianity>(
106106
if let Some(symbol) = view
107107
.symbols()
108108
.iter()
109-
.find(|symbol| symbol.full_name().as_str() == "__elf_section_headers")
109+
.find(|symbol| symbol.full_name().to_string_lossy() == "__elf_section_headers")
110110
{
111111
if let Some(data_var) = view
112112
.data_variables()

plugins/warp/src/bin/sigem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn main() {
131131
fn data_from_view(view: &BinaryView) -> Data {
132132
let mut data = Data::default();
133133
let is_function_named = |f: &BNGuard<BNFunction>| {
134-
!f.symbol().short_name().as_str().contains("sub_") || f.has_user_annotations()
134+
!f.symbol().short_name().to_string_lossy().contains("sub_") || f.has_user_annotations()
135135
};
136136

137137
data.functions = view

plugins/warp/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ pub struct TypeRefID(u64);
439439
impl From<&BNNamedTypeReference> for TypeRefID {
440440
fn from(value: &BNNamedTypeReference) -> Self {
441441
let mut hasher = DefaultHasher::new();
442-
hasher.write(value.id().as_bytes());
442+
hasher.write(value.id().to_bytes());
443443
Self(hasher.finish())
444444
}
445445
}

plugins/warp/src/matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub struct PlatformID(u64);
506506
impl From<&Platform> for PlatformID {
507507
fn from(value: &Platform) -> Self {
508508
let mut hasher = DefaultHasher::new();
509-
hasher.write(value.name().to_bytes());
509+
hasher.write(value.name().as_bytes());
510510
Self(hasher.finish())
511511
}
512512
}

plugins/warp/src/plugin/create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct CreateSignatureFile;
1818
impl Command for CreateSignatureFile {
1919
fn action(&self, view: &BinaryView) {
2020
let is_function_named = |f: &Guard<Function>| {
21-
!f.symbol().short_name().as_str().contains("sub_") || f.has_user_annotations()
21+
!f.symbol().short_name().to_string_lossy().contains("sub_") || f.has_user_annotations()
2222
};
2323
let mut signature_dir = user_signature_dir();
2424
if let Some(default_plat) = view.default_platform() {

rust/src/architecture.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,8 @@ impl CoreArchitecture {
14121412
}
14131413
}
14141414

1415-
pub fn name(&self) -> BnString {
1416-
unsafe { BnString::from_raw(BNGetArchitectureName(self.handle)) }
1415+
pub fn name(&self) -> String {
1416+
unsafe { BnString::to_string(BNGetArchitectureName(self.handle)) }
14171417
}
14181418
}
14191419

rust/src/background_task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ impl BackgroundTask {
7171
unsafe { BNFinishBackgroundTask(self.handle) }
7272
}
7373

74-
pub fn progress_text(&self) -> BnString {
75-
unsafe { BnString::from_raw(BNGetBackgroundTaskProgressText(self.handle)) }
74+
pub fn progress_text(&self) -> String {
75+
unsafe { BnString::to_string(BNGetBackgroundTaskProgressText(self.handle)) }
7676
}
7777

7878
pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {

rust/src/base_detection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ impl BaseAddressDetectionSettings {
174174
let arch_name = value
175175
.arch
176176
.map(|a| a.name().as_ptr())
177-
.unwrap_or(BASE_ADDRESS_AUTO_DETECTION_ARCH.as_ptr() as *const c_char);
177+
.unwrap_or(BASE_ADDRESS_AUTO_DETECTION_ARCH.as_ptr() as *const u8);
178178
BNBaseAddressDetectionSettings {
179-
Architecture: arch_name,
179+
Architecture: arch_name as *const c_char,
180180
Analysis: value.analysis.as_raw().as_ptr(),
181181
MinStrlen: value.min_string_len,
182182
Alignment: value.alignment.get(),

rust/src/binary_view.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::architecture::{Architecture, CoreArchitecture};
2727
use crate::base_detection::BaseAddressDetection;
2828
use crate::basic_block::BasicBlock;
2929
use crate::binary_view::memory_map::MemoryMap;
30-
use crate::component::{Component, IntoComponentGuid};
30+
use crate::component::Component;
3131
use crate::confidence::Conf;
3232
use crate::data_buffer::DataBuffer;
3333
use crate::debuginfo::DebugInfo;
@@ -187,9 +187,9 @@ pub trait BinaryViewExt: BinaryViewBase {
187187
}
188188
}
189189

190-
fn type_name(&self) -> BnString {
190+
fn type_name(&self) -> String {
191191
let ptr: *mut c_char = unsafe { BNGetViewType(self.as_ref().handle) };
192-
unsafe { BnString::from_raw(ptr) }
192+
unsafe { BnString::to_string(ptr) }
193193
}
194194

195195
fn parent_view(&self) -> Option<Ref<BinaryView>> {
@@ -204,9 +204,9 @@ pub trait BinaryViewExt: BinaryViewBase {
204204
self.file().view_of_type("Raw")
205205
}
206206

207-
fn view_type(&self) -> BnString {
207+
fn view_type(&self) -> String {
208208
let ptr: *mut c_char = unsafe { BNGetViewType(self.as_ref().handle) };
209-
unsafe { BnString::from_raw(ptr) }
209+
unsafe { BnString::to_string(ptr) }
210210
}
211211

212212
/// Reads up to `len` bytes from address `offset`
@@ -1493,9 +1493,14 @@ pub trait BinaryViewExt: BinaryViewBase {
14931493
unsafe { BNRemoveComponent(self.as_ref().handle, component.handle.as_ptr()) }
14941494
}
14951495

1496-
fn remove_component_by_guid<P: IntoComponentGuid>(&self, guid: P) -> bool {
1497-
let path = guid.component_guid();
1498-
unsafe { BNRemoveComponentByGuid(self.as_ref().handle, path.as_ptr()) }
1496+
fn remove_component_by_guid<P: BnStrCompatible>(&self, guid: P) -> bool {
1497+
let path = guid.into_bytes_with_nul();
1498+
unsafe {
1499+
BNRemoveComponentByGuid(
1500+
self.as_ref().handle,
1501+
path.as_ref().as_ptr() as *const c_char,
1502+
)
1503+
}
14991504
}
15001505

15011506
fn data_variable_parent_components(&self, data_variable: &DataVariable) -> Array<Component> {

0 commit comments

Comments
 (0)