Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Mar 15, 2024
1 parent 39f35d9 commit 284c6bb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions rust-bindings/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {
.allowlist_function("filter_match")
.allowlist_function("filter_match_any")
.allowlist_function("free_slice")
.allowlist_function("free_filter")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
Expand Down
25 changes: 11 additions & 14 deletions rust-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Drop for EncodedFilter {
}

/// A BIP 158 filter.
pub struct Bip158Filter(bindings::GCSFilter);
pub struct Bip158Filter(*const bindings::GCSFilter);

impl Bip158Filter {
/// Encode a BIP 158 filter from a list of slices.
Expand All @@ -62,11 +62,7 @@ impl Bip158Filter {
/// Encodes the filter.
pub fn encode(&self) -> EncodedFilter {
// SAFETY: The `GCSFilter` struct is guaranteed to be valid for the lifetime of the `Bip158Filter` struct.
unsafe {
EncodedFilter(bindings::encode_filter(
&self.0 as *const bindings::GCSFilter,
))
}
unsafe { EncodedFilter(bindings::encode_filter(self.0)) }
}

/// Matches a single slice against the filter.
Expand All @@ -75,7 +71,7 @@ impl Bip158Filter {
// struct is guaranteed to be valid for the lifetime of the `Bip158Filter` struct, so this should be safe.
unsafe {
bindings::filter_match(
&self.0 as *const bindings::GCSFilter,
self.0,
bindings::Slice {
bytes: slice.as_ptr(),
length: slice.len(),
Expand All @@ -97,13 +93,14 @@ impl Bip158Filter {

// SAFETY: The length provided matches the number of slices, and the `GCSFilter`
// struct is guaranteed to be valid for the lifetime of the `Bip158Filter` struct, so this should be safe.
unsafe {
bindings::filter_match_any(
&self.0 as *const bindings::GCSFilter,
slices.as_ptr(),
slices.len(),
)
}
unsafe { bindings::filter_match_any(self.0, slices.as_ptr(), slices.len()) }
}
}

impl Drop for Bip158Filter {
fn drop(&mut self) {
// SAFETY: The `Slice` struct is guaranteed to be valid for the lifetime of the `EncodedFilter` struct.
unsafe { bindings::free_filter(self.0) }
}
}

Expand Down
8 changes: 6 additions & 2 deletions rust-bindings/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ GCSFilter::ElementSet create_element_set(Slice const* hashes, size_t length) {
return elements;
}

GCSFilter create_filter(Slice const* hashes, size_t length) {
GCSFilter const* create_filter(Slice const* hashes, size_t length) {
auto elements = create_element_set(hashes, length);
return GCSFilter({0, 0, 20, 1 << 20}, elements);
return new GCSFilter({0, 0, 20, 1 << 20}, elements);
}

Slice encode_filter(GCSFilter const* filter) {
Expand All @@ -39,4 +39,8 @@ bool filter_match_any(GCSFilter const* filter, Slice const* hashes, size_t lengt

void free_slice(Slice slice) {
delete[] slice.bytes;
}

void free_filter(GCSFilter const* filter) {
delete filter;
}
5 changes: 3 additions & 2 deletions rust-bindings/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ struct Slice {
const size_t length;
};

GCSFilter create_filter(Slice const* hashes, size_t length);
GCSFilter const* create_filter(Slice const* hashes, size_t length);
Slice encode_filter(GCSFilter const* filter);
bool filter_match(GCSFilter const* filter, Slice hash);
bool filter_match_any(GCSFilter const* filter, Slice const* hashes, size_t length);
void free_slice(Slice slice);
void free_slice(Slice slice);
void free_filter(GCSFilter const* filter);

0 comments on commit 284c6bb

Please sign in to comment.