Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printf: accept non-UTF-8 input in FORMAT and #6812

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 5 additions & 29 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use clap::builder::ValueParser;
use clap::parser::ValuesRef;
use clap::{crate_version, Arg, ArgAction, Command};
use std::ffi::{OsStr, OsString};
use std::ffi::OsString;
use std::io::{self, StdoutLock, Write};
use std::iter::Peekable;
use std::ops::ControlFlow;
use std::slice::Iter;
use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, help_about, help_section, help_usage};
use uucore::error::UResult;
use uucore::{format_usage, help_about, help_section, help_usage, os_str_as_bytes_verbose};

const ABOUT: &str = help_about!("echo.md");
const USAGE: &str = help_usage!("echo.md");
Expand Down Expand Up @@ -355,13 +355,9 @@ fn execute(
arguments_after_options: ValuesRef<'_, OsString>,
) -> UResult<()> {
for (i, input) in arguments_after_options.enumerate() {
let Some(bytes) = bytes_from_os_string(input.as_os_str()) else {
return Err(USimpleError::new(
1,
"Non-UTF-8 arguments provided, but this platform does not support them",
));
};
let bytes = os_str_as_bytes_verbose(input)?;

// Don't print a space before the first argument
if i > 0 {
stdout_lock.write_all(b" ")?;
}
Expand All @@ -381,23 +377,3 @@ fn execute(

Ok(())
}

fn bytes_from_os_string(input: &OsStr) -> Option<&[u8]> {
let option = {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;

Some(input.as_bytes())
}

#[cfg(not(target_family = "unix"))]
{
// TODO
// Verify that this works correctly on these platforms
input.to_str().map(|st| st.as_bytes())
}
};

option
}
31 changes: 21 additions & 10 deletions src/uu/printf/src/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

#![allow(dead_code)]

use clap::builder::ValueParser;
use clap::{crate_version, Arg, ArgAction, Command};
use std::ffi::OsString;
use std::io::stdout;
use std::ops::ControlFlow;
use uucore::error::{UResult, UUsageError};
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
use uucore::{format_usage, help_about, help_section, help_usage};
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatError, FormatItem};
use uucore::{format_usage, help_about, help_section, help_usage, os_str_as_bytes_verbose};

const VERSION: &str = "version";
const HELP: &str = "help";
Expand All @@ -28,17 +30,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args);

let format = matches
.get_one::<String>(options::FORMAT)
.get_one::<OsString>(options::FORMAT)
.ok_or_else(|| UUsageError::new(1, "missing operand"))?;

let values: Vec<_> = match matches.get_many::<String>(options::ARGUMENT) {
Some(s) => s.map(|s| FormatArgument::Unparsed(s.to_string())).collect(),
None => vec![],
let format_bytes = os_str_as_bytes_verbose(format).map_err(FormatError::from)?;

let values = match matches.get_many::<OsString>(options::ARGUMENT) {
Some(os_string) => os_string
.map(|os_string_ref| FormatArgument::Unparsed(os_string_ref.to_owned()))
.collect(),
None => Vec::<FormatArgument>::new(),
};

let mut format_seen = false;
let mut args = values.iter().peekable();
for item in parse_spec_and_escape(format.as_ref()) {

for item in parse_spec_and_escape(format_bytes) {
if let Ok(FormatItem::Spec(_)) = item {
format_seen = true;
}
Expand All @@ -55,7 +62,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

while args.peek().is_some() {
for item in parse_spec_and_escape(format.as_ref()) {
for item in parse_spec_and_escape(format_bytes) {
match item?.write(stdout(), &mut args)? {
ControlFlow::Continue(()) => {}
ControlFlow::Break(()) => return Ok(()),
Expand Down Expand Up @@ -86,6 +93,10 @@ pub fn uu_app() -> Command {
.help("Print version information")
.action(ArgAction::Version),
)
.arg(Arg::new(options::FORMAT))
.arg(Arg::new(options::ARGUMENT).action(ArgAction::Append))
.arg(Arg::new(options::FORMAT).value_parser(ValueParser::os_string()))
.arg(
Arg::new(options::ARGUMENT)
.action(ArgAction::Append)
.value_parser(ValueParser::os_string()),
)
}
80 changes: 49 additions & 31 deletions src/uucore/src/lib/features/format/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use super::FormatError;
use crate::{
error::set_exit_code,
features::format::num_parser::{ParseError, ParsedNumber},
os_str_as_bytes_verbose, os_str_as_str_verbose,
quoting_style::{escape_name, Quotes, QuotingStyle},
show_error, show_warning,
};
use os_display::Quotable;
use std::ffi::OsStr;
use std::ffi::{OsStr, OsString};

/// An argument for formatting
///
Expand All @@ -22,71 +24,86 @@ use std::ffi::OsStr;
#[derive(Clone, Debug)]
pub enum FormatArgument {
Char(char),
String(String),
String(OsString),
UnsignedInt(u64),
SignedInt(i64),
Float(f64),
/// Special argument that gets coerced into the other variants
Unparsed(String),
Unparsed(OsString),
}

pub trait ArgumentIter<'a>: Iterator<Item = &'a FormatArgument> {
fn get_char(&mut self) -> u8;
fn get_i64(&mut self) -> i64;
fn get_u64(&mut self) -> u64;
fn get_f64(&mut self) -> f64;
fn get_str(&mut self) -> &'a str;
fn get_char(&mut self) -> Result<u8, FormatError>;
fn get_i64(&mut self) -> Result<i64, FormatError>;
fn get_u64(&mut self) -> Result<u64, FormatError>;
fn get_f64(&mut self) -> Result<f64, FormatError>;
fn get_str(&mut self) -> &'a OsStr;
}

impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
fn get_char(&mut self) -> u8 {
fn get_char(&mut self) -> Result<u8, FormatError> {
let Some(next) = self.next() else {
return b'\0';
return Ok(b'\0');
};
match next {
FormatArgument::Char(c) => *c as u8,
FormatArgument::Unparsed(s) => s.bytes().next().unwrap_or(b'\0'),
_ => b'\0',
FormatArgument::Char(c) => Ok(*c as u8),
FormatArgument::Unparsed(os) => match os_str_as_bytes_verbose(os)?.first() {
Some(&byte) => Ok(byte),
None => Ok(b'\0'),
},
_ => Ok(b'\0'),
}
}

fn get_u64(&mut self) -> u64 {
fn get_u64(&mut self) -> Result<u64, FormatError> {
let Some(next) = self.next() else {
return 0;
return Ok(0);
};
match next {
FormatArgument::UnsignedInt(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_u64(s), s),
_ => 0,
FormatArgument::UnsignedInt(n) => Ok(*n),
FormatArgument::Unparsed(os) => {
let str = os_str_as_str_verbose(os)?;

Ok(extract_value(ParsedNumber::parse_u64(str), str))
}
_ => Ok(0),
}
}

fn get_i64(&mut self) -> i64 {
fn get_i64(&mut self) -> Result<i64, FormatError> {
let Some(next) = self.next() else {
return 0;
return Ok(0);
};
match next {
FormatArgument::SignedInt(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_i64(s), s),
_ => 0,
FormatArgument::SignedInt(n) => Ok(*n),
FormatArgument::Unparsed(os) => {
let str = os_str_as_str_verbose(os)?;

Ok(extract_value(ParsedNumber::parse_i64(str), str))
}
_ => Ok(0),
}
}

fn get_f64(&mut self) -> f64 {
fn get_f64(&mut self) -> Result<f64, FormatError> {
let Some(next) = self.next() else {
return 0.0;
return Ok(0.0);
};
match next {
FormatArgument::Float(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_f64(s), s),
_ => 0.0,
FormatArgument::Float(n) => Ok(*n),
FormatArgument::Unparsed(os) => {
let str = os_str_as_str_verbose(os)?;

Ok(extract_value(ParsedNumber::parse_f64(str), str))
}
_ => Ok(0.0),
}
}

fn get_str(&mut self) -> &'a str {
fn get_str(&mut self) -> &'a OsStr {
match self.next() {
Some(FormatArgument::Unparsed(s) | FormatArgument::String(s)) => s,
_ => "",
Some(FormatArgument::Unparsed(os) | FormatArgument::String(os)) => os,
_ => "".as_ref(),
}
}
}
Expand Down Expand Up @@ -120,6 +137,7 @@ fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T
} else {
show_error!("{}: value not completely converted", input.quote());
}

v
}
}
Expand Down
36 changes: 28 additions & 8 deletions src/uucore/src/lib/features/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ pub mod num_format;
pub mod num_parser;
mod spec;

pub use argument::*;
pub use argument::FormatArgument;

use self::{
escape::{parse_escape_code, EscapedChar},
num_format::Formatter,
};
use crate::{error::UError, NonUtf8OsStrError, OsStrConversionType};
use spec::Spec;
use std::{
error::Error,
Expand All @@ -46,13 +52,6 @@ use std::{
ops::ControlFlow,
};

use crate::error::UError;

use self::{
escape::{parse_escape_code, EscapedChar},
num_format::Formatter,
};

#[derive(Debug)]
pub enum FormatError {
SpecError(Vec<u8>),
Expand All @@ -63,6 +62,7 @@ pub enum FormatError {
NeedAtLeastOneSpec(Vec<u8>),
WrongSpecType,
InvalidPrecision(String),
InvalidEncoding(NonUtf8OsStrError),
}

impl Error for FormatError {}
Expand All @@ -74,6 +74,12 @@ impl From<std::io::Error> for FormatError {
}
}

impl From<NonUtf8OsStrError> for FormatError {
fn from(value: NonUtf8OsStrError) -> FormatError {
FormatError::InvalidEncoding(value)
}
}

impl Display for FormatError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -98,6 +104,20 @@ impl Display for FormatError {
Self::IoError(_) => write!(f, "io error"),
Self::NoMoreArguments => write!(f, "no more arguments"),
Self::InvalidArgument(_) => write!(f, "invalid argument"),
Self::InvalidEncoding(no) => {
use os_display::Quotable;

let quoted = no.input_lossy_string.quote();

match no.conversion_type {
OsStrConversionType::ToBytes => f.write_fmt(format_args!(
"invalid (non-UTF-8) argument like {quoted} encountered when converting argument to bytes on a platform that doesn't use UTF-8",
)),
OsStrConversionType::ToString => f.write_fmt(format_args!(
"invalid (non-UTF-8) argument like {quoted} encountered",
)),
}
}
}
}
}
Expand Down
Loading
Loading