Skip to content
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
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/jargon.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ makedev
mebi
mebibytes
mergeable
metacharacters
microbenchmark
microbenchmarks
microbenchmarking
Expand Down
24 changes: 16 additions & 8 deletions src/uucore/src/lib/features/format/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use super::{
};
use crate::{
format::FormatArguments,
i18n::UEncoding,
os_str_as_bytes,
quoting_style::{QuotingStyle, locale_aware_escape_name},
quoting_style::{QuotingStyle, escape_name},
};
use std::{io::Write, num::NonZero, ops::ControlFlow};

Expand Down Expand Up @@ -403,10 +404,14 @@ impl Spec {
writer.write_all(&parsed).map_err(FormatError::IoError)
}
Self::QuotedString { position } => {
let s = locale_aware_escape_name(
args.next_string(*position),
QuotingStyle::SHELL_ESCAPE,
);
// printf %q uses committed dollar mode (entire string in $'...' when control chars present)
let printf_style = QuotingStyle::Shell {
escape: true,
always_quote: false,
show_control: false,
commit_dollar_mode: true, // printf %q style
};
let s = escape_name(args.next_string(*position), printf_style, UEncoding::Utf8);
let bytes = os_str_as_bytes(&s)?;
writer.write_all(bytes).map_err(FormatError::IoError)
}
Expand Down Expand Up @@ -595,10 +600,13 @@ fn eat_number(rest: &mut &[u8], index: &mut usize) -> Option<usize> {
match rest[*index..].iter().position(|b| !b.is_ascii_digit()) {
None | Some(0) => None,
Some(i) => {
// Handle large numbers that would cause overflow
let num_str = std::str::from_utf8(&rest[*index..(*index + i)]).unwrap();
// Handle potential overflow when parsing large numbers
let parsed = std::str::from_utf8(&rest[*index..(*index + i)])
.unwrap()
.parse()
.ok()?;
*index += i;
Some(num_str.parse().unwrap_or(usize::MAX))
Some(parsed)
}
}
}
Expand Down
69 changes: 33 additions & 36 deletions src/uucore/src/lib/features/quoting_style/escaped_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ pub enum EscapeState {
}

/// Bytes we need to present as escaped octal, in the form of `\nnn` per byte.
/// Only supports characters up to 2 bytes long in UTF-8.
/// Supports characters up to 4 bytes long in UTF-8.
pub struct EscapeOctal {
c: [u8; 2],
bytes: [u8; 4],
num_bytes: usize,
byte_idx: usize,
digit_idx: u8,
state: EscapeOctalState,
idx: u8,
}

enum EscapeOctalState {
Done,
FirstBackslash,
FirstValue,
LastBackslash,
LastValue,
Backslash,
Value,
}

fn byte_to_octal_digit(byte: u8, idx: u8) -> u8 {
Expand All @@ -51,30 +51,23 @@ impl Iterator for EscapeOctal {
fn next(&mut self) -> Option<char> {
match self.state {
EscapeOctalState::Done => None,
EscapeOctalState::FirstBackslash => {
self.state = EscapeOctalState::FirstValue;
EscapeOctalState::Backslash => {
self.state = EscapeOctalState::Value;
Some('\\')
}
EscapeOctalState::LastBackslash => {
self.state = EscapeOctalState::LastValue;
Some('\\')
}
EscapeOctalState::FirstValue => {
let octal_digit = byte_to_octal_digit(self.c[0], self.idx);
if self.idx == 0 {
self.state = EscapeOctalState::LastBackslash;
self.idx = 2;
} else {
self.idx -= 1;
}
Some(from_digit(octal_digit.into(), 8).unwrap())
}
EscapeOctalState::LastValue => {
let octal_digit = byte_to_octal_digit(self.c[1], self.idx);
if self.idx == 0 {
self.state = EscapeOctalState::Done;
EscapeOctalState::Value => {
let octal_digit = byte_to_octal_digit(self.bytes[self.byte_idx], self.digit_idx);
if self.digit_idx == 0 {
// Move to next byte
self.byte_idx += 1;
if self.byte_idx >= self.num_bytes {
self.state = EscapeOctalState::Done;
} else {
self.state = EscapeOctalState::Backslash;
self.digit_idx = 2;
}
} else {
self.idx -= 1;
self.digit_idx -= 1;
}
Some(from_digit(octal_digit.into(), 8).unwrap())
}
Expand All @@ -88,20 +81,24 @@ impl EscapeOctal {
return Self::from_byte(c as u8);
}

let mut buf = [0; 2];
let _s = c.encode_utf8(&mut buf);
let mut bytes = [0; 4];
let len = c.encode_utf8(&mut bytes).len();
Self {
c: buf,
idx: 2,
state: EscapeOctalState::FirstBackslash,
bytes,
num_bytes: len,
byte_idx: 0,
digit_idx: 2,
state: EscapeOctalState::Backslash,
}
}

fn from_byte(b: u8) -> Self {
Self {
c: [0, b],
idx: 2,
state: EscapeOctalState::LastBackslash,
bytes: [b, 0, 0, 0],
num_bytes: 1,
byte_idx: 0,
digit_idx: 2,
state: EscapeOctalState::Backslash,
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/uucore/src/lib/features/quoting_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub enum QuotingStyle {

/// Whether to show control and non-unicode characters, or replace them with `?`.
show_control: bool,

/// Whether to commit to dollar quoting for the entire string (printf %q style).
/// true: committed mode - wrap entire string in $'...' when control chars present
/// false: selective mode (ls style) - only wrap individual control chars in $'...'
commit_dollar_mode: bool,
},

/// Escape the name as a C string.
Expand All @@ -58,24 +63,28 @@ impl QuotingStyle {
escape: false,
always_quote: false,
show_control: false,
commit_dollar_mode: false, // ls style - selective dollar mode
};

pub const SHELL_ESCAPE: Self = Self::Shell {
escape: true,
always_quote: false,
show_control: false,
commit_dollar_mode: false, // ls style - selective dollar mode
};

pub const SHELL_QUOTE: Self = Self::Shell {
escape: false,
always_quote: true,
show_control: false,
commit_dollar_mode: false, // ls style - selective dollar mode
};

pub const SHELL_ESCAPE_QUOTE: Self = Self::Shell {
escape: true,
always_quote: true,
show_control: false,
commit_dollar_mode: false, // ls style - selective dollar mode
};

pub const C_NO_QUOTES: Self = Self::C {
Expand All @@ -94,11 +103,13 @@ impl QuotingStyle {
Shell {
escape,
always_quote,
commit_dollar_mode,
..
} => Shell {
escape,
always_quote,
show_control,
commit_dollar_mode,
},
Literal { .. } => Literal { show_control },
C { .. } => self,
Expand Down Expand Up @@ -161,17 +172,20 @@ fn escape_name_inner(
QuotingStyle::Shell {
escape: true,
always_quote,
commit_dollar_mode,
..
} => Box::new(EscapedShellQuoter::new(
name,
always_quote,
dirname,
commit_dollar_mode,
name.len(),
)),
QuotingStyle::Shell {
escape: false,
always_quote,
show_control,
..
} => Box::new(NonEscapedShellQuoter::new(
name,
show_control,
Expand Down Expand Up @@ -235,6 +249,7 @@ impl fmt::Display for QuotingStyle {
escape,
always_quote,
show_control,
..
} => {
let mut style = "shell".to_string();
if escape {
Expand Down Expand Up @@ -761,7 +776,9 @@ mod tests {
],
);

// mixed with valid characters
// mixed with valid characters (invalid byte 0xA7 followed by underscore)
// The correct output for shell-escape should be: ''$'\247''_'
// (empty string, ANSI-C quote the invalid byte, then quote the underscore)
check_names_raw_both(
&[continuation, ascii],
&[
Expand Down
Loading
Loading