[php-src] PHP-8.4: standard: Support numeric components of arbitrary length in `version_compare()` (#25536)

From: Date: Thu, 19 Nov 2026 09:48:34 +0000
Subject: [php-src] PHP-8.4: standard: Support numeric components of arbitrary length in `version_compare()` (#25536)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143869@lists.php.net to get a copy of this message
Author: Tim Düsterhus (TimWolla) Committer: GitHub (web-flow) Pusher: TimWolla Date: 2026-11-19T10:48:31+01:00 Commit: https://github.com/php/php-src/commit/362a21258496df07e2aeb537c08e4fa1dff4b4e7 Raw diff: https://github.com/php/php-src/commit/362a21258496df07e2aeb537c08e4fa1dff4b4e7.diff standard: Support numeric components of arbitrary length in version_compare() (#25536) This is a more general fix for php/php-src#25474 than the one used in de5c604302712362dc8d2f366a0b36066ed8dce6. Changed paths: M NEWS M ext/standard/tests/versioning/gh25474.phpt M ext/standard/tests/versioning/version_compare.phpt M ext/standard/versioning.c Diff: diff --git a/NEWS b/NEWS index 01788956353b..67008609a294 100644 --- a/NEWS +++ b/NEWS @@ -147,7 +147,7 @@ PHP NEWS - Standard: . Fixed bug GH-25474 (version_compare() treats large numeric parts as equal - on 64-bit Windows). (lazerg) + on 64-bit Windows). (lazerg, timwolla) . Fixed dns_get_record() returning partially built records for malformed DNS responses. (Marti2203) . Fixed bug GH-25387 (Uploaded files leak in request_parse_body()). diff --git a/ext/standard/tests/versioning/gh25474.phpt b/ext/standard/tests/versioning/gh25474.phpt index c055b69d88bf..7bc4182973c3 100644 --- a/ext/standard/tests/versioning/gh25474.phpt +++ b/ext/standard/tests/versioning/gh25474.phpt @@ -2,10 +2,6 @@ GH-25474 (version_compare() treats numeric parts > 2147483647 as equal on Windows) --CREDITS-- HFigarella ---SKIPIF-- -<?php -if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); -?> --FILE-- <?php var_dump(version_compare('2147483648', '2147483647')); diff --git a/ext/standard/tests/versioning/version_compare.phpt b/ext/standard/tests/versioning/version_compare.phpt index e22ddabfa56b..db7f199c18ed 100644 --- a/ext/standard/tests/versioning/version_compare.phpt +++ b/ext/standard/tests/versioning/version_compare.phpt @@ -24,6 +24,15 @@ foreach ($special_forms as $f1) { } test("1.2.", "1.2."); test("1.2..", "1.2.."); +test("1.2147483647", "1.2147483648"); +test("1.4294967295", "1.4294967296"); +test("1.9223372036854775807", "1.9223372036854775808"); +test("1.18446744073709551615", "1.18446744073709551616"); +test("1.99999999999999999999999999", "1.99999999999999999999999999"); +test("1.99999999999999999999999999", "1.999999999999999999999999999"); +test("1.99999999999999999999999998", "1.99999999999999999999999999"); +test("1.0001", "1.001"); +test("1.0001", "1.002"); print "TESTING OPERATORS\n"; foreach ($special_forms as $f1) { @@ -111,6 +120,15 @@ TESTING COMPARE 1.0pl1 = 1.0pl1 1.2. = 1.2. 1.2.. = 1.2.. +1.2147483647 < 1.2147483648 +1.4294967295 < 1.4294967296 +1.9223372036854775807 < 1.9223372036854775808 +1.18446744073709551615 < 1.18446744073709551616 +1.99999999999999999999999999 = 1.99999999999999999999999999 +1.99999999999999999999999999 < 1.999999999999999999999999999 +1.99999999999999999999999998 < 1.99999999999999999999999999 +1.0001 = 1.001 +1.0001 < 1.002 TESTING OPERATORS 1.0-dev lt 1.0-dev : false 1.0-dev < 1.0-dev : false diff --git a/ext/standard/versioning.c b/ext/standard/versioning.c index 4b3d4180096d..cf34d0d8f888 100644 --- a/ext/standard/versioning.c +++ b/ext/standard/versioning.c @@ -123,6 +123,26 @@ static int compare_special_version_forms(char *form1, char *form2) } /* }}} */ + +static int compare_numeric_version_forms(const char *num1, const char *num2) +{ + while (*num1 == '0') { + num1++; + } + while (*num2 == '0') { + num2++; + } + + size_t len1 = strspn(num1, "0123456789"); + size_t len2 = strspn(num2, "0123456789"); + + if (len1 != len2) { + return ZEND_THREEWAY_COMPARE(len1, len2); + } + + return ZEND_NORMALIZE_BOOL(memcmp(num1, num2, len1)); +} + /* {{{ php_version_compare() */ PHPAPI int @@ -131,7 +151,6 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2) char *ver1; char *ver2; char *p1, *p2, *n1, *n2; - zend_long l1, l2; int compare = 0; if (!*orig_ver1 || !*orig_ver2) { @@ -162,9 +181,7 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2) } if (isdigit((unsigned char)*p1) && isdigit((unsigned char)*p2)) { /* compare element numerically */ - l1 = ZEND_STRTOL(p1, NULL, 10); - l2 = ZEND_STRTOL(p2, NULL, 10); - compare = ZEND_NORMALIZE_BOOL(l1 - l2); + compare = compare_numeric_version_forms(p1, p2); } else if (!isdigit((unsigned char)*p1) && !isdigit((unsigned char)*p2)) { /* compare element names */ compare = compare_special_version_forms(p1, p2);

« previous php.cvs (#143869) next »