From 684663f526f9e7395a6e0c19910bd8ecc662dc33 Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 8 Mar 2007 00:05:58 +0000 Subject: [PATCH] MDL-6196 infinite loop in header encoding when using some site charsets (gb18030); merged from MOODLE_18_STABLE --- lib/textlib.class.php | 6 ++++++ 1 file changed, 6 insertions(+) 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 -- 2.39.5