-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basically, unless the -a/--text flag is given, it is generally always an error to search for an explicit NUL byte because the binary detection will prevent it from matching. Fixes #1838
- Loading branch information
1 parent
7bb9f35
commit 9ed7565
Showing
10 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use regex_syntax::hir::{ | ||
self, ClassBytesRange, ClassUnicodeRange, Hir, HirKind, | ||
}; | ||
|
||
use crate::error::{Error, ErrorKind}; | ||
|
||
/// Returns an error when a sub-expression in `expr` must match `byte`. | ||
pub(crate) fn check(expr: &Hir, byte: u8) -> Result<(), Error> { | ||
assert!(byte.is_ascii(), "ban byte must be ASCII"); | ||
let ch = char::from(byte); | ||
let invalid = || Err(Error::new(ErrorKind::Banned(byte))); | ||
match expr.kind() { | ||
HirKind::Empty => {} | ||
HirKind::Literal(hir::Literal(ref lit)) => { | ||
if lit.iter().find(|&&b| b == byte).is_some() { | ||
return invalid(); | ||
} | ||
} | ||
HirKind::Class(hir::Class::Unicode(ref cls)) => { | ||
if cls.ranges().iter().map(|r| r.len()).sum::<usize>() == 1 { | ||
let contains = | ||
|r: &&ClassUnicodeRange| r.start() <= ch && ch <= r.end(); | ||
if cls.ranges().iter().find(contains).is_some() { | ||
return invalid(); | ||
} | ||
} | ||
} | ||
HirKind::Class(hir::Class::Bytes(ref cls)) => { | ||
if cls.ranges().iter().map(|r| r.len()).sum::<usize>() == 1 { | ||
let contains = |r: &&ClassBytesRange| { | ||
r.start() <= byte && byte <= r.end() | ||
}; | ||
if cls.ranges().iter().find(contains).is_some() { | ||
return invalid(); | ||
} | ||
} | ||
} | ||
HirKind::Look(_) => {} | ||
HirKind::Repetition(ref x) => check(&x.sub, byte)?, | ||
HirKind::Capture(ref x) => check(&x.sub, byte)?, | ||
HirKind::Concat(ref xs) => { | ||
for x in xs.iter() { | ||
check(x, byte)?; | ||
} | ||
} | ||
HirKind::Alternation(ref xs) => { | ||
for x in xs.iter() { | ||
check(x, byte)?; | ||
} | ||
} | ||
}; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use regex_syntax::Parser; | ||
|
||
/// Returns true when the given pattern is detected to contain the given | ||
/// banned byte. | ||
fn check(pattern: &str, byte: u8) -> bool { | ||
let hir = Parser::new().parse(pattern).unwrap(); | ||
super::check(&hir, byte).is_err() | ||
} | ||
|
||
#[test] | ||
fn various() { | ||
assert!(check(r"\x00", 0)); | ||
assert!(check(r"a\x00", 0)); | ||
assert!(check(r"\x00b", 0)); | ||
assert!(check(r"a\x00b", 0)); | ||
assert!(check(r"\x00|ab", 0)); | ||
assert!(check(r"ab|\x00", 0)); | ||
assert!(check(r"\x00?", 0)); | ||
assert!(check(r"(\x00)", 0)); | ||
|
||
assert!(check(r"[\x00]", 0)); | ||
assert!(check(r"[^[^\x00]]", 0)); | ||
|
||
assert!(!check(r"[^\x00]", 0)); | ||
assert!(!check(r"[\x00a]", 0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ pub use crate::{ | |
}; | ||
|
||
mod ast; | ||
mod ban; | ||
mod config; | ||
mod error; | ||
mod literal; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters