Skip to content

Fix #1794: Regexp/NotRegexp no longer panic on invalid regex#1857

Open
cobyfrombrooklyn-bot wants to merge 1 commit intostretchr:masterfrom
cobyfrombrooklyn-bot:fix-issue-1794
Open

Fix #1794: Regexp/NotRegexp no longer panic on invalid regex#1857
cobyfrombrooklyn-bot wants to merge 1 commit intostretchr:masterfrom
cobyfrombrooklyn-bot:fix-issue-1794

Conversation

@cobyfrombrooklyn-bot
Copy link

Fixes #1794

Problem

assert.Regexp and assert.NotRegexp use regexp.MustCompile internally, which panics on invalid regex patterns like \C. This causes a runtime panic instead of a test failure.

Fix

Changed matchRegexp to use regexp.Compile and return an error. Regexp and NotRegexp now handle the error by calling Fail with a clear message like Invalid regexp "\C": error parsing regexp: invalid escape sequence: \C.

Test

Added TestRegexpWithInvalidRegex that passes invalid patterns (\C and (?P<open>[)) to both Regexp and NotRegexp. Without the fix, this test panics. With the fix, it passes (assertions return false with error messages).

Tested locally on macOS ARM (Apple Silicon). Full test suite passes.

Use regexp.Compile instead of regexp.MustCompile in matchRegexp so
invalid patterns cause a test failure with a clear error message
instead of a runtime panic.

Fixes stretchr#1794
@ccoVeille
Copy link
Collaborator

Why opening almost same code than

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.

assert.Regexp panics with invalid rx expression

2 participants