From: Kunal Mehta Date: Thu, 15 Oct 2015 18:37:38 +0000 (-0700) Subject: Throw Exceptions on preg_* failures in MagicWordArray::matchAndRemove() X-Git-Tag: 1.31.0-rc.0~9281 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22auteur_infos%22%2C%20%22id_auteur=%24id%22%29%20.%20%22?a=commitdiff_plain;h=6627978c77c4fea850507cb6cd6e1b8ee36ce748;p=lhc%2Fweb%2Fwiklou.git Throw Exceptions on preg_* failures in MagicWordArray::matchAndRemove() 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 --- diff --git a/includes/MagicWord.php b/includes/MagicWord.php index 2c7ba91bf2..424735e51b 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -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; }