r13599 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r13598‎ | r13599 | r13600 >
Date:04:00, 12 April 2006
Author:tstarling
Status:old
Tags:
Comment:
Proper handling of division by zero
Modified paths:
  • /trunk/extensions/ParserFunctions/Expr.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/Expr.php
@@ -103,6 +103,7 @@
104104 'expr_unexpected_closing_bracket' => 'Expression error: unexpected closing bracket',
105105 'expr_unrecognised_punctuation' => 'Expression error: unrecognised punctuation character "$1"',
106106 'expr_unclosed_bracket' => 'Expression error: unclosed bracket',
 107+ 'expr_division_by_zero' => 'Division by zero',
107108 ));
108109 }
109110
@@ -256,8 +257,8 @@
257258 } elseif ( $char == ')' ) {
258259 $lastOp = end( $operators );
259260 while ( $lastOp && $lastOp != EXPR_OPEN ) {
260 - if ( !$this->doOperation( $lastOp, $operands ) ) {
261 - $this->error( 'missing_operand', $this->names[$lastOp] );
 261+ if ( true !== ( $error = $this->doOperation( $lastOp, $operands ) ) ) {
 262+ $this->error( $error, $this->names[$lastOp] );
262263 return false;
263264 }
264265 array_pop( $operators );
@@ -298,8 +299,8 @@
299300 // Shunting yard magic
300301 $lastOp = end( $operators );
301302 while ( $lastOp && $this->precedence[$op] <= $this->precedence[$lastOp] ) {
302 - if ( !$this->doOperation( $lastOp, $operands ) ) {
303 - $this->error( 'missing_operand', $this->names[$lastOp] );
 303+ if ( true !== ( $error = $this->doOperation( $lastOp, $operands ) ) ) {
 304+ $this->error( $error, $this->names[$lastOp] );
304305 return false;
305306 }
306307 array_pop( $operators );
@@ -315,8 +316,8 @@
316317 $this->error( 'unclosed_bracket' );
317318 return false;
318319 }
319 - if ( !$this->doOperation( $op, $operands ) ) {
320 - $this->error( 'missing_operand', $this->names[$op] );
 320+ if ( true !== ( $error = $this->doOperation( $op, $operands ) ) ) {
 321+ $this->error( $error, $this->names[$op] );
321322 return false;
322323 }
323324 }
@@ -327,98 +328,100 @@
328329 function doOperation( $op, &$stack ) {
329330 switch ( $op ) {
330331 case EXPR_NEGATIVE:
331 - if ( count( $stack ) < 1 ) return false;
 332+ if ( count( $stack ) < 1 ) return 'missing_operand';
332333 $arg = array_pop( $stack );
333334 $stack[] = -$arg;
334335 break;
335336 case EXPR_POSITIVE:
336 - if ( count( $stack ) < 1 ) return false;
 337+ if ( count( $stack ) < 1 ) return 'missing_operand';
337338 break;
338339 case EXPR_TIMES:
339 - if ( count( $stack ) < 2 ) return false;
 340+ if ( count( $stack ) < 2 ) return 'missing_operand';
340341 $right = array_pop( $stack );
341342 $left = array_pop( $stack );
342343 $stack[] = $left * $right;
343344 break;
344345 case EXPR_DIVIDE:
345 - if ( count( $stack ) < 2 ) return false;
 346+ if ( count( $stack ) < 2 ) return 'missing_operand';
346347 $right = array_pop( $stack );
347348 $left = array_pop( $stack );
 349+ if ( $right == 0 ) return 'division_by_zero';
348350 $stack[] = $left / $right;
349351 break;
350352 case EXPR_MOD:
351 - if ( count( $stack ) < 2 ) return false;
 353+ if ( count( $stack ) < 2 ) return 'missing_operand';
352354 $right = array_pop( $stack );
353355 $left = array_pop( $stack );
 356+ if ( $right == 0 ) return 'division_by_zero';
354357 $stack[] = $left % $right;
355358 break;
356359 case EXPR_PLUS:
357 - if ( count( $stack ) < 2 ) return false;
 360+ if ( count( $stack ) < 2 ) return 'missing_operand';
358361 $right = array_pop( $stack );
359362 $left = array_pop( $stack );
360363 $stack[] = $left + $right;
361364 break;
362365 case EXPR_MINUS:
363 - if ( count( $stack ) < 2 ) return false;
 366+ if ( count( $stack ) < 2 ) return 'missing_operand';
364367 $right = array_pop( $stack );
365368 $left = array_pop( $stack );
366369 $stack[] = $left - $right;
367370 break;
368371 case EXPR_AND:
369 - if ( count( $stack ) < 2 ) return false;
 372+ if ( count( $stack ) < 2 ) return 'missing_operand';
370373 $right = array_pop( $stack );
371374 $left = array_pop( $stack );
372375 $stack[] = ( $left && $right ) ? 1 : 0;
373376 break;
374377 case EXPR_OR:
375 - if ( count( $stack ) < 2 ) return false;
 378+ if ( count( $stack ) < 2 ) return 'missing_operand';
376379 $right = array_pop( $stack );
377380 $left = array_pop( $stack );
378381 $stack[] = ( $left || $right ) ? 1 : 0;
379382 break;
380383 case EXPR_EQUALITY:
381 - if ( count( $stack ) < 2 ) return false;
 384+ if ( count( $stack ) < 2 ) return 'missing_operand';
382385 $right = array_pop( $stack );
383386 $left = array_pop( $stack );
384387 $stack[] = ( $left == $right ) ? 1 : 0;
385388 break;
386389 case EXPR_NOT:
387 - if ( count( $stack ) < 1 ) return false;
 390+ if ( count( $stack ) < 1 ) return 'missing_operand';
388391 $arg = array_pop( $stack );
389392 $stack[] = (!$arg) ? 1 : 0;
390393 break;
391394 case EXPR_ROUND:
392 - if ( count( $stack ) < 2 ) return false;
 395+ if ( count( $stack ) < 2 ) return 'missing_operand';
393396 $digits = intval( array_pop( $stack ) );
394397 $value = array_pop( $stack );
395398 $stack[] = round( $value, $digits );
396399 break;
397400 case EXPR_LESS:
398 - if ( count( $stack ) < 2 ) return false;
 401+ if ( count( $stack ) < 2 ) return 'missing_operand';
399402 $right = array_pop( $stack );
400403 $left = array_pop( $stack );
401404 $stack[] = ( $left < $right ) ? 1 : 0;
402405 break;
403406 case EXPR_GREATER:
404 - if ( count( $stack ) < 2 ) return false;
 407+ if ( count( $stack ) < 2 ) return 'missing_operand';
405408 $right = array_pop( $stack );
406409 $left = array_pop( $stack );
407410 $stack[] = ( $left > $right ) ? 1 : 0;
408411 break;
409412 case EXPR_LESSEQ:
410 - if ( count( $stack ) < 2 ) return false;
 413+ if ( count( $stack ) < 2 ) return 'missing_operand';
411414 $right = array_pop( $stack );
412415 $left = array_pop( $stack );
413416 $stack[] = ( $left <= $right ) ? 1 : 0;
414417 break;
415418 case EXPR_GREATEREQ:
416 - if ( count( $stack ) < 2 ) return false;
 419+ if ( count( $stack ) < 2 ) return 'missing_operand';
417420 $right = array_pop( $stack );
418421 $left = array_pop( $stack );
419422 $stack[] = ( $left >= $right ) ? 1 : 0;
420423 break;
421424 case EXPR_NOTEQ:
422 - if ( count( $stack ) < 2 ) return false;
 425+ if ( count( $stack ) < 2 ) return 'missing_operand';
423426 $right = array_pop( $stack );
424427 $left = array_pop( $stack );
425428 $stack[] = ( $left != $right ) ? 1 : 0;

Status & tagging log