Skip to content

Commit 7fb4ab9

Browse files
committed
properly clean up cached regex from set_auxdata
1 parent cca60f6 commit 7fb4ab9

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/utils.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use regex::{Captures, Regex, RegexSet};
2-
32
use sqlite_loadable::prelude::*;
43
use sqlite_loadable::{api, Error, Result};
4+
use std::os::raw::c_void;
55

66
// Raw bytes as performance. the string MUST end in the null byte '\0'
77
const REGEX_POINTER_NAME: &[u8] = b"regex0\0";
@@ -101,8 +101,9 @@ pub fn regex_from_value_or_cache(
101101
}
102102
}
103103

104-
use std::os::raw::c_void;
105-
unsafe extern "C" fn cleanup(_arg1: *mut c_void) {}
104+
unsafe extern "C" fn cleanup_regex(arg1: *mut c_void) {
105+
drop(Box::from_raw(arg1.cast::<Regex>()))
106+
}
106107

107108
pub fn cleanup_regex_value_cached(
108109
context: *mut sqlite3_context,
@@ -112,15 +113,12 @@ pub fn cleanup_regex_value_cached(
112113
match input_type {
113114
RegexInputType::Pointer => (),
114115
RegexInputType::GetAuxdata => {}
115-
RegexInputType::TextInitial(at) => {
116-
api::auxdata_set(
117-
context,
118-
at as i32,
119-
regex.cast::<c_void>(),
120-
// TODO memory leak, box not destroyed?
121-
Some(cleanup),
122-
)
123-
}
116+
RegexInputType::TextInitial(at) => api::auxdata_set(
117+
context,
118+
at as i32,
119+
regex.cast::<c_void>(),
120+
Some(cleanup_regex),
121+
),
124122
}
125123
}
126124

0 commit comments

Comments
 (0)