From f06972ccfdb33570579afb36e01313f1b94f7145 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 2 May 2018 19:13:36 +0200 Subject: [PATCH] unlink: Use a NUL-terminated string when calling unlink(). --- src/unlink/unlink.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/unlink/unlink.rs b/src/unlink/unlink.rs index 4dedcca71..adbc9e60d 100644 --- a/src/unlink/unlink.rs +++ b/src/unlink/unlink.rs @@ -19,7 +19,7 @@ extern crate uucore; use getopts::Options; use libc::{S_IFLNK, S_IFMT, S_IFREG}; -use libc::{c_char, lstat, stat, unlink}; +use libc::{lstat, stat, unlink}; use std::io::{Error, ErrorKind}; use std::mem::uninitialized; use std::ffi::CString; @@ -68,9 +68,10 @@ pub fn uumain(args: Vec) -> i32 { ); } + let c_string = CString::new(matches.free[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0. + let st_mode = { let mut buf: stat = unsafe { uninitialized() }; - let c_string = CString::new(matches.free[0].clone()).unwrap(); // unwrap() cannot fail, the string comes from argv so it cannot contain a \0. let result = unsafe { lstat( c_string.as_ptr(), @@ -96,7 +97,7 @@ pub fn uumain(args: Vec) -> i32 { "Not a regular file or symlink", )) } else { - let result = unsafe { unlink(matches.free[0].as_ptr() as *const c_char) }; + let result = unsafe { unlink(c_string.as_ptr()) }; if result < 0 { Err(Error::last_os_error())