Permalink
Browse files

Merge pull request #89 from maxmind/greg/check-data-type

Check data type returned by MaxMind\Db\Reader. Fixes #83
  • Loading branch information...
2 parents e9e40f7 + 417876a commit 0a5c6eaec7c3d83f986e54e0ba4abe3e41563581 @rafl rafl committed on GitHub Jan 31, 2017
Showing with 14 additions and 0 deletions.
  1. +14 −0 src/Database/Reader.php
@@ -5,6 +5,7 @@
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\ProviderInterface;
use MaxMind\Db\Reader as DbReader;
+use MaxMind\Db\Reader\InvalidDatabaseException;
/**
* Instances of this class provide a reader for the GeoIP2 database format.
@@ -223,6 +224,19 @@ private function getRecord($class, $type, $ipAddress)
"The address $ipAddress is not in the database."
);
}
+ if (!is_array($record)) {
+ // This can happen on corrupt databases. Generally,
+ // MaxMind\Db\Reader will throw a
+ // MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
+ // the lookup may result in a record that looks valid but is not
+ // an array. This mostly happens when the user is ignoring all
+ // exceptions and the more frequent InvalidDatabaseException
+ // exceptions go unnoticed.
+ throw new InvalidDatabaseException(
+ "Expected an array when looking up $ipAddress but received: "
+ . gettype($record)
+ );
+ }
return $record;
}

0 comments on commit 0a5c6ea

Please sign in to comment.