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

Fixed conversion of i64 and f64 to datums on 32-bit machines #1859

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ those remain untested. So far, some of PGRX's build tooling works on Windows, bu

‡ A local PostgreSQL server installation is not required. `cargo pgrx` can download and compile PostgreSQL versions on its own.

⹋ PGRX has not been tested to work on 32-bit: the library assumes an 8-byte `pg_sys::Datum`
which may result in unexpected behavior on 32-bit, like dropping 4 bytes of data from `int8`
and `double`. This may not be "unsound" in itself, as it is "merely" illogical,
but it may undermine otherwise-reasonable safety assumptions of PGRX extensions.
We do not plan to add support without considerable ongoing technical and financial contributions.
⹋ PGRX has not been tested to work on 32-bit, but the library correcly handles conversion of `pg_sys::Datum`
YohDeadfall marked this conversation as resolved.
Show resolved Hide resolved
to and from `int8` and `double` types. Use it only for your own risk. We do not plan to add offical support
without considerable ongoing technical and financial contributions.

<details style="border: 1px solid; padding: 0.25em 0.5em 0;">
<summary><i>How to:</i> <b>Homebrew on macOS</b></summary>
Expand Down
12 changes: 10 additions & 2 deletions pgrx-pg-sys/src/submodules/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ impl From<u32> for Datum {
impl From<u64> for Datum {
#[inline]
fn from(val: u64) -> Datum {
Datum::from(val as usize)
if size_of::<u64>() <= size_of::<usize>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if size_of::<u64>() <= size_of::<usize>() {
if size_of::<u64>() <= size_of::<Datum>() {

Datum::from(val as usize)
} else {
Datum::from(Box::into_raw(Box::new(val)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a palloc.

}
}
}

Expand Down Expand Up @@ -157,7 +161,11 @@ impl From<i32> for Datum {
impl From<i64> for Datum {
#[inline]
fn from(val: i64) -> Datum {
Datum::from(val as usize)
if size_of::<i64>() <= size_of::<usize>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if size_of::<i64>() <= size_of::<usize>() {
if size_of::<i64>() <= size_of::<Datum>() {

Datum::from(val as usize)
} else {
Datum::from(Box::into_raw(Box::new(val)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a palloc.

}
}
}

Expand Down
16 changes: 13 additions & 3 deletions pgrx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use crate::{
pg_sys, varlena, varlena_to_byte_slice, AllocatedByPostgres, IntoDatum, PgBox, PgMemoryContexts,
};
use core::ffi::CStr;
use core::{ffi::CStr, mem::size_of};
use std::num::NonZeroUsize;

/// If converting a Datum to a Rust type fails, this is the set of possible reasons why.
Expand Down Expand Up @@ -283,7 +283,12 @@ impl FromDatum for i64 {
if is_null {
None
} else {
Some(datum.value() as _)
let value = if size_of::<i64>() <= size_of::<usize>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = if size_of::<i64>() <= size_of::<usize>() {
let value = if size_of::<i64>() <= size_of::<pg_sys::Datum>() {

datum.value() as _
} else {
*(datum.cast_mut_ptr() as *const _)
};
Some(value)
}
}
}
Expand Down Expand Up @@ -315,7 +320,12 @@ impl FromDatum for f64 {
if is_null {
None
} else {
Some(f64::from_bits(datum.value() as _))
let value = if size_of::<i64>() <= size_of::<usize>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = if size_of::<i64>() <= size_of::<usize>() {
let value = if size_of::<f64>() <= size_of::<pg_sys::Datum>() {

f64::from_bits(datum.value() as _)
} else {
*(datum.cast_mut_ptr() as *const _)
};
Some(value)
}
}
}
Expand Down
Loading