From 6264254065bbae9d89f51cf3330379b7ace09592 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sat, 3 May 2025 23:15:17 -0400 Subject: [Rust] Return `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. --- rust/src/function.rs | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) (limited to 'rust/src/function.rs') diff --git a/rust/src/function.rs b/rust/src/function.rs index c882ab08..53fec505 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -368,8 +368,8 @@ impl Function { } } - pub fn comment(&self) -> BnString { - unsafe { BnString::from_raw(BNGetFunctionComment(self.handle)) } + pub fn comment(&self) -> String { + unsafe { BnString::into_string(BNGetFunctionComment(self.handle)) } } pub fn set_comment(&self, comment: S) { @@ -390,8 +390,8 @@ impl Function { unsafe { BNSetUserFunctionCanReturn(self.handle, &mut bool_with_confidence) } } - pub fn comment_at(&self, addr: u64) -> BnString { - unsafe { BnString::from_raw(BNGetCommentForAddress(self.handle, addr)) } + pub fn comment_at(&self, addr: u64) -> String { + unsafe { BnString::into_string(BNGetCommentForAddress(self.handle, addr)) } } pub fn set_comment_at(&self, addr: u64, comment: S) { @@ -459,11 +459,11 @@ impl Function { unsafe { Array::new(lines, count, ()) } } - pub fn variable_name(&self, var: &Variable) -> BnString { + pub fn variable_name(&self, var: &Variable) -> String { unsafe { let raw_var = BNVariable::from(var); let raw_name = BNGetVariableName(self.handle, &raw_var); - BnString::from_raw(raw_name) + BnString::into_string(raw_name) } } @@ -1741,10 +1741,10 @@ impl Function { value: u64, operand: usize, arch: Option, - ) -> BnString { + ) -> String { let arch = arch.unwrap_or_else(|| self.arch()); unsafe { - BnString::from_raw(BNGetIntegerConstantDisplayTypeEnumerationType( + BnString::into_string(BNGetIntegerConstantDisplayTypeEnumerationType( self.handle, arch.handle, instr_addr, @@ -1766,7 +1766,7 @@ impl Function { value: u64, operand: usize, arch: Option, - ) -> (IntegerDisplayType, BnString) { + ) -> (IntegerDisplayType, String) { let arch = arch.unwrap_or_else(|| self.arch()); let name = self.int_enum_display_typeid(instr_addr, value, operand, Some(arch)); let display = self.int_display_type(instr_addr, value, operand, Some(arch)); @@ -1922,7 +1922,7 @@ impl Function { addr: u64, offset: i64, arch: Option, - ) -> Option<(Variable, BnString, Conf>)> { + ) -> Option { let arch = arch.unwrap_or_else(|| self.arch()); let mut found_value = BNVariableNameAndType::default(); let found = unsafe { @@ -1937,13 +1937,7 @@ impl Function { if !found { return None; } - let var = Variable::from(found_value.var); - let name = unsafe { BnString::from_raw(found_value.name) }; - let var_type = Conf::new( - unsafe { Type::ref_from_raw(found_value.type_) }, - found_value.typeConfidence, - ); - Some((var, name, var_type)) + Some(NamedVariableWithType::from_owned_raw(found_value)) } pub fn stack_var_at_frame_offset_after_instruction( @@ -1951,7 +1945,7 @@ impl Function { addr: u64, offset: i64, arch: Option, - ) -> Option<(Variable, BnString, Conf>)> { + ) -> Option { let arch = arch.unwrap_or_else(|| self.arch()); let mut found_value = BNVariableNameAndType::default(); let found = unsafe { @@ -1966,13 +1960,7 @@ impl Function { if !found { return None; } - let var = Variable::from(found_value.var); - let name = unsafe { BnString::from_raw(found_value.name) }; - let var_type = Conf::new( - unsafe { Type::ref_from_raw(found_value.type_) }, - found_value.typeConfidence, - ); - Some((var, name, var_type)) + Some(NamedVariableWithType::from_owned_raw(found_value)) } pub fn stack_variables_referenced_by( @@ -2348,8 +2336,8 @@ impl Function { /// Returns a string representing the provenance. This portion of the API /// is under development. Currently the provenance information is /// undocumented, not persistent, and not saved to a database. - pub fn provenance(&self) -> BnString { - unsafe { BnString::from_raw(BNGetProvenanceString(self.handle)) } + pub fn provenance(&self) -> String { + unsafe { BnString::into_string(BNGetProvenanceString(self.handle)) } } /// Get registers that are used for the return value @@ -2780,7 +2768,7 @@ impl Default for HighlightColor { #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct Comment { pub addr: u64, - pub comment: BnString, + pub comment: String, } impl CoreArrayProvider for Comment { -- cgit v1.3.1