Skip to content

Commit 99c3f24

Browse files
authored
fix: prevent infinite loop in word_wrap() with a character limit below 2 (#10596)
* fix: prevent infinite loop in word_wrap() with a character limit below 2 * test: add regression tests for word_wrap() with small character limits * fix: wrap word_wrap() at one character when charlim is 1 * fix: keep long words whole in word_wrap() when charlim is 1 * refactor: drop redundant charlim clamp in word_wrap() * docs: add changelog entry for word_wrap() small charlim fix
1 parent 092c8f3 commit 99c3f24

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎system/Helpers/text_helper.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ function word_wrap(string $str, int $charlim = 76): string
383383

384384
$temp = '';
385385

386-
while (mb_strlen($line) > $charlim) {
386+
// A limit of 1 can't be used to split a word (each piece needs room for
387+
// the continuation), so the word is kept whole, like a long URL.
388+
while ($charlim > 1 && mb_strlen($line) > $charlim) {
387389
// If the over-length word is a URL we won't wrap it
388390
if (preg_match('!\[url.+\]|://|www\.!', $line)) {
389391
break;

‎tests/system/Helpers/TextHelperTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,27 @@ public function testEllipsize(): void
358358
}
359359
}
360360

361+
/**
362+
* A character limit below 2 used to make word_wrap() loop forever.
363+
*/
364+
#[DataProvider('provideWordWrapSmallCharLimit')]
365+
public function testWordWrapSmallCharLimit(int $charlim): void
366+
{
367+
$this->assertSame("aaaa\nbbbb", word_wrap('aaaa bbbb', $charlim));
368+
}
369+
370+
/**
371+
* @return iterable<string, array{int}>
372+
*/
373+
public static function provideWordWrapSmallCharLimit(): iterable
374+
{
375+
yield 'one' => [1];
376+
377+
yield 'zero' => [0];
378+
379+
yield 'negative' => [-5];
380+
}
381+
361382
public function testWordWrap(): void
362383
{
363384
$string = 'Here is a simple string of text that will help us demonstrate this function.';

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Bugs Fixed
5757
- **Debug:** Fixed a bug where ``Timer::start()`` treated ``0.0`` as an empty value and substituted the current time.
5858
- **Files:** Fixed a bug where ``File::move()`` and ``UploadedFile::move()`` set executable and overly permissive file permissions (``0777 & ~umask()`` instead of ``0666 & ~umask()``), and ``UploadedFile::move()`` targeted the parent directory instead of the destination file for ``chmod()``.
5959
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.
60+
- **Helpers:** Fixed a bug where ``word_wrap()`` entered an infinite loop when the character limit was below 2. Over-length words are now kept whole in that case.
6061
- **Honeypot:** Fixed a bug where bot detection returned an HTTP 500 response instead of 403 (Forbidden).
6162
- **I18n:** Fixed a bug where ``Time::today()``, ``Time::yesterday()``, and ``Time::tomorrow()`` ignored the specified ``$timezone`` and ``setTestNow()`` when calculating the day.
6263
- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.

0 commit comments

Comments
 (0)