Skip to content

Commit db7b83a

Browse files
committed
Remove vestigial code and improve check
This code didn't make sense given that we couldn't use an integer bigger than PHP_INT_MAX when reading from the file. I suspect we used to inline more of the actual decoding of the value.
1 parent f2009ea commit db7b83a

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
1.10.1
5+
-------------------
6+
7+
* Fix a `TypeError` exception in the pure PHP reader when using large
8+
databases on 32-bit PHP builds with the `bcmath` extension. Reported
9+
by dodo1708. GitHub #124.
10+
411
1.10.0 (2021-02-09)
512
-------------------
613

src/MaxMind/Db/Reader/Decoder.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,13 @@ private function decodePointer(int $ctrlByte, int $offset): array
286286
// machines
287287
$pointerOffset = $this->decodeUint($buffer, $pointerSize);
288288

289-
$byteLength = $pointerSize + $this->pointerBaseByteSize;
290-
291-
if ($byteLength <= _MM_MAX_INT_BYTES) {
292-
$pointer = $pointerOffset + $this->pointerBase;
293-
} elseif (\extension_loaded('gmp')) {
294-
$pointer = gmp_strval(gmp_add($pointerOffset, $this->pointerBase));
295-
} elseif (\extension_loaded('bcmath')) {
296-
$pointer = bcadd($pointerOffset, (string) $this->pointerBase);
289+
$pointerBase = $this->pointerBase;
290+
291+
if (\PHP_INT_MAX - $pointerBase >= $pointerOffset) {
292+
$pointer = $pointerOffset + $pointerBase;
297293
} else {
298294
throw new RuntimeException(
299-
'The gmp or bcmath extension must be installed to read this database.'
295+
'The database offset is too large to be represented on your platform.'
300296
);
301297
}
302298

tests/MaxMind/Db/Test/Reader/DecoderTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class DecoderTest extends TestCase
159159
*/
160160
private function pointers(): array
161161
{
162-
return [
162+
$v = [
163163
['expected' => 0, 'input' => [0x20, 0x0]],
164164
['expected' => 5, 'input' => [0x20, 0x5]],
165165
['expected' => 10, 'input' => [0x20, 0xa]],
@@ -168,9 +168,14 @@ private function pointers(): array
168168
['expected' => 524283, 'input' => [0x2f, 0xf7, 0xfb]],
169169
['expected' => 526335, 'input' => [0x2f, 0xff, 0xff]],
170170
['expected' => 134217726, 'input' => [0x37, 0xf7, 0xf7, 0xfe]],
171-
['expected' => \PHP_INT_MAX < 4294967295 ? '2147483647' : 2147483647, 'input' => [0x38, 0x7f, 0xff, 0xff, 0xff]],
172-
['expected' => \PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0x38, 0xff, 0xff, 0xff, 0xff]],
171+
['expected' => 2147483647, 'input' => [0x38, 0x7f, 0xff, 0xff, 0xff]],
173172
];
173+
174+
if (\PHP_INT_MAX > 4294967295) {
175+
array_push($v, ['expected' => 4294967295, 'input' => [0x38, 0xff, 0xff, 0xff, 0xff]]);
176+
}
177+
178+
return $v;
174179
}
175180

176181
/**

0 commit comments

Comments
 (0)