Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix IntoDatum for CString and CStr #1063

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions pgx/src/datum/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,26 @@ impl IntoDatum for char {
}

/// for cstring
///
/// ## Safety
///
/// The `&CStr` better be allocated by Postgres
impl<'a> IntoDatum for &'a core::ffi::CStr {
/// The [`core::ffi::CStr`] is copied to `palloc`'d memory. That memory will either be freed by
/// Postgres when [`pg_sys::CurrentMemoryContext`] is reset, or when the function you passed the
/// returned Datum to decides to free it.
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(self.as_ptr().into())
unsafe {
// SAFETY: A `CStr` has already been validated to be a non-null pointer to a null-terminated
// "char *", and it won't ever overlap with a newly palloc'd block of memory. Using
// `to_bytes_with_nul()` ensures that we'll never try to palloc zero bytes -- it'll at
// least always be 1 byte to hold the null terminator for the empty string.
//
// This is akin to Postgres' `pg_sys::pstrdup` or even `pg_sys::pnstrdup` functions, but
// doing the copy ourselves allows us to elide the "strlen" or "strnlen" operations those
// functions need to do; the byte slice returned from `to_bytes_with_nul` knows its length.
let bytes = self.to_bytes_with_nul();
let copy = pg_sys::palloc(bytes.len()).cast();
core::ptr::copy_nonoverlapping(bytes.as_ptr(), copy, bytes.len());
Some(copy.into())
}
}

fn type_oid() -> pg_sys::Oid {
Expand All @@ -347,9 +359,12 @@ impl<'a> IntoDatum for &'a core::ffi::CStr {
}

impl IntoDatum for alloc::ffi::CString {
/// The [`core::ffi::CString`] is copied to `palloc`'d memory. That memory will either be freed by
/// Postgres when [`pg_sys::CurrentMemoryContext`] is reset, or when the function you passed the
/// returned Datum to decides to free it.
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(self.as_ptr().into())
self.as_c_str().into_datum()
}

fn type_oid() -> pg_sys::Oid {
Expand Down