Skip to content

Commit

Permalink
Merge branch 'main' into feature/env_string_args_try_with_shell_words
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture authored Jan 14, 2024
2 parents 394e87c + ac27b6c commit 47d5b55
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ jobs:
arm-unknown-linux-gnueabihf) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;;
aarch64-unknown-linux-*) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;;
*-redox*) sudo apt-get -y update ; sudo apt-get -y install fuse3 libfuse-dev ;;
# Update binutils if MinGW due to https://github.com/rust-lang/rust/issues/112368
x86_64-pc-windows-gnu) C:/msys64/usr/bin/pacman.exe -Syu --needed mingw-w64-x86_64-gcc --noconfirm ; echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH ;;
esac
case '${{ matrix.job.os }}' in
macos-latest) brew install coreutils ;; # needed for testing
Expand Down Expand Up @@ -984,6 +986,10 @@ jobs:
echo "foo" > /home/runner/.plan
;;
esac
case '${{ matrix.job.os }}' in
# Update binutils if MinGW due to https://github.com/rust-lang/rust/issues/112368
windows-latest) C:/msys64/usr/bin/pacman.exe -Syu --needed mingw-w64-x86_64-gcc --noconfirm ; echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH ;;
esac
- name: Initialize toolchain-dependent workflow variables
id: dep_vars
shell: bash
Expand Down
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ self_cell = "1.0.3"
selinux = "0.4"
signal-hook = "0.3.17"
smallvec = { version = "1.11", features = ["union"] }
tempfile = "3.8.1"
tempfile = "3.9.0"
uutils_term_grid = "0.3"
terminal_size = "0.3.0"
textwrap = { version = "0.16.0", features = ["terminal_size"] }
Expand Down
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ skip = [
{ name = "linux-raw-sys", version = "0.3.8" },
# terminal_size
{ name = "rustix", version = "0.37.26" },
# various crates
# notify
{ name = "windows-sys", version = "0.45.0" },
# various crates
{ name = "windows-sys", version = "0.48.0" },
Expand Down
5 changes: 4 additions & 1 deletion fuzz/fuzz_targets/fuzz_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ pub fn run_gnu_cmd(

let output = if let Some(input_str) = pipe_input {
// We have an pipe input
command.stdin(Stdio::piped()).stdout(Stdio::piped());
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());

let mut child = command.spawn().expect("Failed to execute command");
let child_stdin = child.stdin.as_mut().unwrap();
Expand Down
19 changes: 9 additions & 10 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::io::{self, stdin, stdout, BufReader, Read, Write};
use std::iter;
use std::path::Path;
use uucore::{
error::{FromIo, UError, UResult},
format_usage, help_about, help_section, help_usage,
error::{FromIo, UError, UResult, USimpleError},
format_usage, help_about, help_section, help_usage, show,
sum::{
div_ceil, Blake2b, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sm3,
BSD, CRC, SYSV,
Expand Down Expand Up @@ -174,7 +174,13 @@ where
});
let (sum, sz) = digest_read(&mut options.digest, &mut file, options.output_bits)
.map_err_context(|| "failed to read input".to_string())?;

if filename.is_dir() {
show!(USimpleError::new(
1,
format!("{}: Is a directory", filename.display())
));
continue;
}
if options.raw {
let bytes = match options.algo_name {
ALGORITHM_OPTIONS_CRC => sum.parse::<u32>().unwrap().to_be_bytes().to_vec(),
Expand Down Expand Up @@ -214,13 +220,6 @@ where
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
(ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => {
if filename.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("{}: Is a directory", filename.display()),
)
.into());
}
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
println!("BLAKE2b-{} ({}) = {sum}", length * 8, filename.display());
Expand Down
1 change: 1 addition & 0 deletions src/uu/cut/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ pub fn uu_app() -> Command {
.about(ABOUT)
.after_help(AFTER_HELP)
.infer_long_args(true)
.args_override_self(true)
.arg(
Arg::new(options::BYTES)
.short('b')
Expand Down
12 changes: 11 additions & 1 deletion src/uu/printf/src/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::ops::ControlFlow;

use clap::{crate_version, Arg, ArgAction, Command};
use uucore::error::{UResult, UUsageError};
use uucore::format::{parse_spec_and_escape, FormatArgument};
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
use uucore::{format_usage, help_about, help_section, help_usage};

const VERSION: &str = "version";
Expand All @@ -38,14 +38,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
None => vec![],
};

let mut format_seen = false;
let mut args = values.iter().peekable();
for item in parse_spec_and_escape(format_string.as_ref()) {
if let Ok(FormatItem::Spec(_)) = item {
format_seen = true;
}
match item?.write(stdout(), &mut args)? {
ControlFlow::Continue(()) => {}
ControlFlow::Break(()) => return Ok(()),
};
}

// Without format specs in the string, the iter would not consume any args,
// leading to an infinite loop. Thus, we exit early.
if !format_seen {
return Ok(());
}

while args.peek().is_some() {
for item in parse_spec_and_escape(format_string.as_ref()) {
match item?.write(stdout(), &mut args)? {
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/src/lib/features/format/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
};
match next {
FormatArgument::Char(c) => *c,
FormatArgument::Unparsed(s) => s.chars().next().unwrap_or('\0'),
FormatArgument::Unparsed(s) => s.bytes().next().map_or('\0', char::from),
_ => '\0',
}
}
Expand Down
1 change: 1 addition & 0 deletions src/uucore/src/lib/features/format/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub fn parse_escape_code(rest: &mut &[u8]) -> EscapedChar {
*rest = new_rest;
match c {
b'\\' => EscapedChar::Byte(b'\\'),
b'"' => EscapedChar::Byte(b'"'),
b'a' => EscapedChar::Byte(b'\x07'),
b'b' => EscapedChar::Byte(b'\x08'),
b'c' => EscapedChar::End,
Expand Down
55 changes: 40 additions & 15 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,6 @@ fn test_nonexisting_file() {
.stderr_contains(format!("cksum: {file_name}: No such file or directory"));
}

#[test]
fn test_folder() {
let (at, mut ucmd) = at_and_ucmd!();

let folder_name = "a_folder";
at.mkdir(folder_name);

ucmd.arg(folder_name)
.succeeds()
.stdout_only(format!("4294967295 0 {folder_name}\n"));
}

// Make sure crc is correct for files larger than 32 bytes
// but <128 bytes (1 fold pclmul) // spell-checker:disable-line
#[test]
Expand Down Expand Up @@ -312,15 +300,52 @@ fn test_raw_multiple_files() {
}

#[test]
fn test_blake2b_fail_on_directory() {
fn test_fail_on_folder() {
let (at, mut ucmd) = at_and_ucmd!();

let folder_name = "a_folder";
at.mkdir(folder_name);

ucmd.arg("--algorithm=blake2b")
.arg(folder_name)
ucmd.arg(folder_name)
.fails()
.no_stdout()
.stderr_contains(format!("cksum: {folder_name}: Is a directory"));
}

#[test]
fn test_all_algorithms_fail_on_folder() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;

let folder_name = "a_folder";
at.mkdir(folder_name);

for algo in ALGOS {
scene
.ucmd()
.arg(format!("--algorithm={algo}"))
.arg(folder_name)
.fails()
.no_stdout()
.stderr_contains(format!("cksum: {folder_name}: Is a directory"));
}
}

#[test]
fn test_folder_and_file() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;

let folder_name = "a_folder";
at.mkdir(folder_name);

scene
.ucmd()
.arg(folder_name)
.arg("lorem_ipsum.txt")
.fails()
.stderr_contains(format!("cksum: {folder_name}: Is a directory"))
.stdout_is_fixture("crc_single_file.expected");
}
10 changes: 10 additions & 0 deletions tests/by-util/test_cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,13 @@ fn test_equal_as_delimiter3() {
.succeeds()
.stdout_only_bytes("abZcd\n");
}

#[test]
fn test_multiple() {
let result = new_ucmd!()
.args(&["-f2", "-d:", "-d="])
.pipe_in("a=b\n")
.succeeds();
assert_eq!(result.stdout_str(), "b\n");
assert_eq!(result.stderr_str(), "");
}
15 changes: 15 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ fn escaped_slash() {
.stdout_only("hello\\ world");
}

#[test]
fn unescaped_double_quote() {
new_ucmd!().args(&["\\\""]).succeeds().stdout_only("\"");
}

#[test]
fn escaped_hex() {
new_ucmd!().args(&["\\x41"]).succeeds().stdout_only("A");
Expand Down Expand Up @@ -639,3 +644,13 @@ fn partial_char() {
"printf: warning: bc: character(s) following character constant have been ignored\n",
);
}

#[test]
fn char_as_byte() {
new_ucmd!().args(&["%c", "🙃"]).succeeds().stdout_only("ð");
}

#[test]
fn no_infinite_loop() {
new_ucmd!().args(&["a", "b"]).succeeds().stdout_only("a");
}

0 comments on commit 47d5b55

Please sign in to comment.