Skip to content

Commit

Permalink
Demand Ord for PostgresOrd (#1262)
Browse files Browse the repository at this point in the history
Previously, the `left.cmp(&right)` call could resolve to `Iterator::cmp`.
A malicious impl could even force it to use `fn(&mut self, rhs: &Self)`!
Code exploiting this bug is unlikely, but it could hit other errors,
so this way the error emitted is much nicer.

Also makes the previous trybuild test for PostgresEq more realistic.
  • Loading branch information
workingjubilee authored Aug 17, 2023
1 parent fe7fe7c commit a8a09a0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pgrx-macros/src/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn derive_pg_cmp(name: &Ident, path: &proc_macro2::TokenStream) -> proc_macr
#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_extern(immutable, parallel_safe)]
fn #pg_name(left: #path, right: #path) -> i32 {
left.cmp(&right) as i32
::core::cmp::Ord::cmp(&left, &right) as i32
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pgrx-tests/tests/ui/total_eq_for_postgres_eq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use pgrx::prelude::*;
use serde::{Serialize, Deserialize};

#[derive(PartialEq, PostgresEq)]
struct BrokenType {
#[derive(Serialize, Deserialize, PartialEq, PostgresType, PostgresEq)]
pub struct BrokenType {
int: i32,
}

Expand Down
10 changes: 5 additions & 5 deletions pgrx-tests/tests/ui/total_eq_for_postgres_eq.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `BrokenType: std::cmp::Eq` is not satisfied
--> tests/ui/total_eq_for_postgres_eq.rs:4:8
--> tests/ui/total_eq_for_postgres_eq.rs:5:12
|
4 | struct BrokenType {
| ^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `BrokenType`
5 | pub struct BrokenType {
| ^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `BrokenType`
|
note: required by a bound in `PostgresEqRequiresTotalEq`
--> $WORKSPACE/pgrx/src/deriving.rs
Expand All @@ -11,6 +11,6 @@ note: required by a bound in `PostgresEqRequiresTotalEq`
| ^^ required by this bound in `PostgresEqRequiresTotalEq`
help: consider annotating `BrokenType` with `#[derive(Eq)]`
|
4 + #[derive(Eq)]
5 | struct BrokenType {
5 + #[derive(Eq)]
6 | pub struct BrokenType {
|
20 changes: 20 additions & 0 deletions pgrx-tests/tests/ui/total_ord_for_postgres_ord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use pgrx::prelude::*;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, PartialOrd, PostgresType, PostgresOrd)]
pub struct BrokenType {
int: i32,
}

impl BrokenType {
fn cmp(&mut self, _other: &Self) -> Anything {
Anything::Whatever
}
}

#[repr(u8)]
enum Anything {
Whatever = 0,
}

fn main() {}
12 changes: 12 additions & 0 deletions pgrx-tests/tests/ui/total_ord_for_postgres_ord.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: the trait bound `BrokenType: std::cmp::Ord` is not satisfied
--> tests/ui/total_ord_for_postgres_ord.rs:4:71
|
4 | #[derive(Serialize, Deserialize, PartialEq, PartialOrd, PostgresType, PostgresOrd)]
| ^^^^^^^^^^^ the trait `std::cmp::Ord` is not implemented for `BrokenType`
|
= note: this error originates in the derive macro `PostgresOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `BrokenType` with `#[derive(Ord)]`
|
5 + #[derive(Ord)]
6 | pub struct BrokenType {
|

0 comments on commit a8a09a0

Please sign in to comment.