Skip to content

ptx: handle invalid regex arguments gracefully instead of panicking#9825

Merged
ChrisDryden merged 4 commits intouutils:mainfrom
CrazyRoka:ptx-fix-regex-panic
Jan 18, 2026
Merged

ptx: handle invalid regex arguments gracefully instead of panicking#9825
ChrisDryden merged 4 commits intouutils:mainfrom
CrazyRoka:ptx-fix-regex-panic

Conversation

@CrazyRoka
Copy link
Contributor

@CrazyRoka CrazyRoka commented Dec 24, 2025

Description

This PR fixes a panic in ptx when an invalid regular expression is passed to the -W (--word-regexp) argument.

Previously, running:
ptx -W 'bar\\\'
would result in a Rust panic (called Result::unwrap() on an Err value).

This change catches the regex::Error and prints a user-friendly message to stderr.

Testing

  • Added unit tests in tests/by-util/test_ptx.rs to verify:
    • Trailing backslashes (bar\) return exit code 1.
    • Unclosed groups ((wrong) return exit code 1.
    • The stderr contains "invalid regular expression".
    • program succeeds

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/sort/sort-stale-thread-mem. tests/sort/sort-stale-thread-mem is passing on 'main'. Maybe you have to rebase?
Congrats! The gnu test tests/sort/sort-debug-keys is no longer failing!

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/sort/sort-debug-keys is no longer failing!

@CrazyRoka CrazyRoka force-pushed the ptx-fix-regex-panic branch 2 times, most recently from 30a281f to 07c63a1 Compare December 28, 2025 15:04
@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/sort/sort-stale-thread-mem. tests/sort/sort-stale-thread-mem is passing on 'main'. Maybe you have to rebase?

@ChrisDryden
Copy link
Collaborator

ChrisDryden commented Jan 7, 2026

Whats going to be tricky here is that some of the testing to compare the two implementations is going to not work because whats considered invalid to rust's regex does not line up 100% with the GNU regex. Some examples: bar\ and [abc

GNU allows trailing backslashes, but uutils would show a warning for bar\ whereas if theres an unmatched [ it fails completely for gnu ptx but would succeed with an error for the uutils implementation. I think for the time being it would be best to just have this as an open issue to track some of the edge cases and merge the PR's you have with the fixes that pass all of the test cases and then the deviation with GNU can be handled when uutils figures out what it wants to do about the whole regex library situation

@CrazyRoka CrazyRoka force-pushed the ptx-fix-regex-panic branch from 95d53e2 to 13fab7e Compare January 18, 2026 12:59
@github-actions
Copy link

GNU testsuite comparison:

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


/// Try to compile a regex, printing a warning and returning None on failure.
fn try_compile_regex(pattern: &str) -> Option<Regex> {
match Regex::new(pattern) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please consider using .inspect_err().ok() pattern instead of explicit match for more concise error handling

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm provided that feedback previously but then when I was comparing it to the gnu implementation, it seemingly silently succeeds. When you combine the duplicate file pr with this, it's the only PTX gnu test remaining to silently succeeds when passed invalid regex. I'm not sure what would be put in the 'inspect_error'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @ChrisDryden suggested, I removed the logging entirely and just used .ok() to return None silently on failure.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 18, 2026

Merging this PR will degrade performance by 3.26%

❌ 1 regressed benchmark
✅ 281 untouched benchmarks
⏩ 38 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory dd_copy_partial 129.1 KB 133.5 KB -3.26%

Comparing CrazyRoka:ptx-fix-regex-panic (6e81cb7) with main (ca13e33)

Open in CodSpeed

Footnotes

  1. 38 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@github-actions
Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)

@ChrisDryden
Copy link
Collaborator

@sylvestre are there any settings that could make the macOS queue not be as long? Both the ptx prs are just waiting on those

@ChrisDryden ChrisDryden merged commit a0a797d into uutils:main Jan 18, 2026
156 of 157 checks passed
@CrazyRoka CrazyRoka deleted the ptx-fix-regex-panic branch January 18, 2026 20:40
mattsu2020 pushed a commit to mattsu2020/coreutils that referenced this pull request Jan 23, 2026
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.

3 participants