Fix malformed UTF-8 going to query profiler
authorShahyar <shahyar@gmail.com>
Fri, 22 Aug 2014 22:07:49 +0000 (15:07 -0700)
committerShahyar <shahyar@gmail.com>
Fri, 29 Aug 2014 00:10:45 +0000 (17:10 -0700)
Bug: 69926
Change-Id: I1c2e3f22104792fbc59491b1c6a17f2a6a0dd0b6

includes/debug/MWDebug.php

index fb11f36..c2f2223 100644 (file)
@@ -335,6 +335,28 @@ class MWDebug {
                        return -1;
                }
 
+               // Replace invalid UTF-8 chars with a square UTF-8 character
+               // This prevents json_encode from erroring out due to binary SQL data
+               $sql = preg_replace(
+                       '/(
+                               [\xC0-\xC1] # Invalid UTF-8 Bytes
+                               | [\xF5-\xFF] # Invalid UTF-8 Bytes
+                               | \xE0[\x80-\x9F] # Overlong encoding of prior code point
+                               | \xF0[\x80-\x8F] # Overlong encoding of prior code point
+                               | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
+                               | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
+                               | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
+                               | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
+                               | (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]
+                                  |[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
+                               | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
+                               | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
+                               | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
+                       )/x',
+                       '■',
+                       $sql
+               );
+
                self::$query[] = array(
                        'sql' => $sql,
                        'function' => $function,