Skip to content

Commit c804e51

Browse files
committed
Print strings directly without UTF-8 sanitization
1 parent 15d22c2 commit c804e51

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

src/uu/hashsum/src/hashsum.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use uucore::checksum::detect_algo;
2525
use uucore::checksum::digest_reader;
2626
use uucore::checksum::escape_filename;
2727
use uucore::checksum::perform_checksum_validation;
28+
use uucore::display::print_verbatim;
2829
use uucore::error::{UResult, strip_errno};
2930
use uucore::format_usage;
3031
use uucore::sum::{Digest, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Shake128, Shake256};
@@ -597,7 +598,9 @@ where
597598
println!("{sum}");
598599
} else if options.zero {
599600
// with zero, we don't escape the filename
600-
print!("{sum} {binary_marker}{}\0", filename.display());
601+
print!("{sum} {binary_marker}");
602+
print_verbatim(filename).unwrap();
603+
print!("\0");
601604
} else {
602605
println!("{prefix}{sum} {binary_marker}{escaped_filename}");
603606
}

src/uu/head/src/head.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::num::TryFromIntError;
1414
#[cfg(unix)]
1515
use std::os::fd::{AsRawFd, FromRawFd};
1616
use thiserror::Error;
17-
use uucore::display::Quotable;
17+
use uucore::display::{Quotable, print_verbatim};
1818
use uucore::error::{FromIo, UError, UResult};
1919
use uucore::line_ending::LineEnding;
2020
use uucore::translate;
@@ -522,10 +522,9 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
522522
if !first {
523523
println!();
524524
}
525-
match file.to_str() {
526-
Some(name) => println!("==> {name} <=="),
527-
None => println!("==> {} <==", file.to_string_lossy()),
528-
}
525+
print!("==> ");
526+
print_verbatim(file).unwrap();
527+
println!(" <==");
529528
}
530529
head_file(&mut file_handle, options)?;
531530
Ok(())

src/uu/sum/src/sum.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::ffi::OsString;
1010
use std::fs::File;
1111
use std::io::{ErrorKind, Read, Write, stdin, stdout};
1212
use std::path::Path;
13-
use uucore::display::Quotable;
13+
use uucore::display::{OsWrite, Quotable};
1414
use uucore::error::{FromIo, UResult, USimpleError};
1515
use uucore::translate;
1616

@@ -126,11 +126,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
126126

127127
let mut stdout = stdout().lock();
128128
if print_names {
129-
writeln!(
130-
stdout,
131-
"{sum:0width$} {blocks:width$} {}",
132-
file.to_string_lossy()
133-
)?;
129+
write!(stdout, "{sum:0width$} {blocks:width$} ")?;
130+
stdout.write_all_os(file)?;
131+
stdout.write_all(b"\n")?;
134132
} else {
135133
writeln!(stdout, "{sum:0width$} {blocks:width$}")?;
136134
}

src/uu/tty/src/tty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use clap::{Arg, ArgAction, Command};
99
use std::io::{IsTerminal, Write};
10+
use uucore::display::OsWrite;
1011
use uucore::error::{UResult, set_exit_code};
1112
use uucore::format_usage;
1213

@@ -36,7 +37,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3637
let name = nix::unistd::ttyname(std::io::stdin());
3738

3839
let write_result = match name {
39-
Ok(name) => writeln!(stdout, "{}", name.display()),
40+
Ok(name) => stdout.write_all_os(name.as_os_str()),
4041
Err(_) => {
4142
set_exit_code(1);
4243
writeln!(stdout, "{}", translate!("tty-not-a-tty"))

src/uu/uname/src/uname.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
// spell-checker:ignore (API) nodename osname sysname (options) mnrsv mnrsvo
77

8+
use std::ffi::{OsStr, OsString};
9+
810
use clap::{Arg, ArgAction, Command};
911
use platform_info::*;
12+
use uucore::display::println_verbatim;
1013
use uucore::translate;
1114
use uucore::{
1215
error::{UResult, USimpleError},
@@ -26,18 +29,18 @@ pub mod options {
2629
}
2730

2831
pub struct UNameOutput {
29-
pub kernel_name: Option<String>,
30-
pub nodename: Option<String>,
31-
pub kernel_release: Option<String>,
32-
pub kernel_version: Option<String>,
33-
pub machine: Option<String>,
34-
pub os: Option<String>,
35-
pub processor: Option<String>,
36-
pub hardware_platform: Option<String>,
32+
pub kernel_name: Option<OsString>,
33+
pub nodename: Option<OsString>,
34+
pub kernel_release: Option<OsString>,
35+
pub kernel_version: Option<OsString>,
36+
pub machine: Option<OsString>,
37+
pub os: Option<OsString>,
38+
pub processor: Option<OsString>,
39+
pub hardware_platform: Option<OsString>,
3740
}
3841

3942
impl UNameOutput {
40-
fn display(&self) -> String {
43+
fn display(&self) -> OsString {
4144
[
4245
self.kernel_name.as_ref(),
4346
self.nodename.as_ref(),
@@ -50,9 +53,9 @@ impl UNameOutput {
5053
]
5154
.into_iter()
5255
.flatten()
53-
.map(|name| name.as_str())
56+
.map(|name| name.as_os_str())
5457
.collect::<Vec<_>>()
55-
.join(" ")
58+
.join(OsStr::new(" "))
5659
}
5760

5861
pub fn new(opts: &Options) -> UResult<Self> {
@@ -68,30 +71,28 @@ impl UNameOutput {
6871
|| opts.processor
6972
|| opts.hardware_platform);
7073

71-
let kernel_name = (opts.kernel_name || opts.all || none)
72-
.then(|| uname.sysname().to_string_lossy().to_string());
74+
let kernel_name =
75+
(opts.kernel_name || opts.all || none).then(|| uname.sysname().to_owned());
7376

74-
let nodename =
75-
(opts.nodename || opts.all).then(|| uname.nodename().to_string_lossy().to_string());
77+
let nodename = (opts.nodename || opts.all).then(|| uname.nodename().to_owned());
7678

77-
let kernel_release = (opts.kernel_release || opts.all)
78-
.then(|| uname.release().to_string_lossy().to_string());
79+
let kernel_release = (opts.kernel_release || opts.all).then(|| uname.release().to_owned());
7980

80-
let kernel_version = (opts.kernel_version || opts.all)
81-
.then(|| uname.version().to_string_lossy().to_string());
81+
let kernel_version = (opts.kernel_version || opts.all).then(|| uname.version().to_owned());
8282

83-
let machine =
84-
(opts.machine || opts.all).then(|| uname.machine().to_string_lossy().to_string());
83+
let machine = (opts.machine || opts.all).then(|| uname.machine().to_owned());
8584

86-
let os = (opts.os || opts.all).then(|| uname.osname().to_string_lossy().to_string());
85+
let os = (opts.os || opts.all).then(|| uname.osname().to_owned());
8786

8887
// This option is unsupported on modern Linux systems
8988
// See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
90-
let processor = opts.processor.then(|| translate!("uname-unknown"));
89+
let processor = opts.processor.then(|| translate!("uname-unknown").into());
9190

9291
// This option is unsupported on modern Linux systems
9392
// See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
94-
let hardware_platform = opts.hardware_platform.then(|| translate!("uname-unknown"));
93+
let hardware_platform = opts
94+
.hardware_platform
95+
.then(|| translate!("uname-unknown").into());
9596

9697
Ok(Self {
9798
kernel_name,
@@ -134,7 +135,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
134135
os: matches.get_flag(options::OS),
135136
};
136137
let output = UNameOutput::new(&options)?;
137-
println!("{}", output.display());
138+
println_verbatim(output.display().as_os_str()).unwrap();
138139
Ok(())
139140
}
140141

0 commit comments

Comments
 (0)