r65034 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65033‎ | r65034 | r65035 >
Date:16:10, 14 April 2010
Author:vasilievvv
Status:deferred
Tags:
Comment:
* Make sanity limits actually work
* Add testcase for nested templates
Modified paths:
  • /trunk/extensions/InlineScripts/InlineScripts.i18n.php (modified) (history)
  • /trunk/extensions/InlineScripts/InlineScripts.php (modified) (history)
  • /trunk/extensions/InlineScripts/interpreter/Interpreter.php (modified) (history)
  • /trunk/extensions/InlineScripts/interpreter/Shared.php (modified) (history)
  • /trunk/extensions/InlineScripts/interpreterTests.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/InlineScripts/interpreter/Interpreter.php
@@ -16,6 +16,10 @@
1717 public function __construct() {
1818 global $wgInlineScriptsParserClass;
1919 $this->mCodeParser = new $wgInlineScriptsParserClass( $this );
 20+ $this->mMaxRecursion =
 21+ $this->mEvaluations =
 22+ $this->mTokens =
 23+ 0;
2024 }
2125
2226 public function checkRecursionLimit( $rec ) {
@@ -28,7 +32,8 @@
2933 public function increaseEvaluationsCount() {
3034 global $wgInlineScriptsLimits;
3135 $this->mEvaluations++;
32 - return $this->mEvaluations <= $wgInlineScriptsLimits['evaluations'];
 36+ if( $this->mEvaluations > $wgInlineScriptsLimits['evaluations'] )
 37+ throw new ISUserVisibleException( 'toomanyevals', 0 );
3338 }
3439
3540 public function getMaxTokensLeft() {
@@ -93,7 +98,7 @@
9499 * in a sepereate context with its own output, variables and parser frame.
95100 */
96101 class InlineScriptEvaluationContext {
97 - var $mVars, $mOut, $mParser, $mFrame, $mInterpteter;
 102+ var $mVars, $mOut, $mParser, $mFrame, $mInterpreter;
98103
99104 static $mFunctions = array(
100105 'out' => 'funcOut',
@@ -131,7 +136,7 @@
132137 public function __construct( $interpreter, $parser, $frame ) {
133138 $this->mVars = array();
134139 $this->mOut = '';
135 - $this->mInterpteter = $interpreter;
 140+ $this->mInterpreter = $interpreter;
136141 $this->mParser = $parser;
137142 $this->mFrame = $frame;
138143 }
@@ -141,6 +146,10 @@
142147 throw new ISException( 'evaluateNode() accepts only nonterminals' );
143148 }
144149
 150+ if( !$this->mInterpreter->checkRecursionLimit( $rec ) ) {
 151+ throw new ISUserVisibleException( 'recoverflow', 0 );
 152+ }
 153+
145154 $c = $node->getChildren();
146155 switch( $node->getType() ) {
147156 case 'stmts':
@@ -172,7 +181,7 @@
173182 case 'foreach':
174183 $array = $this->evaluateNode( $c[4], $rec + 1 );
175184 if( $array->type != ISData::DList )
176 - throw new ISException( 'invalidforeach', $c[0]->type );
 185+ throw new ISUserVisibleException( 'invalidforeach', $c[0]->type );
177186 $last = new ISData();
178187 foreach( $array->data as $item ) {
179188 $this->setVar( $c[2], $item, $rec );
@@ -206,6 +215,7 @@
207216 return $this->evaluateNode( $c[0], $rec + 1 );
208217 }
209218 case 'exprset':
 219+ $this->mInterpreter->increaseEvaluationsCount();
210220 if( $c[1]->value == '=' ) {
211221 $new = $this->evaluateNode( $c[2], $rec + 1 );
212222 $this->setVar( $c[0], $new, $rec );
@@ -226,6 +236,7 @@
227237 return $this->evaluateNode( $c[4], $rec + 1 );
228238 }
229239 case 'exprlogical':
 240+ $this->mInterpreter->increaseEvaluationsCount();
230241 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
231242 switch( $c[1]->value ) {
232243 case '&':
@@ -246,10 +257,12 @@
247258 }
248259 case 'exprequals':
249260 case 'exprcompare':
 261+ $this->mInterpreter->increaseEvaluationsCount();
250262 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
251263 $arg2 = $this->evaluateNode( $c[2], $rec + 1 );
252264 return ISData::compareOp( $arg1, $arg2, $c[1]->value );
253265 case 'exprsum':
 266+ $this->mInterpreter->increaseEvaluationsCount();
254267 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
255268 $arg2 = $this->evaluateNode( $c[2], $rec + 1 );
256269 switch( $c[1]->value ) {
@@ -259,14 +272,17 @@
260273 return ISData::sub( $arg1, $arg2 );
261274 }
262275 case 'exprmul':
 276+ $this->mInterpreter->increaseEvaluationsCount();
263277 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
264278 $arg2 = $this->evaluateNode( $c[2], $rec + 1 );
265279 return ISData::mulRel( $arg1, $arg2, $c[1]->value, $c[1]->line );
266280 case 'exprpow':
 281+ $this->mInterpreter->increaseEvaluationsCount();
267282 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
268283 $arg2 = $this->evaluateNode( $c[2], $rec + 1 );
269284 return ISData::pow( $arg1, $arg2 );
270285 case 'exprkeyword':
 286+ $this->mInterpreter->increaseEvaluationsCount();
271287 $arg1 = $this->evaluateNode( $c[0], $rec + 1 );
272288 $arg2 = $this->evaluateNode( $c[2], $rec + 1 );
273289 switch( $c[1]->value ) {
@@ -278,15 +294,18 @@
279295 throw new ISException( "Invalid keyword: {$c[1]->value}" );
280296 }
281297 case 'exprinvert':
 298+ $this->mInterpreter->increaseEvaluationsCount();
282299 $arg = $this->evaluateNode( $c[1], $rec + 1 );
283300 return ISData::boolInvert( $arg );
284301 case 'exprunary':
 302+ $this->mInterpreter->increaseEvaluationsCount();
285303 $arg = $this->evaluateNode( $c[1], $rec + 1 );
286304 if( $c[0]->value == '-' )
287305 return ISData::unaryMinus( $arg );
288306 else
289307 return $arg;
290308 case 'exprfunction':
 309+ $this->mInterpreter->increaseEvaluationsCount();
291310 if( $c[0] instanceof ISToken ) {
292311 $funcname = $c[0]->value;
293312 if( !isset( self::$mFunctions[$funcname] ) )
Index: trunk/extensions/InlineScripts/interpreter/Shared.php
@@ -164,7 +164,7 @@
165165
166166 public function appendTokenCount( &$interpr ) {
167167 global $wgInlineScriptsLimits;
168 - $interpr->mTokens += $this->mTokensCount;
 168+ $interpr->mTokens += $this->mTokens;
169169 if( $interpr->mTokens > $wgInlineScriptsLimits['tokens'] )
170170 throw new ISUserVisibleException( 'toomanytokens', 0 );
171171 }
Index: trunk/extensions/InlineScripts/interpreterTests.txt
@@ -209,7 +209,26 @@
210210 </p>
211211 !! end
212212
 213+!! article
 214+Template:123
 215+!! text
 216+<wikiscript>
 217+out( 123 );
 218+</wikiscript>
 219+!! endarticle
 220+
213221 !! test
 222+Nested wikiscripts via parse()
 223+!! input
 224+<wikiscript>
 225+out( parse( '{{123}}' ) );
 226+</wikiscript>
 227+!! result
 228+<p>123
 229+</p>
 230+!! end
 231+
 232+!! test
214233 String functions 1
215234 !! input
216235 {{#inline: lc( 'FOO' ) == 'foo' & uc( 'foo' ) == 'FOO' &
Index: trunk/extensions/InlineScripts/InlineScripts.i18n.php
@@ -17,7 +17,7 @@
1818 'inlinescripts-exception-unclosedstring' => 'Unclosed string at char $1',
1919 'inlinescripts-exception-unrecognisedtoken' => 'Unrecognized token at char $1',
2020 'inlinescripts-exception-toomanytokens' => 'Exceeded tokens limit',
21 - 'inlinescripts-exception-toomanyevals' => 'Exceeded evaluations limit at line $1',
 21+ 'inlinescripts-exception-toomanyevals' => 'Exceeded evaluations limit',
2222 'inlinescripts-exception-recoverflow' => 'Too deep abstract syntax tree',
2323 'inlinescripts-exception-notanarray' => 'Tried to get an element of a non-array at line $1',
2424 'inlinescripts-exception-outofbounds' => 'Got out of array bounds at line $1',
Index: trunk/extensions/InlineScripts/InlineScripts.php
@@ -46,7 +46,7 @@
4747 * Maximal amount of tokens (strings, keywords, numbers, operators,
4848 * but not whitespace) to be parsed.
4949 */
50 - 'tokens' => 25000,
 50+ 'tokens' => 100000,
5151 /**
5252 * Maximal amount of operations (multiplications, comarsions, function
5353 * calls) to be done.
@@ -115,10 +115,11 @@
116116
117117 public static function reportLimits( &$parser, &$report ) {
118118 global $wgInlineScriptsLimits;
 119+ $i = self::getInterpreter();
119120 $report .=
120 - "Inline scripts parser evaluations: {$parser->is_evalsCount}/{$wgInlineScriptsLimits['evaluations']}\n" .
121 - "Inline scripts tokens: {$parser->is_tokensCount}/{$wgInlineScriptsLimits['tokens']}\n" .
122 - "Inline scripts AST maximal depth: {$parser->is_maxDepth}/{$wgInlineScriptsLimits['depth']}\n";
 121+ "Inline scripts parser evaluations: {$i->mEvaluations}/{$wgInlineScriptsLimits['evaluations']}\n" .
 122+ "Inline scripts tokens: {$i->mTokens}/{$wgInlineScriptsLimits['tokens']}\n" .
 123+ "Inline scripts AST maximal depth: {$i->mMaxRecursion}/{$wgInlineScriptsLimits['depth']}\n";
123124 return true;
124125 }
125126

Status & tagging log