diff --git a/rust/utils/src/tmpfile.rs b/rust/utils/src/tmpfile.rs index 07acdba8..883d5586 100644 --- a/rust/utils/src/tmpfile.rs +++ b/rust/utils/src/tmpfile.rs @@ -16,13 +16,14 @@ fn mkdtemp>(template: P) -> Result { // 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);