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
21 changes: 15 additions & 6 deletions src/uu/od/src/input_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ impl<I> InputDecoder<'_, I> {
normal_length: usize,
peek_length: usize,
byte_order: ByteOrder,
) -> InputDecoder<'_, I> {
let bytes = vec![0; normal_length + peek_length];
) -> io::Result<InputDecoder<'_, I>> {
// A huge `--width` would otherwise abort the process while allocating
// the line buffer. Compute the size with a checked add and reserve it
// fallibly so an out-of-range width yields a clean error instead.
let size = normal_length
.checked_add(peek_length)
.ok_or_else(|| io::Error::new(io::ErrorKind::OutOfMemory, "memory exhausted"))?;
let mut data: Vec<u8> = Vec::new();
data.try_reserve_exact(size)
.map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, "memory exhausted"))?;
data.resize(size, 0);

InputDecoder {
Ok(InputDecoder {
input,
data: bytes,
data,
reserved_peek_length: peek_length,
used_normal_length: 0,
used_peek_length: 0,
byte_order,
}
})
}
}

Expand Down Expand Up @@ -235,7 +244,7 @@ mod tests {
fn smoke_test() {
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xff, 0xff];
let mut input = PeekReader::new(Cursor::new(&data));
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little);
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little).unwrap();

// Peek normal length
let mut mem = sut.peek_read().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
od_options.line_bytes,
PEEK_BUFFER_SIZE,
od_options.byte_order,
);
)
.map_err(|e| USimpleError::new(1, e.to_string()))?;

let output_info = OutputInfo::new(
od_options.line_bytes,
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,26 @@ fn test_non_numeric_width_argument() {
.stderr_only("od: invalid -w argument 'w'\n");
}

#[test]
fn test_huge_width_argument() {
// A huge width must produce a clean error instead of aborting the process
// while allocating the line buffer. Regression test for issue #12839.
new_ucmd!()
.arg("-w1162652562662412622")
.arg("-An")
.pipe_in("")
.fails_with_code(1)
.stderr_contains("memory exhausted");

// A width that does not even fit in `usize` is rejected as too large.
new_ucmd!()
.arg("-w99999999999999999999999")
.arg("-An")
.pipe_in("")
.fails_with_code(1)
.stderr_contains("too large");
}

#[test]
fn test_width_without_value() {
let input: [u8; 40] = [0; 40];
Expand Down
Loading