Skip to content

Report deeply nested regex as invalid instead of raising RecursionError - #1539

Closed
deepakganesh78 wants to merge 1 commit into
python-jsonschema:mainfrom
deepakganesh78:fix/issue1538-regex-recursion-error
Closed

Report deeply nested regex as invalid instead of raising RecursionError#1539
deepakganesh78 wants to merge 1 commit into
python-jsonschema:mainfrom
deepakganesh78:fix/issue1538-regex-recursion-error

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #1538.

Summary

The regex format checker crashes with RecursionError instead of reporting the instance as invalid when the pattern contains deeply nested groups.

Reproduction

`python
from jsonschema import Draft202012Validator, FormatChecker

v = Draft202012Validator({"format": "regex"}, format_checker=FormatChecker())
list(v.iter_errors("(" * 500))

RecursionError: maximum recursion depth exceeded

`

Root cause

re.compile raises RecursionError when the regex pattern nesting exceeds the C stack limit. RecursionError is not a subclass of re.error, so FormatChecker.check does not catch it and the exception propagates uncaught.

This is the same class of issue as the OverflowError case in #1526, but a distinct exception type (RecursionError inherits from RuntimeError, not ArithmeticError).

Fix

Add RecursionError alongside re.error in the raises declaration for is_regex, so FormatChecker.check converts it to a FormatError and the instance is reported as invalid.

Compatibility

No backward-incompatible changes. Previously crashing inputs now produce a validation error.

Validation

  • New test test_regex_format_rejects_deeply_nested_pattern fails before the fix, passes after.
  • Full test suite: 8281 passed, 232 skipped, 0 failures.
  • ruff check clean on touched files.

re.compile raises RecursionError (not an re.error subclass) when a
pattern contains deeply nested groups, e.g. '(' * 500.  The regex
format checker was registered with raises=re.error only, so the
RecursionError propagated uncaught from iter_errors instead of being
reported as an invalid format.

Add RecursionError alongside re.error in the raises declaration so
FormatChecker.check converts it to a FormatError.

Fixes python-jsonschema#1538

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 python-jsonschema | 🛠️ Build #33868602 | 📁 Comparing db6c9df against latest (826e5b2)

  🔍 Preview build  

1 file changed
± _modules/jsonschema/_format/index.html

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.

regex format checker crashes with RecursionError on deeply nested patterns

2 participants