summaryrefslogtreecommitdiff
path: root/rust/src/disassembly.rs
diff options
context:
space:
mode:
authorRyan Snyder <ryan@vector35.com>2024-05-01 09:07:29 -0400
committerRyan Snyder <ryan@vector35.com>2024-05-01 09:07:29 -0400
commit8817116bee33f3d8bee71fbd82b1220deb10993d (patch)
tree71713d6a6b02b7bfbfc0bc8c2c25f653fa357534 /rust/src/disassembly.rs
parent44b051965083e087a098a89c88b6316ef3f95032 (diff)
parentee6abdde45c6e25f03727e1fd5a08c69cc074d5b (diff)
Merge branch 'fix-vec-leak' of github.com:rbran/binaryninja-api into dev
Diffstat (limited to 'rust/src/disassembly.rs')
-rw-r--r--rust/src/disassembly.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/rust/src/disassembly.rs b/rust/src/disassembly.rs
index 8b3a4cb5..855807b1 100644
--- a/rust/src/disassembly.rs
+++ b/rust/src/disassembly.rs
@@ -73,7 +73,7 @@ pub type InstructionTextTokenContext = BNInstructionTextTokenContext;
// IndirectImportToken = 69,
// ExternalSymbolToken = 70,
-#[repr(C)]
+#[repr(transparent)]
pub struct InstructionTextToken(pub(crate) BNInstructionTextToken);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -99,8 +99,8 @@ pub enum InstructionTextTokenContents {
}
impl InstructionTextToken {
- pub(crate) unsafe fn from_raw(raw: &BNInstructionTextToken) -> Self {
- Self(*raw)
+ pub(crate) unsafe fn from_raw(raw: &BNInstructionTextToken) -> &Self {
+ mem::transmute(raw)
}
pub fn new(text: &str, contents: InstructionTextTokenContents) -> Self {
@@ -254,13 +254,16 @@ impl Clone for InstructionTextToken {
}
}
-// TODO : There is almost certainly a memory leak here - in the case where
-// `impl CoreOwnedArrayProvider for InstructionTextToken` doesn't get triggered
-// impl Drop for InstructionTextToken {
-// fn drop(&mut self) {
-// let _owned = unsafe { BnString::from_raw(self.0.text) };
-// }
-// }
+impl Drop for InstructionTextToken {
+ fn drop(&mut self) {
+ if !self.0.text.is_null() {
+ let _owned = unsafe { BnString::from_raw(self.0.text) };
+ }
+ if !self.0.typeNames.is_null() && self.0.namesCount != 0 {
+ unsafe { BNFreeStringList(self.0.typeNames, self.0.namesCount) }
+ }
+ }
+}
pub struct DisassemblyTextLine(pub(crate) BNDisassemblyTextLine);
@@ -290,7 +293,7 @@ impl DisassemblyTextLine {
unsafe {
std::slice::from_raw_parts::<BNInstructionTextToken>(self.0.tokens, self.0.count)
.iter()
- .map(|&x| InstructionTextToken::from_raw(&x))
+ .map(|x| InstructionTextToken::from_raw(x).clone())
.collect()
}
}