Skip to content

fix: prevent infinite loop in word_wrap() with a character limit below 2 - #10596

Open
mdalikadar wants to merge 4 commits into
codeigniter4:developfrom
mdalikadar:fix/word-wrap-small-charlim
Open

mdalikadar wants to merge 4 commits into
codeigniter4:developfrom
mdalikadar:fix/word-wrap-small-charlim

Conversation

@mdalikadar

Copy link
Copy Markdown
Contributor

Description

word_wrap() never returns when $charlim is smaller than 2.

The loop that splits over-length words takes $charlim - 1 characters per
iteration:

  • $charlim = 1: the chunk length is 0, so $line never shrinks and the
    while (mb_strlen($line) > $charlim) loop runs forever.
  • $charlim <= 0: the length check is always true, even for an empty line,
    so the loop never ends.

Reproduce:

helper('text');
word_wrap('aaaa bbbb', 1); // hangs until max_execution_time / forever on CLI

Changes:

  • Clamp $charlim to at least 1 at the start of the function.
  • Always consume at least one character per iteration of the splitting loop.

Behavior for $charlim >= 2 is unchanged. For $charlim <= 1, which previously
hung, word_wrap('aaaa bbbb', 1) now returns "aaa\na\nbbb\nb", the same
chunking the function applies for larger limits.

Added regression tests for limits of 1, 0 and a negative value. The new test
hangs without the fix and passes with it.

This is a bug fix with no signature or documented-behavior change, so it targets
develop.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated (not needed: no documented behavior changed)
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 2, 2026
@paulbalandan
paulbalandan requested a balanced review from Copilot October 5, 2026 14:28

Copilot AI left a comment

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.

Copilot review overview

🟡 Changes recommended

The new limit-1 behavior terminates but does not wrap at the documented character count.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Prevents word_wrap() from hanging when the character limit is below 2.

Changes:

  • Clamps limits to at least 1.
  • Ensures each loop iteration consumes a character.
  • Adds regression coverage for limits 1, 0, and -5.

The fix terminates, but limit 1 still produces lines longer than one character. Focused PHP verification confirmed a maximum line length of 3; PHPUnit and full validation remain for CI.

File Description
system/​Helpers/​text_helper.php Adds limit normalization and guaranteed loop progress.
tests/​system/​Helpers/​TextHelperTest.php Tests small and negative limits.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread system/Helpers/text_helper.php Outdated
Comment thread tests/system/Helpers/TextHelperTest.php

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants