From: skodak <skodak>
Date: Thu, 8 Mar 2007 00:05:58 +0000 (+0000)
Subject: MDL-6196 infinite loop in header encoding when using some site charsets (gb18030... 
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=684663f526f9e7395a6e0c19910bd8ecc662dc33;p=moodle.git

MDL-6196 infinite loop in header encoding when using some site charsets (gb18030); merged from MOODLE_18_STABLE
---

diff --git a/lib/textlib.class.php b/lib/textlib.class.php
index d07e751401..61234e8573 100644
--- a/lib/textlib.class.php
+++ b/lib/textlib.class.php
@@ -242,8 +242,14 @@ class textlib {
         $ratio = $this->strlen($text, $charset) / strlen($text);
     /// Base64 ratio
         $magic = $avglength = floor(3 * $length * $ratio / 4);
+    /// basic infinite loop protection
+        $maxiterations = strlen($text)*2;
+        $iteration = 0; 
     /// Iterate over the string in magic chunks
         for ($i=0; $i <= $this->strlen($text, $charset); $i+=$magic) {
+            if ($iteration++ > $maxiterations) {
+                return str_repeat('?', strlen($text)); // probably infinite loop, safer to use raw char length here 
+            } 
             $magic = $avglength;
             $offset = 0;
         /// Ensure the chunk fits in length, reduding magic if necessary