mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
unlink: Use a NUL-terminated string when calling unlink().
This commit is contained in:
parent
dd0d23839a
commit
f06972ccfd
1 changed files with 4 additions and 3 deletions
|
@ -19,7 +19,7 @@ extern crate uucore;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use libc::{S_IFLNK, S_IFMT, S_IFREG};
|
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::io::{Error, ErrorKind};
|
||||||
use std::mem::uninitialized;
|
use std::mem::uninitialized;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
@ -68,9 +68,10 @@ 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 st_mode = {
|
||||||
let mut buf: stat = unsafe { uninitialized() };
|
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 {
|
let result = unsafe {
|
||||||
lstat(
|
lstat(
|
||||||
c_string.as_ptr(),
|
c_string.as_ptr(),
|
||||||
|
@ -96,7 +97,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
"Not a regular file or symlink",
|
"Not a regular file or symlink",
|
||||||
))
|
))
|
||||||
} else {
|
} 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 {
|
if result < 0 {
|
||||||
Err(Error::last_os_error())
|
Err(Error::last_os_error())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue