Skip to content

Commit cf6fe33

Browse files
committed
fix: prevent infinite loop in word_wrap() with a character limit below 2
1 parent 94e9a16 commit cf6fe33

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎system/Helpers/text_helper.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ function convert_accented_characters(string $str): string
345345
*/
346346
function word_wrap(string $str, int $charlim = 76): string
347347
{
348+
// A limit below 1 is meaningless and would make the wrapping loop endless
349+
$charlim = max(1, $charlim);
350+
348351
// Reduce multiple spaces
349352
$str = preg_replace('| +|', ' ', $str);
350353

@@ -388,9 +391,11 @@ function word_wrap(string $str, int $charlim = 76): string
388391
if (preg_match('!\[url.+\]|://|www\.!', $line)) {
389392
break;
390393
}
391-
// Trim the word down
392-
$temp .= mb_substr($line, 0, $charlim - 1);
393-
$line = mb_substr($line, $charlim - 1);
394+
// Trim the word down. Always take at least one character,
395+
// otherwise a limit of 1 or less would never make progress.
396+
$chunkLength = max(1, $charlim - 1);
397+
$temp .= mb_substr($line, 0, $chunkLength);
398+
$line = mb_substr($line, $chunkLength);
394399
}
395400

396401
// If $temp contains data it means we had to split up an over-length

0 commit comments

Comments
 (0)