From 0c9db758e379b743fb53e7d2a02c167a8457607f Mon Sep 17 00:00:00 2001 From: Artavazd Balaian Date: Fri, 8 Aug 2025 19:23:58 +0800 Subject: [PATCH] fix: Use `CString` to safely handle query strings in FFI call in `execute_without_results` otherwise getting memory leak --- src/connection/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/connection/mod.rs b/src/connection/mod.rs index 7ffea70..290cec2 100644 --- a/src/connection/mod.rs +++ b/src/connection/mod.rs @@ -393,10 +393,13 @@ impl Connection { /// Fully Executes provided query but doesn't return any results even if they exist. pub fn execute_without_results(&mut self, query: &str) -> Result<(), MgError> { + // Allocate the C string without leaking. Keep it alive for the duration of the FFI call. + let c_query = CString::new(query).map_err(|_| MgError::new("Invalid query".to_string()))?; + match unsafe { bindings::mg_session_run( self.mg_session, - str_to_c_str(query), + c_query.as_ptr(), // no leak: pointer valid during this call std::ptr::null(), std::ptr::null_mut(), std::ptr::null_mut(),