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

Limit line length to 100 MB to not exhaust system memory on /dev/zero #1972

Closed
wants to merge 1 commit into from
Closed
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Python syntax highlighting no longer suffers from abysmal performance in specific scenarios. See #1688 (@keith-hall)
- First line not shown in diff context. See #1891 (@divagant-martian)
- Do not ignore syntaxes that handle file names with a `*.conf` extension. See #1703 (@cbolgiano)
- Limit line length to 100 MB to not exhaust system memory on `/dev/zero`. See #1972 (@Enselic)

## Performance

Expand Down
16 changes: 14 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub(crate) struct InputReader<'a> {
impl<'a> InputReader<'a> {
fn new<R: BufRead + 'a>(mut reader: R) -> InputReader<'a> {
let mut first_line = vec![];
reader.read_until(b'\n', &mut first_line).ok();
Self::read_line_safely(&mut reader, &mut first_line).ok();
Copy link
Collaborator Author

@Enselic Enselic Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ignore errors here as before, but I don't think that is a problem in practice. Not ignoring the error here is likely to cascade into a larger change with higher risk.


let content_type = if first_line.is_empty() {
None
Expand All @@ -276,14 +276,26 @@ impl<'a> InputReader<'a> {
return Ok(true);
}

let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
let res = Self::read_line_safely(&mut self.inner, buf).map(|size| size > 0)?;

if self.content_type == Some(ContentType::UTF_16LE) {
let _ = self.inner.read_until(0x00, buf);
}

Ok(res)
}

fn read_line_safely<R: BufRead + 'a>(reader: &mut R, buf: &mut Vec<u8>) -> io::Result<usize> {
let limit = 100_000_000_usize;

match reader.take(limit as u64).read_until(b'\n', buf) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: take does not actually take anything, so the name is a bit odd. It just puts an upper limit. See https://doc.rust-lang.org/std/io/trait.Read.html#method.take

Ok(n) if n == limit => Err(io::Error::new(
io::ErrorKind::Other,
"Lines longer than 100 MB are not supported",
)),
r => r,
}
}
}

#[test]
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ fn no_args_doesnt_break() {
assert!(exit_status.success());
}

#[cfg(unix)]
#[test]
fn dev_zero() {
bat()
.arg("/dev/zero")
.assert()
.failure()
.stderr(predicate::str::contains(
"Lines longer than 100 MB are not supported",
));
}

#[test]
fn tabs_numbers() {
bat()
Expand Down