[BUGFIX] canBeInterpretedAsInteger fatals if given an object
authorPhilipp Gampe <forge.typo3.org@philippgampe.info>
Wed, 25 Jan 2012 18:05:16 +0000 (19:05 +0100)
committerSusanne Moog <typo3@susannemoog.de>
Thu, 26 Jan 2012 18:58:11 +0000 (19:58 +0100)
If an object without a string representation is given to
t3lib_utility_math::canBeInterpretedAsInteger($var), the
method will throw a fatal error.

Assume that an object or an array can never be an integer.
<http://php.net/manual/en/language.types.integer
.php#language.types.integer.casting.from-other>

Change-Id: I95e5fe6a5c9f798b5f249c3c14b8cfd4bedd3afe
Fixes: #33446
Releases: 4.6,4.7
Reviewed-on: http://review.typo3.org/8694
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/utility/class.t3lib_utility_math.php
tests/t3lib/utility/class.t3lib_utility_mathTest.php
tests/t3lib/utility/fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php [new file with mode: 0644]

index 0ccc45d..4bbdd26 100644 (file)
@@ -75,11 +75,14 @@ final class t3lib_utility_Math {
        /**
         * Tests if the input can be interpreted as integer.
         *
+        * Note: Integer casting from objects or arrays is considered undefined and thus will return false.
+        * @see http://php.net/manual/en/language.types.integer.php#language.types.integer.casting.from-other
+        *
         * @param $var mixed Any input variable to test
         * @return boolean Returns TRUE if string is an integer
         */
        public static function canBeInterpretedAsInteger($var) {
-               if ($var === '') {
+               if ($var === '' || is_object($var) || is_array($var)) {
                        return FALSE;
                }
                return (string) intval($var) === (string) $var;
index d2076b7..623824e 100644 (file)
@@ -22,6 +22,8 @@
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+require_once('fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php');
+
 /**
  * Testcase for class t3lib_utility_Math
  *
@@ -118,6 +120,12 @@ class t3lib_utility_MathTest extends tx_phpunit_testcase {
         * @return array Data sets
         */
        public function functionCanBeInterpretedAsIntegerInvalidDataProvider() {
+               $objectWithNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
+               $objectWithNumericalStringRepresentation->setString('1234');
+               $objectWithNonNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
+               $objectWithNonNumericalStringRepresentation->setString('foo');
+               $objectWithEmptyStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
+               $objectWithEmptyStringRepresentation->setString('');
                return array(
                        'int as string with leading zero' => array('01234'),
                        'positive int as string with plus modifier' => array('+1234'),
@@ -138,6 +146,10 @@ class t3lib_utility_MathTest extends tx_phpunit_testcase {
                        'empty array' => array(array()),
                        'int in array' => array(array(32425)),
                        'int as string in array' => array(array('32425')),
+                       'object without string representation' => array(new stdClass()),
+                       'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
+                       'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
+                       'object with empty string representation' => array($objectWithEmptyStringRepresentation),
                );
        }
 
diff --git a/tests/t3lib/utility/fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php b/tests/t3lib/utility/fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php
new file mode 100644 (file)
index 0000000..a2eb4d1
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2012 Philipp Gampe (forge.typo3.org@philippgampe.info)
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+
+class t3lib_utility_math_fixture_classWithStringRepresentation {
+
+       protected $string = '';
+
+       public function __toString() {
+               return $this->getString();
+       }
+
+       public function setString($string) {
+               $this->string = $string;
+       }
+
+       public function getString() {
+               return $this->string;
+       }
+
+}
+?>
\ No newline at end of file