Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Limit line length ci test #71

Closed
wants to merge 2 commits 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
16 changes: 12 additions & 4 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::limited_read_until(&mut reader, b'\n', &mut first_line).ok();

let content_type = if first_line.is_empty() {
None
Expand All @@ -260,7 +260,7 @@ impl<'a> InputReader<'a> {
};

if content_type == Some(ContentType::UTF_16LE) {
reader.read_until(0x00, &mut first_line).ok();
Self::limited_read_until(&mut reader, 0x00, &mut first_line).ok();
}

InputReader {
Expand All @@ -276,14 +276,22 @@ impl<'a> InputReader<'a> {
return Ok(true);
}

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

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

Ok(res)
}

fn limited_read_until<R: BufRead + 'a>(
reader: &mut R,
delim: u8,
buf: &mut Vec<u8>,
) -> io::Result<usize> {
crate::read_until_with_limit::read_until_with_limit(reader, delim, buf, 100_000)
}
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(crate) mod paging;
mod preprocessor;
mod pretty_printer;
pub(crate) mod printer;
mod read_until_with_limit;
pub mod style;
pub(crate) mod syntax_mapping;
mod terminal;
Expand Down
75 changes: 75 additions & 0 deletions src/read_until_with_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! This file is based on code from
//! <https://github.com/rust-lang/rust/blob/1.57.0/library/std/src/io/mod.rs>
//! licensed under <https://github.com/rust-lang/rust/blob/1.57.0/LICENSE-MIT>
//! which says:
//!
//! ```txt
//! Permission is hereby granted, free of charge, to any
//! person obtaining a copy of this software and associated
//! documentation files (the "Software"), to deal in the
//! Software without restriction, including without
//! limitation the rights to use, copy, modify, merge,
//! publish, distribute, sublicense, and/or sell copies of
//! the Software, and to permit persons to whom the Software
//! is furnished to do so, subject to the following
//! conditions:
//!
//! The above copyright notice and this permission notice
//! shall be included in all copies or substantial portions
//! of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
//! ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//! TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
//! PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
//! SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
//! CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
//! OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
//! IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//! DEALINGS IN THE SOFTWARE.
//! ```
//!
//! To see the changes that has been made to the file you can run `git diff
//! 766cfe1 -- src/read_until_with_limit.rs`.

use std::io::{BufRead, Error, ErrorKind, Result};

pub(crate) fn read_until_with_limit<R: BufRead + ?Sized>(
r: &mut R,
delim: u8,
buf: &mut Vec<u8>,
limit: usize,
) -> Result<usize> {
let mut read = 0;
loop {
let (done, used) = {
let available = match r.fill_buf() {
Ok(n) => n,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
match available.iter().position(|b| *b == delim) {
Some(i) => {
buf.extend_from_slice(&available[..=i]);
(true, i + 1)
}
None => {
buf.extend_from_slice(available);
(false, available.len())
}
}
};
r.consume(used);
read += used;
if read > limit {
let bat_error = crate::error::Error::Msg(format!(
"Lines longer than {} bytes are not supported. Try auto-formatting your file first.",
limit
));
return Err(Error::new(ErrorKind::Other, bat_error));
}
if done || used == 0 {
return Ok(read);
}
}
}
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 100000 bytes are not supported. Try auto-formatting your file first",
));
}

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