From badc214db774c4cfb38782bd11bf7c96d05d23cf Mon Sep 17 00:00:00 2001 From: Elliana May Date: Mon, 22 Apr 2024 10:08:32 +0800 Subject: [PATCH] fix: pass double ptr to result_from_duckdb_appender this ensures that the pointer is correctly cleared when errors occur --- src/appender/arrow.rs | 2 +- src/appender/mod.rs | 4 ++-- src/error.rs | 8 ++++---- src/inner_connection.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/appender/arrow.rs b/src/appender/arrow.rs index 8aa96a2a..19d79bb4 100644 --- a/src/appender/arrow.rs +++ b/src/appender/arrow.rs @@ -39,7 +39,7 @@ impl Appender<'_> { record_batch_to_duckdb_data_chunk(&record_batch, &mut data_chunk).map_err(|_op| Error::AppendError)?; let rc = unsafe { duckdb_append_data_chunk(self.app, data_chunk.get_ptr()) }; - result_from_duckdb_appender(rc, self.app) + result_from_duckdb_appender(rc, &mut self.app) } } diff --git a/src/appender/mod.rs b/src/appender/mod.rs index 1edb0894..65b6db0f 100644 --- a/src/appender/mod.rs +++ b/src/appender/mod.rs @@ -67,7 +67,7 @@ impl Appender<'_> { params.__bind_in(self)?; // NOTE: we only check end_row return value let rc = unsafe { ffi::duckdb_appender_end_row(self.app) }; - result_from_duckdb_appender(rc, self.app) + result_from_duckdb_appender(rc, &mut self.app) } #[inline] @@ -145,7 +145,7 @@ impl Appender<'_> { pub fn flush(&mut self) -> Result<()> { unsafe { let res = ffi::duckdb_appender_flush(self.app); - result_from_duckdb_appender(res, self.app) + result_from_duckdb_appender(res, &mut self.app) } } } diff --git a/src/error.rs b/src/error.rs index 58c73835..8d07a133 100644 --- a/src/error.rs +++ b/src/error.rs @@ -225,17 +225,17 @@ fn error_from_duckdb_code(code: ffi::duckdb_state, message: Option) -> R #[cold] #[inline] -pub fn result_from_duckdb_appender(code: ffi::duckdb_state, mut appender: ffi::duckdb_appender) -> Result<()> { +pub fn result_from_duckdb_appender(code: ffi::duckdb_state, appender: *mut ffi::duckdb_appender) -> Result<()> { if code == ffi::DuckDBSuccess { return Ok(()); } unsafe { - let message = if appender.is_null() { + let message = if (*appender).is_null() { Some("appender is null".to_string()) } else { - let c_err = ffi::duckdb_appender_error(appender); + let c_err = ffi::duckdb_appender_error(*appender); let message = Some(CStr::from_ptr(c_err).to_string_lossy().to_string()); - ffi::duckdb_appender_destroy(&mut appender); + ffi::duckdb_appender_destroy(appender); message }; error_from_duckdb_code(code, message) diff --git a/src/inner_connection.rs b/src/inner_connection.rs index 993c2492..f9d50c1c 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -102,7 +102,7 @@ impl InnerConnection { &mut c_app, ) }; - result_from_duckdb_appender(r, c_app)?; + result_from_duckdb_appender(r, &mut c_app)?; Ok(Appender::new(conn, c_app)) }