Skip to content

Commit

Permalink
chore(util): replace an unsafe libc::mkdtemp call
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Jul 10, 2024
1 parent 615b821 commit ca6fa87
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ shlex = "1.3.0"
strum = { version = "0.26", features = ["derive"] }
strum_macros = "0.26"
libc = "0.2"
nix = { version = "0.29.0", features = ["signal", "process"] }
nix = { version = "0.29.0", features = ["signal", "process", "fs", "feature"] }
thiserror = "1.0.61"

[profile.release]
Expand Down
23 changes: 4 additions & 19 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
use crate::types::IpAddr;
use libc::{c_char, mkdtemp};
use std::ffi::{CStr, CString};
use nix::unistd::mkdtemp;
use std::fs;
use std::io::{self, BufRead, BufReader, ErrorKind, Read};
use std::io::{self, BufRead, BufReader, Read};
use std::path::PathBuf;

fn impl_tempdir(tpl: &str) -> io::Result<PathBuf> {
use io::Error;

let tpl = CString::new(tpl).map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;

unsafe {
let ptr: *const c_char = mkdtemp(tpl.as_ptr() as *mut c_char);

if ptr.is_null() {
return Err(std::io::Error::last_os_error());
}

CStr::from_ptr(ptr)
}
.to_str()
.map_err(|e| Error::new(ErrorKind::Other, e))
.map(PathBuf::from)
Ok(mkdtemp(tpl)?)
}

pub(crate) fn tempdir() -> io::Result<PathBuf> {
Expand Down Expand Up @@ -124,6 +108,7 @@ pub(crate) fn join_shell_args<T: AsRef<str>>(args: &Vec<T>) -> String {
#[cfg(test)]
mod tests {
use super::*;
use std::io::ErrorKind;

#[test]
fn test_split_shell_args() {
Expand Down

0 comments on commit ca6fa87

Please sign in to comment.