From 87be38a570eadf7d29750e4dc1e0aa4a02780e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 27 Nov 2026 07:18:05 +0100 Subject: [PATCH 1/3] ext/uri: Optimize GHSA-mg3p-mxj9-g2mm tests Changing the input URI to a relative path reference makes the tests much faster, because uriparser only increases a pointer for each character while parsing the beginning of a URI while it's not yet known if the current component is a scheme or a path. On the other hand, uriparser calls a `URI_FUNC(ParsePchar)` function for each character while parsing the path after a "/" character (in case of absolute URIs, or absolute path references). Additionally, the strspn() call is replaced with strcmp() because strspn() was also slow. I tested the performance of my changes on Mac OS with ASAN, and the execution time of the first two tests decreased by ~40%, while GHSA-mg3p-mxj9-g2mm-3.phpt decreased by ~70%. --- .../tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt | 3 ++- .../tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt | 3 ++- .../tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt | 10 ++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt index 57e9e9947d40..28cd3920e2cc 100644 --- a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt @@ -16,7 +16,8 @@ all memory_limit=-1 --FILE-- toRawString(); diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt index 60755a914dfb..47c8c6132961 100644 --- a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt @@ -16,7 +16,8 @@ all memory_limit=-1 --FILE-- toRawString(); diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt index 199515635b11..748eb094088b 100644 --- a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt @@ -16,14 +16,12 @@ all memory_limit=-1 --FILE-- toRawString(); +$length = 2147483646; // 32-bit INT_MAX - 1 +// Parsing a single relative path segment is cheap under sanitizers. +$string = (new Uri\Rfc3986\Uri(str_repeat("x", $length)))->toRawString(); var_dump(strlen($string)); -var_dump(substr($string, 0, 8)); -var_dump(strspn($string, "x", 6) === 2147483646 - 6); +var_dump(strcmp($string, str_repeat("x", $length)) === 0); ?> --EXPECT-- int(2147483646) -string(8) "a://b/xx" bool(true) From d32a3c15c26208543ae8ad5f6b04dbc66cfa10d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 27 Nov 2026 13:22:57 +0100 Subject: [PATCH 2/3] Update GHSA-mg3p-mxj9-g2mm-3.phpt --- ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt index 748eb094088b..54adc9a655ae 100644 --- a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt @@ -16,11 +16,16 @@ all memory_limit=-1 --FILE-- toRawString(); +$uri = new Uri\Rfc3986\Uri(str_repeat("x", $length)); + +$string = $uri->toRawString(); + var_dump(strlen($string)); -var_dump(strcmp($string, str_repeat("x", $length)) === 0); +var_dump($string === str_repeat("x", $length)); ?> --EXPECT-- int(2147483646) From 3954564149402721ce1711c0580aa1589142b164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 27 Nov 2026 13:42:17 +0100 Subject: [PATCH 3/3] Fix peak memory usage regression --- ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt index 54adc9a655ae..ecfe6cb2db73 100644 --- a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt @@ -20,9 +20,7 @@ memory_limit=-1 $length = 2147483646; // 32-bit INT_MAX - 1 // Parsing a single relative path segment is cheap under sanitizers. -$uri = new Uri\Rfc3986\Uri(str_repeat("x", $length)); - -$string = $uri->toRawString(); +$string = (new Uri\Rfc3986\Uri(str_repeat("x", $length)))->toRawString(); var_dump(strlen($string)); var_dump($string === str_repeat("x", $length));