From 39a63a3c16b54a89e4ccdadfa52dcf5bdd8ff1bb Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 19 Jul 2018 10:50:28 +0100 Subject: [PATCH] Make MapCacheLRU throw errors for bad $field arguments Change-Id: Ibde33e0ff671d3428b73c66766478f58763726e1 --- includes/libs/MapCacheLRU.php | 13 ++++- .../phpunit/includes/libs/MapCacheLRUTest.php | 50 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php index ad5e58d808..a0381dece9 100644 --- a/includes/libs/MapCacheLRU.php +++ b/includes/libs/MapCacheLRU.php @@ -141,7 +141,7 @@ class MapCacheLRU implements IExpiringStore, Serializable { public function has( $key, $maxAge = 0.0 ) { if ( !is_int( $key ) && !is_string( $key ) ) { throw new UnexpectedValueException( - __METHOD__ . ' called with invalid key. Must be string or integer.' ); + __METHOD__ . ': invalid key; must be string or integer.' ); } if ( !array_key_exists( $key, $this->cache ) ) { @@ -183,6 +183,11 @@ class MapCacheLRU implements IExpiringStore, Serializable { $this->set( $key, [], $initRank ); } + if ( !is_int( $field ) && !is_string( $field ) ) { + throw new UnexpectedValueException( + __METHOD__ . ": invalid field for '$key'; must be string or integer." ); + } + if ( !is_array( $this->cache[$key] ) ) { throw new UnexpectedValueException( "The value of '$key' is not an array." ); } @@ -199,6 +204,12 @@ class MapCacheLRU implements IExpiringStore, Serializable { */ public function hasField( $key, $field, $maxAge = 0.0 ) { $value = $this->get( $key ); + + if ( !is_int( $field ) && !is_string( $field ) ) { + throw new UnexpectedValueException( + __METHOD__ . ": invalid field for '$key'; must be string or integer." ); + } + if ( !is_array( $value ) || !array_key_exists( $field, $value ) ) { return false; } diff --git a/tests/phpunit/includes/libs/MapCacheLRUTest.php b/tests/phpunit/includes/libs/MapCacheLRUTest.php index a06ef62dfc..7147c6fa22 100644 --- a/tests/phpunit/includes/libs/MapCacheLRUTest.php +++ b/tests/phpunit/includes/libs/MapCacheLRUTest.php @@ -214,4 +214,54 @@ class MapCacheLRUTest extends PHPUnit\Framework\TestCase { $this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) ); $this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) ); } + + /** + * @covers MapCacheLRU::has() + * @covers MapCacheLRU::get() + * @covers MapCacheLRU::set() + * @covers MapCacheLRU::hasField() + * @covers MapCacheLRU::getField() + * @covers MapCacheLRU::setField() + */ + public function testInvalidKeys() { + $cache = MapCacheLRU::newFromArray( [], 3 ); + + try { + $cache->has( 3.4 ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + try { + $cache->get( false ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + try { + $cache->set( 3.4, 'x' ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + + try { + $cache->hasField( 'x', 3.4 ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + try { + $cache->getField( 'x', false ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + try { + $cache->setField( 'x', 3.4, 'x' ); + $this->fail( "No exception" ); + } catch ( UnexpectedValueException $e ) { + $this->assertRegExp( '/must be string or integer/', $e->getMessage() ); + } + } } -- 2.20.1