Skip to content

Fix mbregex retry-limit failures in Multibyte::trim - #39

Merged
vanderlee merged 2 commits into
masterfrom
fix/27-multibyte-trim-retry-limit
Sep 25, 2026
Merged

vanderlee merged 2 commits into
masterfrom
fix/27-multibyte-trim-retry-limit

Conversation

@vanderlee

@vanderlee vanderlee commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #27.

Long internal whitespace runs cause the trailing \s*$ alternative in Multibyte::trim() to backtrack and retry from successive positions. This can exhaust mbregex's retry limit, emit a warning, and return false instead of the original text.

Use absolute string anchors, possessive whitespace quantifiers, and a negative whitespace lookbehind for the trailing alternative. Each whitespace run is consumed without backtracking and cannot be retried from inside the run. This retains mbregex's encoding and Unicode whitespace support without increasing retry limits or suppressing errors.

Regression coverage includes the reporter's exact 801/1348-space input, the same input with surrounding whitespace, a 20,000-space internal run, empty and whitespace-only strings, CR/LF/tab boundaries, internal newlines, non-breaking spaces, ideographic spaces, and multibyte text. Tests explicitly set and restore UTF-8 for the fixtures and lower mbstring.regex_retry_limit to 1000 when available; PHPUnit restores the limit afterward. Older PHP versions still run the same input cases.

Validation:

  • Before the fix, the trim tests produce three retry-limit-in-match over errors with the limit set to 1000, including the reported input. Current local runtimes did not fail on that input at their default limit.
  • composer test, PHP 8.2.10 / PHPUnit 9.6.37: 47 tests, 133 assertions passed.
  • Full PHPUnit suite in an isolated Linux PHP 8.3.4 container: 47 tests, 133 assertions passed.
  • All seven CI checks passed: PHP 5.4, 5.6, 7.4, 8.0, 8.4, 8.5, and 8.6.
  • composer validate --strict and git diff --check passed.
  • PHPUnit 9 reports the existing phpunit.xml legacy <filter> configuration warning, also present before this change.

@vanderlee
vanderlee merged commit 0465228 into master Sep 25, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant