Throw Exceptions on preg_* failures in MagicWordArray::matchAndRemove()
authorKunal Mehta <legoktm@gmail.com>
Thu, 15 Oct 2015 18:37:38 +0000 (11:37 -0700)
committerOri.livneh <ori@wikimedia.org>
Fri, 23 Oct 2015 16:24:42 +0000 (16:24 +0000)
There are a lot of other cases in this file alone that need to be fixed
(e.g. (bool)preg_match), but those should be fixed in a more systematic
way like a wrapper function.

Bug: T115514
Change-Id: I3840a56adc0a6e50963b930051892491f8e90245

includes/MagicWord.php

index 2c7ba91..424735e 100644 (file)
@@ -941,6 +941,7 @@ class MagicWordArray {
         *
         * @param string $text
         *
+        * @throws Exception
         * @return array
         */
        public function matchAndRemove( &$text ) {
@@ -951,13 +952,22 @@ class MagicWordArray {
                                continue;
                        }
                        $matches = array();
-                       if ( preg_match_all( $regex, $text, $matches, PREG_SET_ORDER ) ) {
+                       $matched = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
+                       if ( $matched === false ) {
+                               throw new Exception( __METHOD__ . ': preg_match_all returned false' );
+                       }
+                       if ( $matched ) {
                                foreach ( $matches as $m ) {
                                        list( $name, $param ) = $this->parseMatch( $m );
                                        $found[$name] = $param;
                                }
                        }
-                       $text = preg_replace( $regex, '', $text );
+                       $replaced = preg_replace( $regex, '', $text );
+                       if ( $replaced !== null ) {
+                               $text = $replaced;
+                       } else {
+                               throw new Exception( __METHOD__ . ': preg_replace returned null' );
+                       }
                }
                return $found;
        }