Skip to content

expr: Escape anchor characters within pattern #7842

New issue

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

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

Already on GitHub? Sign in to your account

Merged

Conversation

frendsick
Copy link
Contributor

@frendsick frendsick commented Apr 25, 2025

Escape the regexp start of string anchor characters '^' within pattern definitions. Only keep the start anchor as is if it is at the beginning of the user input pattern.

Changes

  • Extracts a core pattern by removing a single leading ^
  • Escapes any '^' characters inside the pattern body
  • Ensures an asterisk (*) at the beginning of the pattern is escaped to avoid unintended regex behavior
  • Always prepend a start anchor ^ to the core pattern

Testing

  • Test that the start anchor (^) is not escaped if it is at the start of the input pattern
  • Test that start (^) anchors are escaped within the core pattern

fixes #7663

Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

The `rust-onig` bug still exists but parsing carets is fixed within uutils' `expr`
@frendsick frendsick requested a review from sylvestre April 25, 2025 19:06
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/misc/stdbuf (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

@sylvestre sylvestre requested a review from Copilot April 26, 2025 10:45
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the character escaping logic in regexp pattern definitions and reactivates/upgrades tests to verify the new behavior.

  • Core pattern extraction now removes any extra leading '^' and escapes internal '^' characters.
  • Tests have been updated to cover various scenarios including patterns starting with '^', '*' and multiple '^' occurrences.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/by-util/test_expr.rs Updated tests to reflect the new escaping behavior for patterns.
src/uu/expr/src/syntax_tree.rs Reworked regex string building logic to correctly escape internal '^' characters.
Comments suppressed due to low confidence (2)

src/uu/expr/src/syntax_tree.rs:170

  • [nitpick] Consider renaming the variable 'prev' to a more descriptive name like 'previous_char' to improve code clarity.
let mut prev = first.unwrap_or_default();

tests/by-util/test_expr.rs:293

  • [nitpick] Consider adding an inline comment that explains the expected output for patterns starting with '^' to clarify the transformation rules for future maintainers.
new_ucmd!().args(&["^abc", ":", "^^abc"])

@sylvestre
Copy link
Contributor

and it passes:
2025-04-26T10:40:45.1514002Z bre10...

@sylvestre sylvestre merged commit 07501be into uutils:main Apr 27, 2025
70 checks passed
@frendsick frendsick deleted the fix/expr-match-escape-anchor-characters branch April 27, 2025 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expr: fails to match string to regular expression with ^
2 participants