Skip to content

lexer: don't end a regex at a / inside a bracket expression - #207

Open
Harp404 wants to merge 4 commits into
uutils:mainfrom
Harp404:fix-slash-in-regex-bracket
Open

Harp404 wants to merge 4 commits into
uutils:mainfrom
Harp404:fix-slash-in-regex-bracket

Conversation

@Harp404

@Harp404 Harp404 commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #173.

Inside a bracket expression, / is an ordinary character, so /a[/]b/ matches a/b. The lexer ended the regex at the first / it found, including one inside [...], and then failed to parse the rest of the line.

parse_content now tracks whether a regex is inside a bracket expression and skips the delimiter there. It follows the same rules as gawk's scanner:

  • A ] right after [ or [^ is part of the list, as in []/] or [^]/].
  • A character class such as [:alpha:] inside the brackets doesn't close them, so [[:alpha:]/] works.
  • [. and [= don't nest. gawk rejects /a[[=a=]=/]b/ and so does this.
  • Escapes are handled as before, so [\/] and \/ still work.

String literals are unchanged and still use memchr3.

I compared 15 programs with gawk, covering the cases above, division (4 / 2, 6 / 2 / 3) and unterminated regexes, and all of them match.

One existing test changed. invalid_regex_literal_is_fatal expected /[q/ to reach the regex engine and fail at run time with "unbalanced [". Since the / inside the open [ no longer ends the regex, /[q/ is now an unterminated regex at parse time with exit status 1, which is what gawk reports ("unterminated regexp", exit 1). I moved those two cases into the new test with that expectation and kept the ( cases.

The new test slash_in_regex_bracket fails on main and passes with this change. cargo test --workspace passes (273 tests), and cargo fmt and cargo clippy --workspace --all-targets -- -D warnings are clean. The issue's suggested test uses sub(), which isn't implemented yet, so the test uses ~ and a bare $0 ~ /[/]/ pattern instead.

I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.

Inside [...] a / is an ordinary character, so /a[/]b/ matches a/b.
Track bracket expressions while scanning a regex literal, with the same
rules as gawk's scanner. Fixes uutils#173.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 14:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 122 / FAILED: 446 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 121 / FAILED: 447 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + regexpbrack

Comment thread lexer/src/lib.rs Outdated
// A character class such as `[:alpha:]`. Like gawk, only
// `[:` nests; `[.` and `[=` do not.
if rest.get(i + 1) == Some(&b':')
&& let Some(end) = rest[i + 2..].windows(2).position(|w| w == b":]")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this search for :] can run past a newline (/[[:x/ then a :] on a later line), so we skip the newline and keep lexing instead of reporting an unterminated regex
could you please stop it at \n? and add a test :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, fixed in 3478c03. The :] search now stops at the first newline, so the newline ends the regex as usual.

The new test regex_class_name_ends_at_newline uses /[[:x/ { print 1 } followed by /:]]/ { print 2 } on the next line. Before the fix, those two lines were lexed as one regex, which then failed with "invalid character class name" (exit 2). Now it is "Unterminated regex" (exit 1), like gawk's "unterminated regexp". (With just /[[:x/ and a :] inside a later string, the bracket never closes, so it was already unterminated before the fix and the test would not have caught it.)

Copilot AI balanced review requested due to automatic review settings December 1, 2026 02:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

…acket

# Conflicts:
#	tests/by-util/test_awk.rs
Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 122 / FAILED: 446 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 121 / FAILED: 447 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + regexpbrack

Copilot AI balanced review requested due to automatic review settings December 1, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 122 / FAILED: 446 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 121 / FAILED: 447 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + regexpbrack

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.

awk: a / inside a bracket expression ends the regex literal

3 participants