mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust/utils: mkdtemp: fix memory leak
Fix memory leak of @template_raw. The documentation of CString::into_raw
reads:
"Consumes the CString and transfers ownership of the string to a C
caller.
...
Failure to call CString::from_raw will lead to a memory leak." [1]
Let's fix the memory leak by always calling `CString::from_raw` and
therefore reclaim the ownership.
[1] https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_raw
Fixes: e56acf4f14 ("pv_core: add `TemporaryDirectory`")
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
6520070a46
commit
3f6572e901
@@ -16,13 +16,14 @@ fn mkdtemp<P: AsRef<Path>>(template: P) -> Result<PathBuf, std::io::Error> {
|
||||
// SAFETY: template_raw is a valid CString because it was generated by
|
||||
// the `CString::new`.
|
||||
let ret = libc::mkdtemp(template_raw);
|
||||
// SAFETY: `template_raw` is still a valid CString because it was
|
||||
// generated by `CString::new` and modified by `libc::mkdtemp`.
|
||||
let path_cstr = std::ffi::CString::from_raw(template_raw);
|
||||
|
||||
if ret.is_null() {
|
||||
drop(path_cstr);
|
||||
Err(std::io::Error::last_os_error())
|
||||
} else {
|
||||
// SAFETY: `template_raw` is still a valid CString because it was
|
||||
// generated by `CString::new` and modified by `libc::mkdtemp`.
|
||||
let path_cstr = std::ffi::CString::from_raw(template_raw);
|
||||
let path = OsStr::from_bytes(path_cstr.as_bytes());
|
||||
let path = std::path::PathBuf::from(path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user