From 6627978c77c4fea850507cb6cd6e1b8ee36ce748 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 15 Oct 2015 11:37:38 -0700 Subject: [PATCH] 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 --- includes/MagicWord.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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; } -- 2.20.1