Skip to content

Commit 0a5c6ea

Browse files
authored
Merge pull request #89 from maxmind/greg/check-data-type
Check data type returned by MaxMind\Db\Reader. Fixes #83
2 parents e9e40f7 + 417876a commit 0a5c6ea

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Database/Reader.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeoIp2\Exception\AddressNotFoundException;
66
use GeoIp2\ProviderInterface;
77
use MaxMind\Db\Reader as DbReader;
8+
use MaxMind\Db\Reader\InvalidDatabaseException;
89

910
/**
1011
* Instances of this class provide a reader for the GeoIP2 database format.
@@ -223,6 +224,19 @@ private function getRecord($class, $type, $ipAddress)
223224
"The address $ipAddress is not in the database."
224225
);
225226
}
227+
if (!is_array($record)) {
228+
// This can happen on corrupt databases. Generally,
229+
// MaxMind\Db\Reader will throw a
230+
// MaxMind\Db\Reader\InvalidDatabaseException, but occasionally
231+
// the lookup may result in a record that looks valid but is not
232+
// an array. This mostly happens when the user is ignoring all
233+
// exceptions and the more frequent InvalidDatabaseException
234+
// exceptions go unnoticed.
235+
throw new InvalidDatabaseException(
236+
"Expected an array when looking up $ipAddress but received: "
237+
. gettype($record)
238+
);
239+
}
226240
return $record;
227241
}
228242

0 commit comments

Comments
 (0)