Skip to content
Open
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
38 changes: 36 additions & 2 deletions src/uu/sort/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{

use memchr::memchr_iter;
use self_cell::self_cell;
use uucore::error::{UResult, USimpleError};
use uucore::error::{UResult, USimpleError, strip_errno};

use crate::{
GeneralBigDecimalParseResult, GlobalSettings, Line, SortMode, numeric_str_cmp::NumInfo,
Expand Down Expand Up @@ -427,7 +427,7 @@ fn read_to_buffer<T: Read>(
Err(e) if e.kind() == ErrorKind::Interrupted => {
// retry
}
Err(e) => return Err(USimpleError::new(2, e.to_string())),
Err(e) => return Err(USimpleError::new(2, strip_errno(&e))),
}
}
}
Expand Down Expand Up @@ -460,3 +460,37 @@ pub fn parse_into_chunk<'a>(
line_count_hint,
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Error;

struct FailingReader;

impl Read for FailingReader {
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
Err(Error::from_raw_os_error(5))
}
}

#[test]
fn read_error_message_has_no_errno_suffix() {
let mut buffer = vec![0u8; 64];
let mut next_files = std::iter::empty::<UResult<FailingReader>>();
let err = read_to_buffer(
&mut FailingReader,
&mut next_files,
&mut buffer,
None,
0,
b'\n',
)
.unwrap_err();
let msg = err.to_string();
assert!(!msg.contains("(os error"), "leaked errno: {msg}");
assert_eq!(msg, strip_errno(&Error::from_raw_os_error(5)));
// Sanity: an error without an errno is untouched.
assert_eq!(strip_errno(&Error::other("custom")), "custom");
}
}
Loading