Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/uu/setpgid/src/setpgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use clap::builder::ValueParser;
use clap::{crate_version, Arg, ArgAction, Command as ClapCommand};
use uucore::{
error::{UResult, USimpleError},
Expand All @@ -15,14 +16,15 @@ const USAGE: &str = help_usage!("setpgid.md");
#[cfg(target_family = "unix")]
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
use std::ffi::CString;
use std::ffi::{CString, OsString};
use std::fs::File;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::AsRawFd;

let matches = uu_app().try_get_matches_from(args)?;

let remaining_args: Vec<String> = matches
.get_many::<String>("args")
let remaining_args: Vec<OsString> = matches
.get_many::<OsString>("args")
.unwrap()
.cloned()
.collect();
Expand Down Expand Up @@ -50,16 +52,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let program_args = &remaining_args[1..];

// Command line arguments can't contain NUL bytes, so unwrap() is safe here.
let program_cstr = CString::new(program.as_str()).unwrap();
let program_cstr = CString::new(program.as_bytes()).unwrap();
let mut argv = vec![program_cstr.clone()];
for arg in program_args {
argv.push(CString::new(arg.as_str()).unwrap());
argv.push(CString::new(arg.as_bytes()).unwrap());
}

let Err(e) = nix::unistd::execvp(&program_cstr, &argv);
Err(USimpleError::new(
1,
format!("failed to execute '{}': {}", program, e),
format!("failed to execute '{}': {}", program.to_string_lossy(), e),
))
}

Expand Down Expand Up @@ -94,6 +96,7 @@ pub fn uu_app() -> ClapCommand {
.required(true)
.action(ArgAction::Append)
.num_args(1..)
.value_parser(ValueParser::os_string())
.trailing_var_arg(true),
)
}
29 changes: 29 additions & 0 deletions tests/by-util/test_setpgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,32 @@ fn test_flag_after_command() {
.succeeds()
.stdout_is("-f\n");
}

#[test]
#[cfg(target_family = "unix")]
fn test_non_utf8_arguments() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

new_ucmd!()
.arg("echo")
.arg(OsStr::from_bytes(b"\xff"))
.succeeds()
.stdout_is_bytes(b"\xff\n");
}

#[test]
#[cfg(all(unix, not(target_os = "macos")))]
fn test_non_utf8_program_name() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use uutests::at_and_ucmd;

let (at, mut ucmd) = at_and_ucmd!();
let program = at.plus(OsStr::from_bytes(b"sh_\xff"));
std::os::unix::fs::symlink("/bin/sh", &program).unwrap();
ucmd.arg(&program)
.args(&["-c", "echo ran"])
.succeeds()
.stdout_is("ran\n");
}
Loading