when converting a Vec<T> into a Postgres array Datum, make sure to initialize the array so we can return zero-length arrays instead of crashing Postgres
@@ -347,7 +347,13 @@ where
T: IntoDatum,
{
fn into_datum(self) -> Option<pg_sys::Datum> {
- let mut state = std::ptr::null_mut();
+ let mut state = unsafe {
+ pg_sys::initArrayResult(
+ T::type_oid(),
+ PgMemoryContexts::CurrentMemoryContext.value(),
+ false,
+ )
+ };
for s in self {
let datum = s.into_datum();
let isnull = datum.is_none();
@@ -363,10 +369,15 @@ where
}
}
+ if state.is_null() {
+ // shoudln't happen
+ None
+ } else {
Some(unsafe {
pg_sys::makeArrayResult(state, PgMemoryContexts::CurrentMemoryContext.value())
})
}
+ }
fn type_oid() -> u32 {
unsafe { pg_sys::get_array_type(T::type_oid()) }
