From e7829c05d3b9234f053e7d480f7e75028831d772 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 19 Apr 2019 07:10:24 -0400 Subject: cli: fix bug where last byte was stripped In an effort to strip line terminators, we assumed their existence. But a pattern file may not end with a line terminator, so we shouldn't unconditionally strip them. We fix this by moving to bstr's line handling, which does this for us automatically. --- CHANGELOG.md | 10 ++++++++++ grep-cli/src/pattern.rs | 28 ++++++++++++---------------- tests/regression.rs | 11 +++++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27213652..51fe7fa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +TBD +=== +TODO. + +Bug fixes: + +* [BUG #1259](https://github.com/BurntSushi/ripgrep/issues/1259): + Fix bug where the last byte of a `-f file` was stripped if it wasn't a `\n`. + + 11.0.1 (2019-04-16) =================== ripgrep 11.0.1 is a new patch release that fixes a search regression introduced diff --git a/grep-cli/src/pattern.rs b/grep-cli/src/pattern.rs index dbdb1d8f..2855857e 100644 --- a/grep-cli/src/pattern.rs +++ b/grep-cli/src/pattern.rs @@ -2,10 +2,12 @@ use std::error; use std::ffi::OsStr; use std::fmt; use std::fs::File; -use std::io::{self, BufRead}; +use std::io; use std::path::Path; use std::str; +use bstr::io::BufReadExt; + use escape::{escape, escape_os}; /// An error that occurs when a pattern could not be converted to valid UTF-8. @@ -156,28 +158,22 @@ pub fn patterns_from_stdin() -> io::Result> { /// ``` pub fn patterns_from_reader(rdr: R) -> io::Result> { let mut patterns = vec![]; - let mut bufrdr = io::BufReader::new(rdr); - let mut line = vec![]; let mut line_number = 0; - while { - line.clear(); + io::BufReader::new(rdr).for_byte_line(|line| { line_number += 1; - bufrdr.read_until(b'\n', &mut line)? > 0 - } { - line.pop().unwrap(); // remove trailing '\n' - if line.last() == Some(&b'\r') { - line.pop().unwrap(); - } - match pattern_from_bytes(&line) { - Ok(pattern) => patterns.push(pattern.to_string()), + match pattern_from_bytes(line.as_bytes()) { + Ok(pattern) => { + patterns.push(pattern.to_string()); + Ok(true) + } Err(err) => { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::Other, format!("{}: {}", line_number, err), - )); + )) } } - } + })?; Ok(patterns) } diff --git a/tests/regression.rs b/tests/regression.rs index 76a7b0b6..40a84654 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -705,3 +705,14 @@ rgtest!(r1203_reverse_suffix_literal, |dir: Dir, _: TestCommand| { let mut cmd = dir.command(); eqnice!("153.230000\n", cmd.arg(r"\d\d\d000").arg("test").stdout()); }); + +// See: https://github.com/BurntSushi/ripgrep/issues/1259 +rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| { + dir.create("patterns-nonl", "[foo]"); + dir.create("patterns-nl", "[foo]\n"); + dir.create("test", "fz"); + + eqnice!("fz\n", cmd.arg("-f").arg("patterns-nonl").arg("test").stdout()); + cmd = dir.command(); + eqnice!("fz\n", cmd.arg("-f").arg("patterns-nl").arg("test").stdout()); +}); -- cgit v1.2.3