Fix potential use-after-free

Clippy pointed out this potential use-after-free of a CString buffer. I
don't think in this case it caused an issue because the temporary
CString _should_ outlive the function call (at least, that's how C++
works...), however it's refactor-brittle, so best to fix.

https://github.com/Manishearth/rust-clippy/wiki#temporary_cstring_as_ptr
without.crypto
Dan Burkert 8 years ago
parent de8bdbd890
commit 2c43e693e6
  1. 1
      src/cursor.rs
  2. 11
      src/environment.rs
  3. 1
      src/error.rs

@ -290,7 +290,6 @@ mod test {
use flags::*; use flags::*;
use super::*; use super::*;
use test_utils::*; use test_utils::*;
use transaction::*;
#[test] #[test]
fn test_get() { fn test_get() {

@ -1,5 +1,5 @@
use libc::{c_uint, size_t}; use libc::{c_uint, size_t};
use std::ffi::CString; use std::ffi::{CString, NulError};
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
#[cfg(windows)] #[cfg(windows)]
@ -207,10 +207,11 @@ impl EnvironmentBuilder {
lmdb_try_with_cleanup!(ffi::mdb_env_set_mapsize(env, map_size), lmdb_try_with_cleanup!(ffi::mdb_env_set_mapsize(env, map_size),
ffi::mdb_env_close(env)) ffi::mdb_env_close(env))
} }
lmdb_try_with_cleanup!(ffi::mdb_env_open(env, let path = match CString::new(path.as_os_str().as_bytes()) {
CString::new(path.as_os_str().as_bytes()).unwrap().as_ptr(), Ok(path) => path,
self.flags.bits(), Err(NulError { .. }) => return Err(::Error::Invalid),
mode), };
lmdb_try_with_cleanup!(ffi::mdb_env_open(env, path.as_ptr(), self.flags.bits(), mode),
ffi::mdb_env_close(env)); ffi::mdb_env_close(env));
} }
Ok(Environment { env: env, dbi_open_mutex: Mutex::new(()) }) Ok(Environment { env: env, dbi_open_mutex: Mutex::new(()) })

@ -145,5 +145,4 @@ mod test {
assert_eq!("MDB_NOTFOUND: No matching key/data pair found", assert_eq!("MDB_NOTFOUND: No matching key/data pair found",
Error::NotFound.description()); Error::NotFound.description());
} }
} }

Loading…
Cancel
Save