mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-25 17:21:57 -07:00
impl: fix --multiline anchored match bug
This fixes a bug where using \A or (?-m)^ in combination with -U/--multiline would permit matches that aren't anchored to the beginning of the file. The underlying cause was an optimization that occurred when mmaps couldn't be used. Namely, ripgrep tries to still read the input incrementally if it knows the pattern can't match through a new line. But the detection logic was flawed, since it didn't account for line anchors. This commit fixes that. Fixes #1878, Fixes #1879
This commit is contained in:
@@ -882,3 +882,26 @@ test:3:5:foo quux
|
||||
";
|
||||
eqnice!(expected, cmd.stdout());
|
||||
});
|
||||
|
||||
rgtest!(r1878, |dir: Dir, _: TestCommand| {
|
||||
dir.create("test", "a\nbaz\nabc\n");
|
||||
|
||||
// Since ripgrep enables (?m) by default, '^' will match at the beginning
|
||||
// of a line, even when -U/--multiline is used.
|
||||
let args = &["-U", "--no-mmap", r"^baz", "test"];
|
||||
eqnice!("baz\n", dir.command().args(args).stdout());
|
||||
let args = &["-U", "--mmap", r"^baz", "test"];
|
||||
eqnice!("baz\n", dir.command().args(args).stdout());
|
||||
|
||||
// But when (?-m) is disabled, or when \A is used, then there should be no
|
||||
// matches that aren't anchored to the beginning of the file.
|
||||
let args = &["-U", "--no-mmap", r"(?-m)^baz", "test"];
|
||||
dir.command().args(args).assert_err();
|
||||
let args = &["-U", "--mmap", r"(?-m)^baz", "test"];
|
||||
dir.command().args(args).assert_err();
|
||||
|
||||
let args = &["-U", "--no-mmap", r"\Abaz", "test"];
|
||||
dir.command().args(args).assert_err();
|
||||
let args = &["-U", "--mmap", r"\Abaz", "test"];
|
||||
dir.command().args(args).assert_err();
|
||||
});
|
||||
|
Reference in New Issue
Block a user