Skip to content

Commit 30efdf1

Browse files
authored
fix: Use CString to safely handle query strings in FFI call in execute_without_results otherwise getting memory leak (#67)
1 parent a6a4b46 commit 30efdf1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/connection/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,13 @@ impl Connection {
393393

394394
/// Fully Executes provided query but doesn't return any results even if they exist.
395395
pub fn execute_without_results(&mut self, query: &str) -> Result<(), MgError> {
396+
// Allocate the C string without leaking. Keep it alive for the duration of the FFI call.
397+
let c_query = CString::new(query).map_err(|_| MgError::new("Invalid query".to_string()))?;
398+
396399
match unsafe {
397400
bindings::mg_session_run(
398401
self.mg_session,
399-
str_to_c_str(query),
402+
c_query.as_ptr(), // no leak: pointer valid during this call
400403
std::ptr::null(),
401404
std::ptr::null_mut(),
402405
std::ptr::null_mut(),

0 commit comments

Comments
 (0)