Skip to content

Commit 3c4dcf8

Browse files
committed
Add a canary to PostgresConnection
This is a workaround for rust-lang/rust#13246 to prevent total badness until it gets fixed. cc #34, #31
1 parent 0d70c17 commit 3c4dcf8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ macro_rules! try_desync(
183183
)
184184

185185
macro_rules! check_desync(
186-
($e:expr) => (
186+
($e:expr) => ({
187+
if $e.canary() != CANARY {
188+
fail!("PostgresConnection use after free. See mozilla/rust#13246.");
189+
}
187190
if $e.is_desynchronized() {
188191
return Err(PgStreamDesynchronized);
189192
}
190-
)
193+
})
191194
)
192195

193196
pub mod error;
@@ -198,6 +201,7 @@ pub mod types;
198201
mod test;
199202

200203
static DEFAULT_PORT: Port = 5432;
204+
static CANARY: u32 = 0xdeadbeef;
201205

202206
/// A typedef of the result returned by many methods.
203207
pub type PostgresResult<T> = Result<T, PostgresError>;
@@ -392,6 +396,7 @@ struct InnerPostgresConnection {
392396
unknown_types: HashMap<Oid, ~str>,
393397
desynchronized: bool,
394398
finished: bool,
399+
canary: u32,
395400
}
396401

397402
impl Drop for InnerPostgresConnection {
@@ -438,6 +443,7 @@ impl InnerPostgresConnection {
438443
unknown_types: HashMap::new(),
439444
desynchronized: false,
440445
finished: false,
446+
canary: CANARY,
441447
};
442448

443449
args.push((~"client_encoding", ~"UTF8"));
@@ -644,6 +650,10 @@ impl InnerPostgresConnection {
644650
self.desynchronized
645651
}
646652

653+
fn canary(&self) -> u32 {
654+
self.canary
655+
}
656+
647657
fn wait_for_ready(&mut self) -> PostgresResult<()> {
648658
match try_pg!(self.read_message()) {
649659
ReadyForQuery { .. } => Ok(()),
@@ -677,6 +687,7 @@ impl InnerPostgresConnection {
677687

678688
fn finish_inner(&mut self) -> PostgresResult<()> {
679689
check_desync!(self);
690+
self.canary = 0;
680691
Ok(try_pg!(self.write_messages([Terminate])))
681692
}
682693
}
@@ -835,6 +846,10 @@ impl PostgresConnection {
835846
conn.finish_inner()
836847
}
837848

849+
fn canary(&self) -> u32 {
850+
self.conn.borrow_mut().canary()
851+
}
852+
838853
fn quick_query(&self, query: &str)
839854
-> PostgresResult<Vec<Vec<Option<~str>>>> {
840855
self.conn.borrow_mut().quick_query(query)

0 commit comments

Comments
 (0)