Skip to content

Commit dc4fe7b

Browse files
committed
[Rust] Fix leaking list in Function::guided_source_blocks
Forgot to call `BNFreeArchitectureAndAddressList`, also use `Location` instead of `ArchAndAddr`.
1 parent f788c27 commit dc4fe7b

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

rust/src/function.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ impl From<BNArchitectureAndAddress> for Location {
9494
}
9595
}
9696

97+
impl From<&BNArchitectureAndAddress> for Location {
98+
fn from(value: &BNArchitectureAndAddress) -> Self {
99+
Self::from_raw(value.address, value.arch)
100+
}
101+
}
102+
97103
impl From<Location> for BNArchitectureAndAddress {
98104
fn from(value: Location) -> Self {
99105
Self {
@@ -103,6 +109,29 @@ impl From<Location> for BNArchitectureAndAddress {
103109
}
104110
}
105111

112+
impl From<&Location> for BNArchitectureAndAddress {
113+
fn from(value: &Location) -> Self {
114+
Self::from(*value)
115+
}
116+
}
117+
118+
impl CoreArrayProvider for Location {
119+
type Raw = BNArchitectureAndAddress;
120+
type Context = ();
121+
type Wrapped<'a> = Self;
122+
}
123+
124+
unsafe impl CoreArrayProviderInner for Location {
125+
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
126+
// NOTE: Does not use _count because freeing does not require iterating the list.
127+
BNFreeArchitectureAndAddressList(raw)
128+
}
129+
130+
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
131+
Location::from(*raw)
132+
}
133+
}
134+
106135
pub struct NativeBlockIter {
107136
arch: CoreArchitecture,
108137
bv: Ref<BinaryView>,
@@ -2594,19 +2623,14 @@ impl Function {
25942623
unsafe { BNFunctionRemoveMetadata(self.handle, key.as_ptr()) };
25952624
}
25962625

2597-
pub fn guided_source_blocks(&self) -> HashSet<ArchAndAddr> {
2626+
/// The current list of guided source block start [`Location`]s for this function.
2627+
///
2628+
/// These blocks have their direct outgoing branch targets analyzed.
2629+
pub fn guided_source_blocks(&self) -> HashSet<Location> {
25982630
let mut count = 0;
25992631
let raw = unsafe { BNGetGuidedSourceBlocks(self.handle, &mut count) };
2600-
if raw.is_null() || count == 0 {
2601-
return HashSet::new();
2602-
}
2603-
2604-
(0..count)
2605-
.map(|i| {
2606-
let raw = unsafe { std::ptr::read(raw.add(i)) };
2607-
ArchAndAddr::from(raw)
2608-
})
2609-
.collect::<HashSet<_>>()
2632+
let array: Array<Location> = unsafe { Array::new(raw, count, ()) };
2633+
array.into_iter().collect()
26102634
}
26112635
}
26122636

0 commit comments

Comments
 (0)