media: limit size of stderr being logged
authorAntoine Musso <hashar@free.fr>
Wed, 4 Dec 2013 17:00:21 +0000 (18:00 +0100)
committerAntoine Musso <hashar@free.fr>
Fri, 6 Dec 2013 17:27:42 +0000 (18:27 +0100)
Whenever a rendering commands emit a ton of errors (spotted a case
generating 125MB), the call to trim() makes PHP copy the stderr which
might exhaust the remaining available memory.

The new constant MediaHandler::MAX_ERR_LOG_SIZE = 65535 is used to
substr() the error log before it get trimmed and logged.

bug: 57985
Change-Id: Ibd4cf0331529d323a68273163ac230b07a6c6108

includes/media/MediaHandler.php

index 562de13..b002dfb 100644 (file)
@@ -31,6 +31,10 @@ abstract class MediaHandler {
        const METADATA_GOOD = true;
        const METADATA_BAD = false;
        const METADATA_COMPATIBLE = 2; // for old but backwards compatible.
+       /**
+        * Max length of error logged by logErrorForExternalProcess()
+        */
+       const MAX_ERR_LOG_SIZE = 65535;
 
        /** @var MediaHandler[] Instance cache with array of MediaHandler */
        protected static $handlers = array();
@@ -711,13 +715,17 @@ abstract class MediaHandler {
         *
         * @since 1.23
         * @param $retval int
-        * @param $err int
+        * @param $err string Error reported by command. Anything longer than
+        * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
         * @param $cmd string
         */
        protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
+               # Keep error output limited (bug 57985)
+               $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
+
                wfDebugLog( 'thumbnail',
                        sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
-                                       wfHostname(), $retval, trim( $err ), $cmd ) );
+                                       wfHostname(), $retval, $errMessage, $cmd ) );
        }
 
 }