From 993f5a486527b63702a17513b8c6b35e09da9c08 Mon Sep 17 00:00:00 2001
From: Steven Wittens <steven@10.no-reply.drupal.org>
Date: Thu, 9 Feb 2006 08:56:11 +0000
Subject: [PATCH] - #41555: truncate_utf8 infinite loop on invalid utf-8

---
 includes/unicode.inc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/includes/unicode.inc b/includes/unicode.inc
index 54fe7bb8ba5b..cd8285514935 100644
--- a/includes/unicode.inc
+++ b/includes/unicode.inc
@@ -193,12 +193,16 @@ function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
     return $string;
   }
   if ($wordsafe) {
+    $end = $len;
     while (($string[--$len] != ' ') && ($len > 0)) {};
+    if ($len == 0) {
+      $len = $end;
+    }
   }
   if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
     return substr($string, 0, $len) . ($dots ? ' ...' : '');
   }
-  while (ord($string[--$len]) < 0xC0) {};
+  while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {};
   return substr($string, 0, $len) . ($dots ? ' ...' : '');
 }
 
-- 
GitLab