Repository navigation
grep: reject non-POSIX and unclosed [:name:] classes in BRE/ERE - #253
yunusarda-sudo wants to merge 1 commit into
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #253 +/- ##
==========================================
+ Coverage 97.32% 97.38% +0.06%
==========================================
Files 6 6
Lines 2316 2331 +15
Branches 250 250
==========================================
+ Hits 2254 2270 +16
+ Misses 62 61 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
GNU grep testsuite comparison: |
1 similar comment
|
GNU grep testsuite comparison: |
|
GNU grep testsuite comparison: |
| fn has_confusing_bracket(pattern: &[u8]) -> bool { | ||
| fn is_posix_char_class(name: &[u8]) -> bool { | ||
| matches!( | ||
| name, |
There was a problem hiding this comment.
this line is a leftover of the old doc comment, it now sits on is_posix_char_class.
please fix it, and keep the explanation of what a "confusing" bracket is somewhere (on scan_bracket?)
There was a problem hiding this comment.
Fixed: the stray line is gone, and the "confusing bracket" explanation now lives on scan_bracket.
| #[cfg(test)] | ||
| fn has_confusing_bracket(pattern: &[u8]) -> bool { | ||
| check_brackets(pattern) == Err("character class syntax is [[:space:]], not [:space:]") | ||
| } |
There was a problem hiding this comment.
do we need to keep this only for the tests?
could the unit tests call check_brackets directly instead of comparing the message string?
There was a problem hiding this comment.
Done: has_confusing_bracket is removed, the unit tests call check_brackets directly.
| } | ||
| i = next; | ||
| } | ||
| _ => i += 1, |
There was a problem hiding this comment.
this message is now written twice, could it be a const?
There was a problem hiding this comment.
Done: the three messages are consts now.
| @@ -617,32 +614,56 @@ fn has_empty_interval(pattern: &[u8]) -> bool { | |||
| } | |||
There was a problem hiding this comment.
check_brackets runs before has_empty_interval, so this branch can't be reached, no?
probably the line codecov is complaining about
There was a problem hiding this comment.
scan_bracket returns Result<usize, _> now, so the dummy end index on error is gone; has_empty_interval only falls back to pattern.len() for brackets check_brackets already accepted. Say if you meant another branch.
| ("[[:^digit:]]", "Invalid character class name"), | ||
| ("[[::]]", "Invalid character class name"), | ||
| ("[[:digit]]", "Unmatched [, [^, [:, [., or [="), | ||
| ("[x[:]y]", "Unmatched [, [^, [:, [., or [="), |
There was a problem hiding this comment.
please add cases for unclosed [. and [= too, like [[.a] and [[=a]
There was a problem hiding this comment.
[[.a] and [[=a] are in the table in rejects_invalid_character_class_names (for -G and -E); I also added a unit test that calls check_brackets on them.
…uutils#171) GNU grep accepts only the twelve POSIX class names inside a bracket expression. Any other name, such as `[[:word:]]`, `[[:^alpha:]]` or `[[::]]`, fails with `Invalid character class name` and exit code 2. An unclosed `[:`, `[.` or `[=` inside a bracket fails with `Unmatched [, [^, [:, [., or [=`.
7289229 to
fac440d
Compare
|
GNU grep testsuite comparison: |
|
The cygwin failure is the known PTY flake ( |
GNU grep accepts only the twelve POSIX class names inside a bracket expression (
alnum,alpha,blank,cntrl,digit,graph,lower,print,punct,space,upper,xdigit). Any other name, such as[[:word:]],[[:ascii:]], the negated[[:^alpha:]]or the empty[[::]], fails withInvalid character class nameand exit code 2. An unclosed[:,[., or[=inside a bracket expression (like[[:alpha]]or[x[:]y]) fails withUnmatched [, [^, [:, [., or [=.Previously,
scan_bracketonly checked for misspelled[:space:]syntax without validating the class name or reporting unclosed subexpressions, allowing non-POSIX classes to pass through to Oniguruma.This patch:
[:...:]and errors withInvalid character class nameon non-POSIX/negated names.Unmatched [, [^, [:, [., or [=when[:,[., or[=is not properly closed before the end of the bracket.[:space:]diagnostic.Fixes #171
Investigated and verified locally with
cargo fmt --all -- --check,cargo clippy -- -D warnings, andcargo test.