Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions .github/workflows/test-bundled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ jobs:
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: ${{ matrix.php-version }}
# gmp so ReaderTest's uint128 boundary cases run rather than skip;
# they are gated on it, and uint128 is the branch this job is the
# only one to compile.
extensions: "mbstring, intl, gmp"
extensions: "mbstring, intl"
tools: "composer, phpize"

- name: Checkout
Expand Down Expand Up @@ -138,29 +135,14 @@ jobs:
- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

# Both preconditions are load-bearing, and neither failure is loud on its
# own.
#
# A failed `extension=` load is a warning, not a fatal, and PHP carries
# on. phpunit.xml.dist bootstraps vendor/autoload.php, whose PSR-4 map
# resolves MaxMind\Db\Reader to the pure-PHP implementation, and the
# suite is written to pass either way -- so an extension that does not
# load leaves every assertion above testing src/ rather than the object
# this job built.
#
# gmp is what turns ReaderTest's decimal uint128 case from
# markTestIncomplete into a real assertion, and markTestIncomplete does
# not fail a run. (The hex form at ReaderTest.php:162 is gated on
# extension_loaded('maxminddb'), not on gmp, so only half the uint128
# coverage depends on this -- but it is the half that checks the value
# rather than the format.)
- name: Check the extension and gmp are actually loaded
run: |
php -d extension="$PWD/ext/modules/maxminddb.so" -r \
'exit(extension_loaded("maxminddb") ? 0 : 1);' ||
{ echo "::error::the bundled extension did not load; PHPUnit would silently test the pure-PHP reader"; exit 1; }
php -r 'exit(extension_loaded("gmp") ? 0 : 1);' ||
{ echo "::error::gmp is not loaded; ReaderTest's uint128 value assertions would be skipped, not run"; exit 1; }
- name: Check the extension is actually loaded
run: php -d extension="$PWD/ext/modules/maxminddb.so" dev-bin/check-extensions.php maxminddb

- name: Test with phpunit using the bundled extension
run: php -d extension="$PWD/ext/modules/maxminddb.so" vendor/bin/phpunit
Expand Down Expand Up @@ -213,15 +195,31 @@ jobs:
runs-on: windows-latest
name: "Windows bundled build (x64, nts, PHP 8.4)"
steps:
# The action clones and initialises its own copy to build from; this
# checkout supplies dev-bin/verify-extension.php and the tests/data
# database the gate below queries.
# The action clones and initialises its own copy to build from. This
# checkout supplies the dev-bin scripts, the tests/data databases,
# composer.json and the PHPUnit suite that the steps below use.
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: recursive
persist-credentials: false

# The phpt files decode nothing, so the PHPUnit step below is the only
# run of the decoding tests against the DLL. See the bundled job above
# for why it matters. The runner's PHP only runs Composer. The tests use
# the downloaded php.exe, for the reason the gate step gives. These steps
# run before the build, so a failed install does not lose the build.
- name: Install PHP for Composer
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: '8.4'
tools: composer
env:
fail-fast: true

- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

# extension-url rather than the action's local mode, which cannot be used
# here: local mode selects itself by finding a config.w32 anywhere in the
# tree, but Get-ExtensionName then reads "config.w32" relative to the
Expand All @@ -233,11 +231,15 @@ jobs:
# One job is enough. config.w32 does not vary by PHP version, arch or
# thread safety in any way this exercises, and PHP <= 7.4 needs a
# toolchain windows-latest no longer carries.
#
# github.sha is the commit the checkout above uses. For a pull request,
# that is the merge commit, which the action can fetch through
# refs/pull/<n>/merge.
- name: Build the extension
uses: php/php-windows-builder/extension@29352c0ef9e8ce65264ea9e287881a6f7758a953 # 1.9.0
with:
extension-url: https://github.com/${{ github.repository }}
extension-ref: ${{ github.event.pull_request.head.sha || github.sha }}
extension-ref: ${{ github.sha }}
build-directory: winbuild
php-version: '8.4'
arch: x64
Expand All @@ -252,7 +254,8 @@ jobs:
#
# Uses the php.exe the build downloaded, not the runner's: version, arch,
# thread safety and toolset all have to match. Neither is on PATH in a
# later step, so both are located by search.
# later step, so both are located by search and saved for the PHPUnit
# step.
- name: Gate the built DLL
shell: pwsh
run: |
Expand All @@ -261,6 +264,8 @@ jobs:
Where-Object { $_.Directory.Name -eq 'php-bin' })
if ($dll.Count -ne 1) { throw "expected one php_maxminddb.dll, found $($dll.Count)" }
if ($php.Count -ne 1) { throw "expected one php-bin\php.exe, found $($php.Count)" }
"BUILD_PHP=$($php[0].FullName)" >> $env:GITHUB_ENV
"BUILD_DLL=$($dll[0].FullName)" >> $env:GITHUB_ENV

$ac = Get-Content ext/libmaxminddb/configure.ac -Raw
if ($ac -notmatch 'AC_INIT\(\[libmaxminddb\], \[([^\]]+)\]') {
Expand All @@ -275,3 +280,21 @@ jobs:
dev-bin/verify-extension.php `
tests/data/test-data/GeoIP2-City-Test.mmdb $version
if ($LASTEXITCODE -ne 0) { throw "the DLL did not load and query cleanly" }

# -n ignores the php.ini the build wrote, so extension_dir is set here.
# A failed extension= load is only a warning. Without the check, PHPUnit
# could pass with the pure-PHP reader.
- name: Test with phpunit using the DLL
shell: pwsh
run: |
$extDir = Join-Path (Split-Path $env:BUILD_PHP) ext
$phpArgs = @(
'-n',
'-d', "extension_dir=$extDir",
'-d', 'extension=mbstring',
'-d', "extension=$env:BUILD_DLL"
)
& $env:BUILD_PHP @phpArgs dev-bin/check-extensions.php maxminddb
if ($LASTEXITCODE -ne 0) { throw "the DLL did not load" }
& $env:BUILD_PHP @phpArgs vendor/bin/phpunit
if ($LASTEXITCODE -ne 0) { throw "PHPUnit failed" }
57 changes: 43 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,33 @@ permissions: {}

jobs:
run:
runs-on: ${{ matrix.runner }}
container: shivammathur/node:latest-${{ matrix.arch }}
runs-on: ${{ matrix.os }}
strategy:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The matrix keeps the default fail-fast: true. The new macOS and Windows jobs depend on Homebrew and the network. If one of them fails (for example, a temporary brew install failure, or a macOS-only -Werror failure from a new Xcode clang with --enable-maxminddb-debug), GitHub cancels all the Linux jobs that are still running, and their results are lost.

test-bundled.yml sets fail-fast: false for this reason. Should this matrix do the same?

🤖 Comment by Claude Opus 5.5.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to leave

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply from Claude Code (an AI agent) on behalf of the PR author.

OK, leaving fail-fast at the default.

matrix:
arch: ["amd64", "arm64v8"]
os: [ubuntu-latest, ubuntu-24.04-arm]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
php-version: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
php-extension: ['bcmath', 'gmp']
include:
- arch: amd64
runner: ubuntu-latest
- arch: arm64v8
runner: ubuntu-24.04-arm
# The runner's PHP loads gmp by default, and the decoder uses gmp
# when it is loaded. Disable gmp so that the bcmath path runs.
- php-extension: bcmath
disabled-extensions: ':gmp'
# Two PHP versions on each OS are enough to find platform problems.
- { os: macos-latest, php-version: '8.1', php-extension: gmp }
- { os: macos-latest, php-version: '8.5', php-extension: gmp }
- { os: windows-latest, php-version: '8.1', php-extension: gmp }
- { os: windows-latest, php-version: '8.5', php-extension: gmp }

name: "PHP ${{ matrix.php-version }} (with ${{ matrix.php-extension }}) test on ${{ matrix.runner }}"
name: "PHP ${{ matrix.php-version }} (with ${{ matrix.php-extension }}) test on ${{ matrix.os }}"
defaults:
run:
shell: bash
steps:
- name: Install PHP
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: ${{ matrix.php-version }}
extensions: "mbstring, intl, ${{ matrix.php-extension }}"
extensions: "mbstring, intl, ${{ matrix.php-extension }}, ${{ matrix.disabled-extensions }}"
tools: "composer, phpize"
Comment on lines +39 to 40

- name: Checkout
Expand All @@ -39,7 +46,16 @@ jobs:
submodules: true
persist-credentials: false

- name: Install libmaxminddb
# The decoder uses gmp if it is loaded, and bcmath otherwise. Check that
# each job uses the extension it names.
- name: Check the math extension
env:
MATH_EXTENSION: ${{ matrix.php-extension }}
DISABLED_EXTENSIONS: ${{ matrix.disabled-extensions }}
run: php dev-bin/check-extensions.php "$MATH_EXTENSION" $DISABLED_EXTENSIONS

- name: Install libmaxminddb from git
if: runner.os == 'Linux'
run: |
mkdir -p "$HOME/libmaxminddb"
git clone --recursive https://github.com/maxmind/libmaxminddb.git
Expand All @@ -48,11 +64,19 @@ jobs:
./configure --prefix="$HOME/libmaxminddb"
make
make install
echo "CFLAGS=-L$HOME/libmaxminddb/lib" >> "$GITHUB_ENV"
echo "CPPFLAGS=-I$HOME/libmaxminddb/include" >> "$GITHUB_ENV"

# configure finds the Homebrew libmaxminddb through pkg-config.
- name: Install libmaxminddb from Homebrew
if: runner.os == 'macOS'
run: brew install libmaxminddb pkgconf

# This job does not build the extension on Windows. test-bundled.yml
# tests the bundled DLL.
- name: Build extension
if: runner.os != 'Windows'
run: |
export CFLAGS="-L$HOME/libmaxminddb/lib"
export CPPFLAGS="-I$HOME/libmaxminddb/include"
cd ext
phpize
./configure --with-maxminddb --enable-maxminddb-debug
Expand All @@ -65,7 +89,12 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Test with phpunit without extension
run: vendor/bin/phpunit
run: php vendor/bin/phpunit

# make test runs the phpt suite with the PHP from php-config, so it does
# not show that the php on PATH can load the extension.
- name: Test with phpunit using extension
run: php -d extension=ext/modules/maxminddb.so vendor/bin/phpunit
if: runner.os != 'Windows'
run: |
php -d extension=ext/modules/maxminddb.so dev-bin/check-extensions.php maxminddb
php -d extension=ext/modules/maxminddb.so vendor/bin/phpunit
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ CHANGELOG
cannot throw.
* `MaxMind\Db\Reader\Decoder::decode()` and `MaxMind\Db\Reader\Util::read()`
declare their exceptions.
* On 64-bit Windows, the extension returned `uint32` and `uint64` values
from 2^31 to `PHP_INT_MAX` as strings. It now returns them as integers.

1.14.0 (2026-09-10)
-------------------
Expand Down
34 changes: 34 additions & 0 deletions dev-bin/check-extensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

// Check which extensions are loaded. A failed `extension=` load is only a
// warning, and the test suite passes without the C extension or gmp, so CI
// runs this before PHPUnit with the same PHP options.
//
// Usage: php [-d ...] check-extensions.php <name>...
//
// A name that starts with ":" must not be loaded. This is the syntax that
// setup-php uses to disable an extension.

if ($argc < 2) {
fwrite(\STDERR, "usage: check-extensions.php <name>...\n");

exit(1);
}

$failed = false;
foreach (array_slice($argv, 1) as $arg) {
if (substr($arg, 0, 1) === ':') {
$name = substr($arg, 1);
if (extension_loaded($name)) {
echo "::error::{$name} is loaded, but this job must not load it\n";
$failed = true;
}
} elseif (!extension_loaded($arg)) {
echo "::error::{$arg} is not loaded\n";
$failed = true;
}
}

exit($failed ? 1 : 0);
11 changes: 3 additions & 8 deletions ext/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,11 @@ static void handle_uint32(const MMDB_entry_data_list_s *entry_data_list,
zval *z_value TSRMLS_DC) {
uint32_t val = entry_data_list->entry_data.uint32;

#if LONG_MAX >= UINT32_MAX
#if ZEND_LONG_MAX >= UINT32_MAX
ZVAL_LONG(z_value, val);
return;
#else

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, this #else branch (return values above ZEND_LONG_MAX as strings) compiles only when zend_long is 32 bits, for example i386 or 32-bit Windows. No CI job builds 32-bit PHP.

Before this change, every Windows build compiled and ran this path, because LONG_MAX is 32 bits there. Now a regression here, such as a wrong format macro or a missing CHECK_ALLOCATED, would not be found. The change is correct. This is only a note about the coverage that we lose.

🤖 Comment by Claude Opus 5.5.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply from Claude Code (an AI agent) on behalf of the PR author.

Agreed. No job builds 32-bit PHP since 7095078 replaced the i386 jobs with arm64, so this is not changed here.

if (val <= LONG_MAX) {
if (val <= ZEND_LONG_MAX) {
ZVAL_LONG(z_value, val);
return;
}
Expand All @@ -550,11 +550,7 @@ static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,
zval *z_value TSRMLS_DC) {
uint64_t val = entry_data_list->entry_data.uint64;

#if LONG_MAX >= UINT64_MAX
ZVAL_LONG(z_value, val);
return;
#else
if (val <= LONG_MAX) {
if (val <= ZEND_LONG_MAX) {
ZVAL_LONG(z_value, val);
return;
}
Expand All @@ -565,7 +561,6 @@ static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,

ZVAL_STRING(z_value, int_str);
efree(int_str);
#endif
}

static void maxminddb_free_storage(free_obj_t *object TSRMLS_DC) {
Expand Down
36 changes: 11 additions & 25 deletions tests/MaxMind/Db/Test/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,10 @@ public function testDecoder(): void
$this->assertSame(268435456, $record['uint32']);
$this->assertSame(\PHP_INT_MAX > 1152921504606846976 ? 1152921504606846976 : '1152921504606846976', $record['uint64']);

$uint128 = $record['uint128'];

// For the C extension, which returns a hexadecimal
if (\extension_loaded('gmp')) {
$uint128 = gmp_strval($uint128);
} else {
$this->markTestIncomplete('Requires gmp extension to check value of uint128');
}

// The C extension returns uint128 values as hexadecimal strings.
$this->assertSame(
'1329227995784915872903807060280344576',
$uint128
\extension_loaded('maxminddb') ? '0x01000000000000000000000000000000' : '1329227995784915872903807060280344576',
$record['uint128']
);
}

Expand All @@ -99,13 +91,10 @@ public function testZeros(): void
$this->assertSame(0, $record['uint32']);
$this->assertSame('0', $record['uint64'] . '');

$uint128 = $record['uint128'];
if (\extension_loaded('gmp')) {
$uint128 = gmp_strval($uint128);
} else {
$this->markTestIncomplete('Requires gmp extension to check value of uint128');
}
$this->assertSame('0', $uint128);
$this->assertSame(
\extension_loaded('maxminddb') ? '0x00000000000000000000000000000000' : 0,
$record['uint128']
);
}

public function testMax(): void
Expand All @@ -120,13 +109,10 @@ public function testMax(): void
$this->assertSame(\PHP_INT_MAX < 0xFFFFFFFF ? '4294967295' : 0xFFFFFFFF, $record['uint32']);
$this->assertSame('18446744073709551615', $record['uint64'] . '');

$uint128 = $record['uint128'];
if (\extension_loaded('gmp')) {
$uint128 = gmp_strval($uint128);
} else {
$this->markTestIncomplete('Requires gmp extension to check value of uint128');
}
$this->assertSame('340282366920938463463374607431768211455', $uint128);
$this->assertSame(
\extension_loaded('maxminddb') ? '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' : '340282366920938463463374607431768211455',
$record['uint128']
);
}

public function testInvalidMetadataType(): void
Expand Down
Loading