-
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop SpiClient soundness from regressing
...by adding a regression test with trybuild! Adds ui tests using trybuild's test runner.
- Loading branch information
1 parent
ec8c42a
commit d0796ab
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use trybuild::TestCases; | ||
|
||
#[test] | ||
fn ui() { | ||
let t = TestCases::new(); | ||
t.compile_fail("tests/ui/*.rs"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use pgrx::prelude::*; | ||
use std::error::Error; | ||
|
||
#[pg_test] | ||
fn issue1209() -> Result<Option<String>, Box<dyn Error>> { | ||
// create the cursor we actually care about | ||
let mut res = Spi::connect(|c| { | ||
c.open_cursor("select 'hello' from generate_series(1, 10000)", None) | ||
.fetch(10000) | ||
.unwrap() | ||
}); | ||
|
||
// here we just perform some allocations to make sure that the previous cursor gets invalidated | ||
for _ in 0..1000 { | ||
Spi::connect(|c| c.open_cursor("select 1", None).fetch(1).unwrap()); | ||
} | ||
|
||
// later elements are probably more likely to point to deallocated memory | ||
for _ in 0..1000 { | ||
res.next(); | ||
} | ||
|
||
// segfault | ||
Ok(res.next().unwrap().get::<String>(1)?) | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error: lifetime may not live long enough | ||
--> tests/ui/escaping-spiclient-1209-cursor.rs:8:9 | ||
| | ||
7 | let mut res = Spi::connect(|c| { | ||
| -- return type of closure is SpiTupleTable<'2> | ||
| | | ||
| has type `SpiClient<'1>` | ||
8 | / c.open_cursor("select 'hello' from generate_series(1, 10000)", None) | ||
9 | | .fetch(10000) | ||
10 | | .unwrap() | ||
| |_____________________^ returning this value requires that `'1` must outlive `'2` | ||
|
||
error[E0515]: cannot return value referencing temporary value | ||
--> tests/ui/escaping-spiclient-1209-cursor.rs:8:9 | ||
| | ||
8 | c.open_cursor("select 'hello' from generate_series(1, 10000)", None) | ||
| ^------------------------------------------------------------------- | ||
| | | ||
| _________temporary value created here | ||
| | | ||
9 | | .fetch(10000) | ||
10 | | .unwrap() | ||
| |_____________________^ returns a value referencing data owned by the current function | ||
| | ||
= help: use `.collect()` to allocate the iterator | ||
|
||
error: lifetime may not live long enough | ||
--> tests/ui/escaping-spiclient-1209-cursor.rs:15:26 | ||
| | ||
15 | Spi::connect(|c| c.open_cursor("select 1", None).fetch(1).unwrap()); | ||
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` | ||
| || | ||
| |return type of closure is SpiTupleTable<'2> | ||
| has type `SpiClient<'1>` | ||
|
||
error[E0515]: cannot return value referencing temporary value | ||
--> tests/ui/escaping-spiclient-1209-cursor.rs:15:26 | ||
| | ||
15 | Spi::connect(|c| c.open_cursor("select 1", None).fetch(1).unwrap()); | ||
| -------------------------------^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| returns a value referencing data owned by the current function | ||
| temporary value created here | ||
| | ||
= help: use `.collect()` to allocate the iterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use pgrx::prelude::*; | ||
use std::error::Error; | ||
|
||
#[pg_extern] | ||
fn issue1209_prepared_stmt(q: &str) -> Result<Option<String>, Box<dyn Error>> { | ||
use pgrx::spi::Query; | ||
|
||
let prepared = { Spi::connect(|c| c.prepare(q, None))? }; | ||
|
||
Ok(Spi::connect(|c| prepared.execute(&c, Some(1), None)?.first().get(1))?) | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: lifetime may not live long enough | ||
--> tests/ui/escaping-spiclient-1209-prep-stmt.rs:8:39 | ||
| | ||
8 | let prepared = { Spi::connect(|c| c.prepare(q, None))? }; | ||
| -- ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` | ||
| || | ||
| |return type of closure is std::result::Result<PreparedStatement<'2>, pgrx::spi::Error> | ||
| has type `SpiClient<'1>` |