From 2c43e693e65ca16476780bdd8442c5e1cb933022 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Sun, 25 Jun 2017 12:04:45 -0700 Subject: [PATCH] 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 --- src/cursor.rs | 1 - src/environment.rs | 11 ++++++----- src/error.rs | 1 - 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index 7bc8ce4..aa26746 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -290,7 +290,6 @@ mod test { use flags::*; use super::*; use test_utils::*; - use transaction::*; #[test] fn test_get() { diff --git a/src/environment.rs b/src/environment.rs index a2ebdcc..5962f3a 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,5 +1,5 @@ use libc::{c_uint, size_t}; -use std::ffi::CString; +use std::ffi::{CString, NulError}; #[cfg(unix)] use std::os::unix::ffi::OsStrExt; #[cfg(windows)] @@ -207,10 +207,11 @@ impl EnvironmentBuilder { lmdb_try_with_cleanup!(ffi::mdb_env_set_mapsize(env, map_size), ffi::mdb_env_close(env)) } - lmdb_try_with_cleanup!(ffi::mdb_env_open(env, - CString::new(path.as_os_str().as_bytes()).unwrap().as_ptr(), - self.flags.bits(), - mode), + let path = match CString::new(path.as_os_str().as_bytes()) { + Ok(path) => path, + Err(NulError { .. }) => return Err(::Error::Invalid), + }; + lmdb_try_with_cleanup!(ffi::mdb_env_open(env, path.as_ptr(), self.flags.bits(), mode), ffi::mdb_env_close(env)); } Ok(Environment { env: env, dbi_open_mutex: Mutex::new(()) }) diff --git a/src/error.rs b/src/error.rs index 5f73391..7907c40 100644 --- a/src/error.rs +++ b/src/error.rs @@ -145,5 +145,4 @@ mod test { assert_eq!("MDB_NOTFOUND: No matching key/data pair found", Error::NotFound.description()); } - }