diff --git a/.github/workflows/test-bundled.yml b/.github/workflows/test-bundled.yml index e23bd30..c313416 100644 --- a/.github/workflows/test-bundled.yml +++ b/.github/workflows/test-bundled.yml @@ -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 @@ -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 @@ -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 @@ -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//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 @@ -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: | @@ -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\], \[([^\]]+)\]') { @@ -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" } diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 27425dc..85489ee 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,26 +10,33 @@ permissions: {} jobs: run: - runs-on: ${{ matrix.runner }} - container: shivammathur/node:latest-${{ matrix.arch }} + runs-on: ${{ matrix.os }} strategy: matrix: - arch: ["amd64", "arm64v8"] + os: [ubuntu-latest, ubuntu-24.04-arm] 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" - name: Checkout @@ -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 @@ -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 @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e47a6c..38aa3c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------- diff --git a/dev-bin/check-extensions.php b/dev-bin/check-extensions.php new file mode 100644 index 0000000..b6616cc --- /dev/null +++ b/dev-bin/check-extensions.php @@ -0,0 +1,34 @@ +... +// +// 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 ...\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); diff --git a/ext/maxminddb.c b/ext/maxminddb.c index 5c5d35b..cf4226e 100644 --- a/ext/maxminddb.c +++ b/ext/maxminddb.c @@ -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 - if (val <= LONG_MAX) { + if (val <= ZEND_LONG_MAX) { ZVAL_LONG(z_value, val); return; } @@ -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; } @@ -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) { diff --git a/tests/MaxMind/Db/Test/ReaderTest.php b/tests/MaxMind/Db/Test/ReaderTest.php index 1430d51..665a7ed 100644 --- a/tests/MaxMind/Db/Test/ReaderTest.php +++ b/tests/MaxMind/Db/Test/ReaderTest.php @@ -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'] ); } @@ -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 @@ -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