From 3c9121c85b6b0d29d425a1715ffd335ce0d52873 Mon Sep 17 00:00:00 2001 From: yor1xd Date: Thu, 21 May 2026 18:46:09 -0300 Subject: [PATCH] fmt: propagate read errors instead of silently treating them as EOF --- src/uu/fmt/locales/en-US.ftl | 1 + src/uu/fmt/locales/fr-FR.ftl | 1 + src/uu/fmt/src/fmt.rs | 8 +- src/uu/fmt/src/parasplit.rs | 158 ++++++++++++++++------------------- tests/by-util/test_fmt.rs | 25 ++++++ 5 files changed, 107 insertions(+), 86 deletions(-) diff --git a/src/uu/fmt/locales/en-US.ftl b/src/uu/fmt/locales/en-US.ftl index 222540c29f5..a65fd392411 100644 --- a/src/uu/fmt/locales/en-US.ftl +++ b/src/uu/fmt/locales/en-US.ftl @@ -30,3 +30,4 @@ fmt-error-invalid-width-malformed = invalid width: {$width} fmt-error-cannot-open-for-reading = cannot open {$file} for reading fmt-error-cannot-get-metadata = cannot get metadata for {$file} fmt-error-failed-to-write-output = failed to write output +fmt-error-read = error reading {$file} diff --git a/src/uu/fmt/locales/fr-FR.ftl b/src/uu/fmt/locales/fr-FR.ftl index bd19b929926..7b4763011aa 100644 --- a/src/uu/fmt/locales/fr-FR.ftl +++ b/src/uu/fmt/locales/fr-FR.ftl @@ -30,3 +30,4 @@ fmt-error-invalid-width-malformed = largeur invalide : {$width} fmt-error-cannot-open-for-reading = impossible d'ouvrir {$file} en lecture fmt-error-cannot-get-metadata = impossible d'obtenir les métadonnées pour {$file} fmt-error-failed-to-write-output = échec de l'écriture de sortie +fmt-error-read = erreur de lecture de {$file} diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index 0bae1560c38..13e9542ad15 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -240,7 +240,13 @@ fn process_file( let p_stream = ParagraphStream::new(fmt_opts, &mut fp); for para_result in p_stream { match para_result { - Err(nfl) => { + Err(parasplit::ParagraphStreamError::IoError(e)) => { + show!(e.map_err_context( + || translate!("fmt-error-read", "file" => Path::new(file_name).quote()) + )); + break; + } + Err(parasplit::ParagraphStreamError::NoFormatLine(nfl)) => { ostream .write_all(&nfl.line) .map_err_context(|| translate!("fmt-error-failed-to-write-output"))?; diff --git a/src/uu/fmt/src/parasplit.rs b/src/uu/fmt/src/parasplit.rs index 77706fb14ea..d8fcb112d5c 100644 --- a/src/uu/fmt/src/parasplit.rs +++ b/src/uu/fmt/src/parasplit.rs @@ -159,24 +159,6 @@ pub struct NoFormatLine { pub newline: bool, } -impl Line { - /// when we know that it's a [`Line::FormatLine`], as in the [`ParagraphStream`] iterator - fn get_formatline(self) -> FileLine { - match self { - Self::FormatLine(fl) => fl, - Self::NoFormatLine(..) => panic!("Found NoFormatLine when expecting FormatLine"), - } - } - - /// when we know that it's a [`Line::NoFormatLine`], as in the [`ParagraphStream`] iterator - fn get_noformatline(self) -> NoFormatLine { - match self { - Self::NoFormatLine(nfl) => nfl, - Self::FormatLine(..) => panic!("Found FormatLine when expecting NoFormatLine"), - } - } -} - /// Each line's prefix has to be considered to know whether to merge it with /// the next line or not #[derive(Debug)] @@ -274,14 +256,19 @@ impl FileLines<'_> { } impl Iterator for FileLines<'_> { - type Item = Line; + type Item = Result; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { let mut buf = Vec::new(); - self.reader - .read_until(b'\n', &mut buf) - .ok() - .filter(|n| *n > 0)?; + + // Distinguish EOF from an actual read error. + match self.reader.read_until(b'\n', &mut buf) { + Ok(0) => return None, + Ok(_) => {} + Err(e) => return Some(Err(e)), + } + + // Preserve newline information from main. let has_newline = buf.ends_with(b"\n"); if has_newline { buf.pop(); @@ -289,70 +276,61 @@ impl Iterator for FileLines<'_> { buf.pop(); } } + let n = buf; - // if this line is entirely whitespace, - // emit a blank line - // Err(true) indicates that this was a linebreak, - // which is important to know when detecting mail headers + // Empty or whitespace-only line. if n.iter().all(|&b| is_fmt_whitespace_byte(b)) { - return Some(Line::NoFormatLine(NoFormatLine { + return Some(Ok(Line::NoFormatLine(NoFormatLine { line: Vec::new(), blank: true, newline: true, - })); + }))); } let (pmatch, poffset) = self.match_prefix(&n[..]); - // if this line does not match the prefix, - // emit the line unprocessed (even when it lacks a final newline) - // and iterate again + // Lines without the requested prefix pass through. if !pmatch { - return Some(Line::NoFormatLine(NoFormatLine { + return Some(Ok(Line::NoFormatLine(NoFormatLine { line: n, blank: false, newline: has_newline, - })); + }))); } - // if the line matches the prefix, but is blank after, - // don't allow lines to be combined through it (that is, - // treat it like a blank line, except that since it's - // not truly blank we will not allow mail headers on the - // following line) + // Lines containing only a prefix and whitespace. if n[poffset + self.opts.prefix.as_ref().map_or(0, String::len)..] .iter() .all(|&b| is_fmt_whitespace_byte(b)) { - return Some(Line::NoFormatLine(NoFormatLine { + return Some(Ok(Line::NoFormatLine(NoFormatLine { line: n, blank: false, newline: true, - })); + }))); } - // skip if this line matches the anti_prefix - // (NOTE definition of match_anti_prefix is TRUE if we should process) + // Lines excluded by the anti-prefix pass through. if !self.match_anti_prefix(&n[..]) { - return Some(Line::NoFormatLine(NoFormatLine { + return Some(Ok(Line::NoFormatLine(NoFormatLine { line: n, blank: false, newline: has_newline, - })); + }))); } - // figure out the indent, prefix, and prefixindent ending points let prefix_end = poffset + self.opts.prefix.as_ref().map_or(0, String::len); + let (indent_end, prefix_len, indent_len) = self.compute_indent(&n[..], prefix_end); - Some(Line::FormatLine(FileLine { + Some(Ok(Line::FormatLine(FileLine { line: n, indent_end, prefix_indent_end: poffset, indent_len, prefix_len, - })) + }))) } } @@ -429,25 +407,39 @@ impl ParagraphStream<'_> { } } +pub enum ParagraphStreamError { + /// A line that should be passed through unformatted + NoFormatLine(NoFormatLine), + /// An IO error encountered while reading + IoError(std::io::Error), +} + impl Iterator for ParagraphStream<'_> { - type Item = Result; + type Item = Result; #[allow(clippy::cognitive_complexity)] - fn next(&mut self) -> Option> { - // return a NoFormatLine in an Err; it should immediately be output - let noformat = match self.lines.peek()? { - Line::FormatLine(_) => false, - Line::NoFormatLine(_) => true, - }; + fn next(&mut self) -> Option { + // Return unformatted lines directly, but propagate read errors. + match self.lines.peek()? { + Err(_) => { + // peek() confirmed the next item is a read error. + let Some(Err(e)) = self.lines.next() else { + unreachable!("peeked read error must still be available"); + }; + return Some(Err(ParagraphStreamError::IoError(e))); + } + Ok(Line::FormatLine(_)) => {} + Ok(Line::NoFormatLine(_)) => { + let Some(Ok(Line::NoFormatLine(nfl))) = self.lines.next() else { + unreachable!("peeked unformatted line must still be available"); + }; - // found a NoFormatLine, immediately dump it out - if noformat { - let nfl = self.lines.next().unwrap().get_noformatline(); - self.next_mail = nfl.blank; - return Some(Err(nfl)); + self.next_mail = nfl.blank; + return Some(Err(ParagraphStreamError::NoFormatLine(nfl))); + } } - // found a FormatLine, now build a paragraph + // Found a FormatLine; build a paragraph. let mut init_str = Vec::new(); let mut init_end = 0; let mut init_len = 0; @@ -459,16 +451,14 @@ impl Iterator for ParagraphStream<'_> { let mut p_lines = Vec::new(); let mut in_mail = false; - let mut second_done = false; // for when we use crown or tagged mode - while let Some(Line::FormatLine(fl)) = self.lines.peek() { + let mut second_done = false; // for crown or tagged mode + + while let Some(Ok(Line::FormatLine(fl))) = self.lines.peek() { if p_lines.is_empty() { - // first time through the loop, get things set up - // detect mail header + // First time through the loop: set up the paragraph. if self.opts.mail && self.next_mail && ParagraphStream::is_mail_header(fl) { in_mail = true; - // there can't be any indent or prefixindent because otherwise is_mail_header - // would fail since there cannot be any whitespace before the colon in a - // valid header field + // Mail headers have no indent or prefix indent. indent_str.extend_from_slice(b" "); indent_len = 2; } else { @@ -480,14 +470,11 @@ impl Iterator for ParagraphStream<'_> { second_done = true; } - // these will be overwritten in the 2nd line of crown or tagged mode, but - // we are not guaranteed to get to the 2nd line, e.g., if the next line - // is a NoFormatLine or None. Thus, we set sane defaults the 1st time around + // Initialize indentation from the first line. indent_str.extend_from_slice(&fl.line[..fl.indent_end]); indent_len = fl.indent_len; indent_end = fl.indent_end; - // save these to check for matching lines prefix_len = fl.prefix_len; prefix_indent_end = fl.prefix_indent_end; @@ -508,14 +495,12 @@ impl Iterator for ParagraphStream<'_> { } } } else if in_mail { - // lines following mail headers must begin with spaces + // Lines following mail headers must begin with spaces. if fl.indent_end == 0 || (self.opts.prefix.is_some() && fl.prefix_indent_end == 0) { - break; // this line does not begin with spaces + break; } } else if !second_done { - // now we have enough info to handle crown margin and tagged mode - - // in both crown and tagged modes we require that prefix_len is the same + // In crown and tagged modes, prefix information must match. if prefix_len != fl.prefix_len || prefix_indent_end != fl.prefix_indent_end { break; } @@ -525,7 +510,7 @@ impl Iterator for ParagraphStream<'_> { break; } - // this is part of the same paragraph, get the indent info from this line + // Use the second line's indentation. indent_str.clear(); indent_str.extend_from_slice(&fl.line[..fl.indent_end]); indent_len = fl.indent_len; @@ -533,7 +518,7 @@ impl Iterator for ParagraphStream<'_> { second_done = true; } else { - // detect mismatch + // A mismatch indicates the start of another paragraph. if indent_end != fl.indent_end || prefix_indent_end != fl.prefix_indent_end || indent_len != fl.indent_len @@ -543,17 +528,20 @@ impl Iterator for ParagraphStream<'_> { } } - p_lines.push(self.lines.next().unwrap().get_formatline().line); + // peek() confirmed that this is a formatted line. + let Some(Ok(Line::FormatLine(fl))) = self.lines.next() else { + unreachable!("peeked formatted line must still be available"); + }; + + p_lines.push(fl.line); - // when we're in split-only mode, we never join lines, so stop here + // In split-only mode, never join lines. if self.opts.split_only { break; } } - // if this was a mail header, then the next line can be detected as one. Otherwise, it cannot. - // NOTE next_mail is true at ParagraphStream instantiation, and is set to true after a blank - // NoFormatLine. + // Allow another mail header only after a mail header paragraph. self.next_mail = in_mail; Some(Ok(Paragraph { diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index 339ce184fcb..7cb465158c0 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -290,6 +290,31 @@ fn test_fmt_continues_after_missing_file() { .stderr_is("fmt: cannot open 'non-existing' for reading: No such file or directory\n"); } +#[test] +#[cfg(target_os = "linux")] +#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] +fn test_fmt_read_error() { + // Reading /proc/self/mem from offset 0 fails with EIO. + // fmt must propagate the read error instead of treating it as EOF. + new_ucmd!() + .arg("/proc/self/mem") + .fails_with_code(1) + .stderr_contains("fmt: error reading '/proc/self/mem'"); +} + +#[test] +#[cfg(target_os = "linux")] +#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] +fn test_fmt_continues_after_read_error() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("valid.txt", "one\ntwo\n"); + + ucmd.args(&["/proc/self/mem", "valid.txt"]) + .fails_with_code(1) + .stdout_is("one two\n") + .stderr_contains("fmt: error reading '/proc/self/mem'"); +} + #[test] fn test_fmt_invalid_goal() { for param in ["-g", "--goal"] {