r99059 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99058‎ | r99059 | r99060 >
Date:23:31, 5 October 2011
Author:tstarling
Status:ok
Tags:
Comment:
(bug 31379) Don't use the $errcontext parameter of a PHP error handler to get information for error display, this introduces an unexpected, difficult-to-maintain data flow which leads to bugs like the referenced one above.
Modified paths:
  • /trunk/extensions/AbuseFilter/AbuseFilter.parser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/AbuseFilter.parser.php
@@ -216,12 +216,13 @@
217217 $pattern .= 'i';
218218 }
219219
 220+ $handler = new AFPRegexErrorHandler( $pattern, $pos );
220221 try {
221 - set_error_handler( array( 'AbuseFilterParser', 'regexErrorHandler' ) );
 222+ $handler->install();
222223 $result = preg_match( $pattern, $str );
223 - restore_error_handler();
 224+ $handler->restore();
224225 } catch ( Exception $e ) {
225 - restore_error_handler();
 226+ $handler->restore();
226227 throw $e;
227228 }
228229 return new AFPData( self::DBool, (bool)$result );
@@ -394,6 +395,32 @@
395396 }
396397 }
397398
 399+class AFPRegexErrorHandler {
 400+ function __construct( $regex, $pos ) {
 401+ $this->regex = $regex;
 402+ $this->pos = $pos;
 403+ }
 404+
 405+ function handleError( $errno, $errstr, $errfile, $errline, $context ) {
 406+ if ( error_reporting() == 0 ) {
 407+ return true;
 408+ }
 409+ throw new AFPUserVisibleException(
 410+ 'regexfailure',
 411+ $this->pos,
 412+ array( $errstr, $this->regex )
 413+ );
 414+ }
 415+
 416+ function install() {
 417+ set_error_handler( array( $this, 'handleError' ) );
 418+ }
 419+
 420+ function restore() {
 421+ restore_error_handler();
 422+ }
 423+}
 424+
398425 class AbuseFilterParser {
399426 var $mParams, $mVars, $mCode, $mTokens, $mPos, $mCur, $mShortCircuit, $mAllowShort;
400427
@@ -1477,12 +1504,13 @@
14781505
14791506 $matches = array();
14801507
 1508+ $handler = new AFPRegexErrorHandler( $needle, $this->mCur->pos );
14811509 try {
1482 - set_error_handler( array( 'AbuseFilterParser', 'regexErrorHandler' ) );
 1510+ $handler->install();
14831511 $count = preg_match_all( $needle, $haystack, $matches );
1484 - restore_error_handler();
 1512+ $handler->restore();
14851513 } catch ( Exception $e ) {
1486 - restore_error_handler();
 1514+ $handler->restore();
14871515 throw $e;
14881516 }
14891517 }
@@ -1766,17 +1794,6 @@
17671795
17681796 return AFPData::castTypes( $val, AFPData::DBool );
17691797 }
1770 -
1771 - public static function regexErrorHandler( $errno, $errstr, $errfile, $errline, $context ) {
1772 - if ( error_reporting() == 0 ) {
1773 - return true;
1774 - }
1775 - throw new AFPUserVisibleException(
1776 - 'regexfailure',
1777 - $context['pos'],
1778 - array( $errstr, $context['regex'] )
1779 - );
1780 - }
17811798 }
17821799
17831800 # Taken from http://au2.php.net/manual/en/function.fnmatch.php#71725

Follow-up revisions

RevisionCommit summaryAuthorDate
r99060MFT r99059aaron23:39, 5 October 2011
r100383REL1_18 MFT r97886, r97899, r97969, r98179, r98497, r98654, r98773, r98801, r...reedy21:36, 20 October 2011

Status & tagging log