Skip to content

Commit ff265b2

Browse files
committed
Check the validity of the env and transaction when opening databases
1 parent 6a4fe85 commit ff265b2

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

heed/src/db/polymorph.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl PolyDatabase {
158158
KC: BytesEncode<'a>,
159159
DC: BytesDecode<'txn>,
160160
{
161-
assert_matching_env_txn!(self, txn);
161+
assert_eq_env_db_txn!(self, txn);
162162

163163
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
164164

@@ -232,7 +232,7 @@ impl PolyDatabase {
232232
KC: BytesEncode<'a> + BytesDecode<'txn>,
233233
DC: BytesDecode<'txn>,
234234
{
235-
assert_matching_env_txn!(self, txn);
235+
assert_eq_env_db_txn!(self, txn);
236236

237237
let mut cursor = RoCursor::new(txn, self.dbi)?;
238238
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
@@ -300,7 +300,7 @@ impl PolyDatabase {
300300
KC: BytesEncode<'a> + BytesDecode<'txn>,
301301
DC: BytesDecode<'txn>,
302302
{
303-
assert_matching_env_txn!(self, txn);
303+
assert_eq_env_db_txn!(self, txn);
304304

305305
let mut cursor = RoCursor::new(txn, self.dbi)?;
306306
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
@@ -372,7 +372,7 @@ impl PolyDatabase {
372372
KC: BytesEncode<'a> + BytesDecode<'txn>,
373373
DC: BytesDecode<'txn>,
374374
{
375-
assert_matching_env_txn!(self, txn);
375+
assert_eq_env_db_txn!(self, txn);
376376

377377
let mut cursor = RoCursor::new(txn, self.dbi)?;
378378
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
@@ -443,7 +443,7 @@ impl PolyDatabase {
443443
KC: BytesEncode<'a> + BytesDecode<'txn>,
444444
DC: BytesDecode<'txn>,
445445
{
446-
assert_matching_env_txn!(self, txn);
446+
assert_eq_env_db_txn!(self, txn);
447447

448448
let mut cursor = RoCursor::new(txn, self.dbi)?;
449449
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
@@ -497,7 +497,7 @@ impl PolyDatabase {
497497
KC: BytesDecode<'txn>,
498498
DC: BytesDecode<'txn>,
499499
{
500-
assert_matching_env_txn!(self, txn);
500+
assert_eq_env_db_txn!(self, txn);
501501

502502
let mut cursor = RoCursor::new(txn, self.dbi)?;
503503
match cursor.move_on_first() {
@@ -550,7 +550,7 @@ impl PolyDatabase {
550550
KC: BytesDecode<'txn>,
551551
DC: BytesDecode<'txn>,
552552
{
553-
assert_matching_env_txn!(self, txn);
553+
assert_eq_env_db_txn!(self, txn);
554554

555555
let mut cursor = RoCursor::new(txn, self.dbi)?;
556556
match cursor.move_on_last() {
@@ -603,7 +603,7 @@ impl PolyDatabase {
603603
/// # Ok(()) }
604604
/// ```
605605
pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result<u64> {
606-
assert_matching_env_txn!(self, txn);
606+
assert_eq_env_db_txn!(self, txn);
607607

608608
let mut db_stat = mem::MaybeUninit::uninit();
609609
let result = unsafe { mdb_result(ffi::mdb_stat(txn.txn, self.dbi, db_stat.as_mut_ptr())) };
@@ -656,7 +656,7 @@ impl PolyDatabase {
656656
/// # Ok(()) }
657657
/// ```
658658
pub fn is_empty<'txn>(&self, txn: &'txn RoTxn) -> Result<bool> {
659-
assert_matching_env_txn!(self, txn);
659+
assert_eq_env_db_txn!(self, txn);
660660

661661
let mut cursor = RoCursor::new(txn, self.dbi)?;
662662
match cursor.move_on_first()? {
@@ -702,7 +702,7 @@ impl PolyDatabase {
702702
/// # Ok(()) }
703703
/// ```
704704
pub fn iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result<RoIter<'txn, KC, DC>> {
705-
assert_matching_env_txn!(self, txn);
705+
assert_eq_env_db_txn!(self, txn);
706706

707707
RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor))
708708
}
@@ -757,7 +757,7 @@ impl PolyDatabase {
757757
/// # Ok(()) }
758758
/// ```
759759
pub fn iter_mut<'txn, KC, DC>(&self, txn: &'txn mut RwTxn) -> Result<RwIter<'txn, KC, DC>> {
760-
assert_matching_env_txn!(self, txn);
760+
assert_eq_env_db_txn!(self, txn);
761761

762762
RwCursor::new(txn, self.dbi).map(|cursor| RwIter::new(cursor))
763763
}
@@ -799,7 +799,7 @@ impl PolyDatabase {
799799
/// # Ok(()) }
800800
/// ```
801801
pub fn rev_iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result<RoRevIter<'txn, KC, DC>> {
802-
assert_matching_env_txn!(self, txn);
802+
assert_eq_env_db_txn!(self, txn);
803803

804804
RoCursor::new(txn, self.dbi).map(|cursor| RoRevIter::new(cursor))
805805
}
@@ -858,7 +858,7 @@ impl PolyDatabase {
858858
&self,
859859
txn: &'txn mut RwTxn,
860860
) -> Result<RwRevIter<'txn, KC, DC>> {
861-
assert_matching_env_txn!(self, txn);
861+
assert_eq_env_db_txn!(self, txn);
862862

863863
RwCursor::new(txn, self.dbi).map(|cursor| RwRevIter::new(cursor))
864864
}
@@ -911,7 +911,7 @@ impl PolyDatabase {
911911
KC: BytesEncode<'a>,
912912
R: RangeBounds<KC::EItem>,
913913
{
914-
assert_matching_env_txn!(self, txn);
914+
assert_eq_env_db_txn!(self, txn);
915915

916916
let start_bound = match range.start_bound() {
917917
Bound::Included(bound) => {
@@ -1002,7 +1002,7 @@ impl PolyDatabase {
10021002
KC: BytesEncode<'a>,
10031003
R: RangeBounds<KC::EItem>,
10041004
{
1005-
assert_matching_env_txn!(self, txn);
1005+
assert_eq_env_db_txn!(self, txn);
10061006

10071007
let start_bound = match range.start_bound() {
10081008
Bound::Included(bound) => {
@@ -1080,7 +1080,7 @@ impl PolyDatabase {
10801080
KC: BytesEncode<'a>,
10811081
R: RangeBounds<KC::EItem>,
10821082
{
1083-
assert_matching_env_txn!(self, txn);
1083+
assert_eq_env_db_txn!(self, txn);
10841084

10851085
let start_bound = match range.start_bound() {
10861086
Bound::Included(bound) => {
@@ -1171,7 +1171,7 @@ impl PolyDatabase {
11711171
KC: BytesEncode<'a>,
11721172
R: RangeBounds<KC::EItem>,
11731173
{
1174-
assert_matching_env_txn!(self, txn);
1174+
assert_eq_env_db_txn!(self, txn);
11751175

11761176
let start_bound = match range.start_bound() {
11771177
Bound::Included(bound) => {
@@ -1249,7 +1249,7 @@ impl PolyDatabase {
12491249
where
12501250
KC: BytesEncode<'a>,
12511251
{
1252-
assert_matching_env_txn!(self, txn);
1252+
assert_eq_env_db_txn!(self, txn);
12531253

12541254
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
12551255
let prefix_bytes = prefix_bytes.into_owned();
@@ -1318,7 +1318,7 @@ impl PolyDatabase {
13181318
where
13191319
KC: BytesEncode<'a>,
13201320
{
1321-
assert_matching_env_txn!(self, txn);
1321+
assert_eq_env_db_txn!(self, txn);
13221322

13231323
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
13241324
let prefix_bytes = prefix_bytes.into_owned();
@@ -1374,7 +1374,7 @@ impl PolyDatabase {
13741374
where
13751375
KC: BytesEncode<'a>,
13761376
{
1377-
assert_matching_env_txn!(self, txn);
1377+
assert_eq_env_db_txn!(self, txn);
13781378

13791379
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
13801380
let prefix_bytes = prefix_bytes.into_owned();
@@ -1443,7 +1443,7 @@ impl PolyDatabase {
14431443
where
14441444
KC: BytesEncode<'a>,
14451445
{
1446-
assert_matching_env_txn!(self, txn);
1446+
assert_eq_env_db_txn!(self, txn);
14471447

14481448
let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?;
14491449
let prefix_bytes = prefix_bytes.into_owned();
@@ -1493,7 +1493,7 @@ impl PolyDatabase {
14931493
KC: BytesEncode<'a>,
14941494
DC: BytesEncode<'a>,
14951495
{
1496-
assert_matching_env_txn!(self, txn);
1496+
assert_eq_env_db_txn!(self, txn);
14971497

14981498
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
14991499
let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?;
@@ -1554,7 +1554,7 @@ impl PolyDatabase {
15541554
KC: BytesEncode<'a>,
15551555
F: FnMut(&mut ReservedSpace) -> io::Result<()>,
15561556
{
1557-
assert_matching_env_txn!(self, txn);
1557+
assert_eq_env_db_txn!(self, txn);
15581558

15591559
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
15601560
let mut key_val = unsafe { crate::into_val(&key_bytes) };
@@ -1620,7 +1620,7 @@ impl PolyDatabase {
16201620
KC: BytesEncode<'a>,
16211621
DC: BytesEncode<'a>,
16221622
{
1623-
assert_matching_env_txn!(self, txn);
1623+
assert_eq_env_db_txn!(self, txn);
16241624

16251625
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
16261626
let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?;
@@ -1681,7 +1681,7 @@ impl PolyDatabase {
16811681
where
16821682
KC: BytesEncode<'a>,
16831683
{
1684-
assert_matching_env_txn!(self, txn);
1684+
assert_eq_env_db_txn!(self, txn);
16851685

16861686
let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?;
16871687
let mut key_val = unsafe { crate::into_val(&key_bytes) };
@@ -1749,7 +1749,7 @@ impl PolyDatabase {
17491749
KC: BytesEncode<'a> + BytesDecode<'txn>,
17501750
R: RangeBounds<KC::EItem>,
17511751
{
1752-
assert_matching_env_txn!(self, txn);
1752+
assert_eq_env_db_txn!(self, txn);
17531753

17541754
let mut count = 0;
17551755
let mut iter = self.range_mut::<KC, DecodeIgnore, _>(txn, range)?;
@@ -1806,7 +1806,7 @@ impl PolyDatabase {
18061806
/// # Ok(()) }
18071807
/// ```
18081808
pub fn clear(&self, txn: &mut RwTxn) -> Result<()> {
1809-
assert_matching_env_txn!(self, txn);
1809+
assert_eq_env_db_txn!(self, txn);
18101810

18111811
unsafe { mdb_result(ffi::mdb_drop(txn.txn.txn, self.dbi, 0)).map_err(Into::into) }
18121812
}

heed/src/env.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use synchronoise::event::SignalEvent;
2222

2323
use crate::mdb::error::mdb_result;
2424
use crate::mdb::ffi;
25-
use crate::{Database, Error, Flags, PolyDatabase, Result, RoCursor, RoTxn, RwTxn};
25+
use crate::{
26+
assert_eq_env_txn, Database, Error, Flags, PolyDatabase, Result, RoCursor, RoTxn, RwTxn,
27+
};
2628

2729
/// The list of opened environments, the value is an optional environment, it is None
2830
/// when someone asks to close the environment, closing is a two-phase step, to make sure
@@ -297,7 +299,7 @@ impl Drop for EnvInner {
297299
unsafe {
298300
let _ = ffi::mdb_env_close(self.env);
299301
}
300-
// We signal to all the waiters that we have closed the env.
302+
// We signal to all the waiters that the env is closed now.
301303
signal_event.signal();
302304
}
303305
}
@@ -442,6 +444,8 @@ impl Env {
442444
KC: 'static,
443445
DC: 'static,
444446
{
447+
assert_eq_env_txn!(self, rtxn);
448+
445449
let types = (TypeId::of::<KC>(), TypeId::of::<DC>());
446450
match self.raw_init_database(rtxn.txn, name, Some(types), false) {
447451
Ok(dbi) => Ok(Some(Database::new(self.env_mut_ptr() as _, dbi))),
@@ -464,6 +468,8 @@ impl Env {
464468
rtxn: &RoTxn,
465469
name: Option<&str>,
466470
) -> Result<Option<PolyDatabase>> {
471+
assert_eq_env_txn!(self, rtxn);
472+
467473
match self.raw_init_database(rtxn.txn, name, None, false) {
468474
Ok(dbi) => Ok(Some(PolyDatabase::new(self.env_mut_ptr() as _, dbi))),
469475
Err(Error::Mdb(e)) if e.not_found() => Ok(None),
@@ -489,6 +495,8 @@ impl Env {
489495
KC: 'static,
490496
DC: 'static,
491497
{
498+
assert_eq_env_txn!(self, wtxn);
499+
492500
let types = (TypeId::of::<KC>(), TypeId::of::<DC>());
493501
match self.raw_init_database(wtxn.txn.txn, name, Some(types), true) {
494502
Ok(dbi) => Ok(Database::new(self.env_mut_ptr() as _, dbi)),
@@ -510,6 +518,8 @@ impl Env {
510518
wtxn: &mut RwTxn,
511519
name: Option<&str>,
512520
) -> Result<PolyDatabase> {
521+
assert_eq_env_txn!(self, wtxn);
522+
513523
match self.raw_init_database(wtxn.txn.txn, name, None, true) {
514524
Ok(dbi) => Ok(PolyDatabase::new(self.env_mut_ptr() as _, dbi)),
515525
Err(e) => Err(e),

heed/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ mod tests {
146146
}
147147
}
148148

149-
macro_rules! assert_matching_env_txn {
149+
macro_rules! assert_eq_env_db_txn {
150150
($database:ident, $txn:ident) => {
151151
assert!(
152152
$database.env_ident == $txn.env_mut_ptr() as usize,
@@ -155,4 +155,13 @@ macro_rules! assert_matching_env_txn {
155155
};
156156
}
157157

158-
pub(crate) use assert_matching_env_txn;
158+
macro_rules! assert_eq_env_txn {
159+
($env:ident, $txn:ident) => {
160+
assert!(
161+
$env.env_mut_ptr() == $txn.env_mut_ptr(),
162+
"The environment doesn't match the transaction's environment"
163+
);
164+
};
165+
}
166+
167+
pub(crate) use {assert_eq_env_db_txn, assert_eq_env_txn};

0 commit comments

Comments
 (0)