Skip to content

Commit

Permalink
Work around UB in LMDB bindings (sigp#6211)
Browse files Browse the repository at this point in the history
* Work around UB in LMDB bindings
  • Loading branch information
michaelsproul authored and AgeManning committed Sep 3, 2024
1 parent c356488 commit 71c2e37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion slasher/src/database/lmdb_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ impl<'env> Cursor<'env> {
}

pub fn get_current(&mut self) -> Result<Option<(Key<'env>, Value<'env>)>, Error> {
// FIXME: lmdb has an extremely broken API which can mutate the SHARED REFERENCE
// `value` after `get_current` is called. We need to convert it to a Vec here in order
// to avoid `value` changing after another cursor operation. I think this represents a bug
// in the LMDB bindings, as shared references should be immutable.
if let Some((Some(key), value)) = self.cursor.get(None, None, MDB_GET_CURRENT).optional()? {
Ok(Some((Cow::Borrowed(key), Cow::Borrowed(value))))
Ok(Some((Cow::Borrowed(key), Cow::Owned(value.to_vec()))))
} else {
Ok(None)
}
Expand Down
5 changes: 5 additions & 0 deletions slasher/tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ fn no_crash_blocks_example1() {
},
);
}

#[test]
fn no_crash_aug_24() {
random_test(13519442335106054152, TestConfig::default())
}

0 comments on commit 71c2e37

Please sign in to comment.