1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

Merge pull request #1189 from ProgVal/unlink-nul-terminator

unlink: Add a nul-terminator after the file name before calling lstat.
This commit is contained in:
Alex Lyon 2018-05-02 11:45:32 -07:00 committed by GitHub
commit c50e7b5bc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,9 +19,10 @@ 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;
static NAME: &'static str = "unlink";
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
@ -67,11 +68,13 @@ pub fn uumain(args: Vec<String>) -> 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 result = unsafe {
lstat(
matches.free[0].as_ptr() as *const c_char,
c_string.as_ptr(),
&mut buf as *mut stat,
)
};
@ -94,7 +97,7 @@ pub fn uumain(args: Vec<String>) -> 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())