From ae92b85572ac9a5f14103cdedbf6448f9e8db09d Mon Sep 17 00:00:00 2001 From: Peter Kokot Date: Wed, 14 Feb 2024 13:52:01 +0100 Subject: [PATCH 01/44] Fix linking ext/curl against OpenSSL (#13262) This is backport for 8.3 of b222c020bfa876ae1cea87406beb8af0b53f0fab that originally targeted only 8.4+. This is however a bug fix. Following 68f6ab711323678382d2746e57358d3f57a3446b, the ext/curl doesn't need to be linked against OpenSSL anymore, if curl_version_info_data ssl_version is OpenSSL/1.1 or later. With OpenSSL 3 and later the check for old SSL crypto locking callbacks was detected here. This also uses a common PHP_SETUP_OPENSSL macro for checking OpenSSL and syncs the minimum OpenSSL version (currently 1.0.2 or later) across the PHP build system. --- NEWS | 1 + ext/curl/config.m4 | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 6867a3dbcaeb8..5eb1694b5e816 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ PHP NEWS - Curl: . Fix memory leak when setting a list via curl_setopt fails. (nielsdos) + . Fix incorrect OpenSSL version detection. (Peter Kokot) - Date: . Fix leaks with multiple calls to DatePeriod iterator current(). (nielsdos) diff --git a/ext/curl/config.m4 b/ext/curl/config.m4 index 3b11739654bd6..c0325f990ad11 100644 --- a/ext/curl/config.m4 +++ b/ext/curl/config.m4 @@ -28,6 +28,7 @@ if test "$PHP_CURL" != "no"; then AC_MSG_CHECKING([for libcurl linked against old openssl]) AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#include #include #include @@ -39,9 +40,18 @@ int main(int argc, char *argv[]) const char *ptr = data->ssl_version; while(*ptr == ' ') ++ptr; - if (strncasecmp(ptr, "OpenSSL/1.1", sizeof("OpenSSL/1.1")-1) == 0) { - /* New OpenSSL version */ - return 3; + int major, minor; + if (sscanf(ptr, "OpenSSL/%d", &major) == 1) { + if (major >= 3) { + /* OpenSSL version 3 or later */ + return 4; + } + } + if (sscanf(ptr, "OpenSSL/%d.%d", &major, &minor) == 2) { + if (major > 1 || (major == 1 && minor >= 1)) { + /* OpenSSL version 1.1 or later */ + return 3; + } } if (strncasecmp(ptr, "OpenSSL", sizeof("OpenSSL")-1) == 0) { /* Old OpenSSL version */ @@ -56,11 +66,7 @@ int main(int argc, char *argv[]) ]])],[ AC_MSG_RESULT([yes]) AC_DEFINE([HAVE_CURL_OLD_OPENSSL], [1], [Have cURL with old OpenSSL]) - PKG_CHECK_MODULES([OPENSSL], [openssl], [ - PHP_EVAL_LIBLINE($OPENSSL_LIBS, CURL_SHARED_LIBADD) - PHP_EVAL_INCLINE($OPENSSL_CFLAGS) - AC_CHECK_HEADERS([openssl/crypto.h]) - ], []) + PHP_SETUP_OPENSSL(CURL_SHARED_LIBADD,[AC_CHECK_HEADERS([openssl/crypto.h])],[]) ], [ AC_MSG_RESULT([no]) ], [ From 01abca98525c3ac4196ea8dd22d8dd89b325e5eb Mon Sep 17 00:00:00 2001 From: NickSdot <32384907+NickSdot@users.noreply.github.com> Date: Fri, 6 Jun 2025 20:25:19 +0800 Subject: [PATCH 02/44] [skip ci ] fix: deleted stray semicolon (GH-18782) --- scripts/dev/search_underscores.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dev/search_underscores.php b/scripts/dev/search_underscores.php index 4f2f8e8eebc9a..42e4c876e0541 100755 --- a/scripts/dev/search_underscores.php +++ b/scripts/dev/search_underscores.php @@ -45,7 +45,7 @@ if (strpos($c, "_") !== false) { $err++; $ref = new ReflectionClass($c); - if (!($ext = $ref->getExtensionName())) {; + if (!($ext = $ref->getExtensionName())) { $ext = $ref->isInternal() ? "" : ""; } if (!array_key_exists($ext, $extensions)) { From cce0efdff87de3cea3e2326cdb36c8adc58a4bdb Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 6 Jun 2025 14:45:59 +0100 Subject: [PATCH 03/44] Revert "ext/pdo_pgsql: Delete unused constants" This reverts commit e549ccb32eae11c5affd3bb288e57f1cc0295436. --- NEWS | 1 - UPGRADING | 3 --- ext/pdo_pgsql/pdo_pgsql.c | 5 +++++ ext/pdo_pgsql/pdo_pgsql.stub.php | 15 +++++++++++++++ ext/pdo_pgsql/pdo_pgsql_arginfo.h | 32 ++++++++++++++++++++++++++++++- ext/pdo_pgsql/php_pdo_pgsql_int.h | 8 ++++++++ 6 files changed, 59 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 9e1944e1fba38..88ba1f1e5bcc2 100644 --- a/NEWS +++ b/NEWS @@ -137,7 +137,6 @@ PHP NEWS . Implement GH-15387 Pdo\Pgsql::setAttribute(PDO::ATTR_PREFETCH, 0) or Pdo\Pgsql::prepare(…, [ PDO::ATTR_PREFETCH => 0 ]) make fetch() lazy instead of storing the whole result set in memory (Guillaume Outters) - . Removed unused constants Pdo\Pgsql::TRANSACTION_* (vrana). - PDO_SQLITE: . throw on null bytes / resolve GH-13952 (divinity76). diff --git a/UPGRADING b/UPGRADING index 1f55fa8ce8946..0a639508ef696 100644 --- a/UPGRADING +++ b/UPGRADING @@ -101,9 +101,6 @@ PHP 8.5 UPGRADE NOTES . A ValueError is now thrown when trying to set a cursor name that is too long on a PDOStatement resulting from the Firebird driver. -- PDO_PGSQL: - . Removed unused constants Pdo\Pgsql::TRANSACTION_*. - - Session: . Attempting to write session data where $_SESSION has a key containing the pipe character will now emit a warning instead of silently failing. diff --git a/ext/pdo_pgsql/pdo_pgsql.c b/ext/pdo_pgsql/pdo_pgsql.c index af9dcd0ec4102..49efa0b238484 100644 --- a/ext/pdo_pgsql/pdo_pgsql.c +++ b/ext/pdo_pgsql/pdo_pgsql.c @@ -179,6 +179,11 @@ PHP_METHOD(Pdo_Pgsql, setNoticeCallback) PHP_MINIT_FUNCTION(pdo_pgsql) { REGISTER_PDO_CLASS_CONST_LONG("PGSQL_ATTR_DISABLE_PREPARES", PDO_PGSQL_ATTR_DISABLE_PREPARES); + REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_IDLE", (zend_long)PGSQL_TRANSACTION_IDLE); + REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_ACTIVE", (zend_long)PGSQL_TRANSACTION_ACTIVE); + REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INTRANS", (zend_long)PGSQL_TRANSACTION_INTRANS); + REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INERROR", (zend_long)PGSQL_TRANSACTION_INERROR); + REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_UNKNOWN", (zend_long)PGSQL_TRANSACTION_UNKNOWN); PdoPgsql_ce = register_class_Pdo_Pgsql(pdo_dbh_ce); PdoPgsql_ce->create_object = pdo_dbh_new; diff --git a/ext/pdo_pgsql/pdo_pgsql.stub.php b/ext/pdo_pgsql/pdo_pgsql.stub.php index 93415369c625d..8e96a55266824 100644 --- a/ext/pdo_pgsql/pdo_pgsql.stub.php +++ b/ext/pdo_pgsql/pdo_pgsql.stub.php @@ -18,6 +18,21 @@ class Pgsql extends \PDO public const int ATTR_RESULT_MEMORY_SIZE = UNKNOWN; #endif + /** @cvalue PGSQL_TRANSACTION_IDLE */ + public const int TRANSACTION_IDLE = UNKNOWN; + + /** @cvalue PGSQL_TRANSACTION_ACTIVE */ + public const int TRANSACTION_ACTIVE = UNKNOWN; + + /** @cvalue PGSQL_TRANSACTION_INTRANS */ + public const int TRANSACTION_INTRANS = UNKNOWN; + + /** @cvalue PGSQL_TRANSACTION_INERROR */ + public const int TRANSACTION_INERROR = UNKNOWN; + + /** @cvalue PGSQL_TRANSACTION_UNKNOWN */ + public const int TRANSACTION_UNKNOWN = UNKNOWN; + public function escapeIdentifier(string $input): string {} public function copyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = "\\\\N", ?string $fields = null): bool {} diff --git a/ext/pdo_pgsql/pdo_pgsql_arginfo.h b/ext/pdo_pgsql/pdo_pgsql_arginfo.h index 8efdfe53a0d16..f7f54cb600c72 100644 --- a/ext/pdo_pgsql/pdo_pgsql_arginfo.h +++ b/ext/pdo_pgsql/pdo_pgsql_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 81399a3d342a9327733f86f6ab733bb317a4599e */ + * Stub hash: 225cbb077d441f93b7c6bdb9826ab3e8f634b79d */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Pdo_Pgsql_escapeIdentifier, 0, 1, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, input, IS_STRING, 0) @@ -102,5 +102,35 @@ static zend_class_entry *register_class_Pdo_Pgsql(zend_class_entry *class_entry_ zend_string_release(const_ATTR_RESULT_MEMORY_SIZE_name); #endif + zval const_TRANSACTION_IDLE_value; + ZVAL_LONG(&const_TRANSACTION_IDLE_value, PGSQL_TRANSACTION_IDLE); + zend_string *const_TRANSACTION_IDLE_name = zend_string_init_interned("TRANSACTION_IDLE", sizeof("TRANSACTION_IDLE") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_TRANSACTION_IDLE_name, &const_TRANSACTION_IDLE_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_TRANSACTION_IDLE_name); + + zval const_TRANSACTION_ACTIVE_value; + ZVAL_LONG(&const_TRANSACTION_ACTIVE_value, PGSQL_TRANSACTION_ACTIVE); + zend_string *const_TRANSACTION_ACTIVE_name = zend_string_init_interned("TRANSACTION_ACTIVE", sizeof("TRANSACTION_ACTIVE") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_TRANSACTION_ACTIVE_name, &const_TRANSACTION_ACTIVE_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_TRANSACTION_ACTIVE_name); + + zval const_TRANSACTION_INTRANS_value; + ZVAL_LONG(&const_TRANSACTION_INTRANS_value, PGSQL_TRANSACTION_INTRANS); + zend_string *const_TRANSACTION_INTRANS_name = zend_string_init_interned("TRANSACTION_INTRANS", sizeof("TRANSACTION_INTRANS") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_TRANSACTION_INTRANS_name, &const_TRANSACTION_INTRANS_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_TRANSACTION_INTRANS_name); + + zval const_TRANSACTION_INERROR_value; + ZVAL_LONG(&const_TRANSACTION_INERROR_value, PGSQL_TRANSACTION_INERROR); + zend_string *const_TRANSACTION_INERROR_name = zend_string_init_interned("TRANSACTION_INERROR", sizeof("TRANSACTION_INERROR") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_TRANSACTION_INERROR_name, &const_TRANSACTION_INERROR_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_TRANSACTION_INERROR_name); + + zval const_TRANSACTION_UNKNOWN_value; + ZVAL_LONG(&const_TRANSACTION_UNKNOWN_value, PGSQL_TRANSACTION_UNKNOWN); + zend_string *const_TRANSACTION_UNKNOWN_name = zend_string_init_interned("TRANSACTION_UNKNOWN", sizeof("TRANSACTION_UNKNOWN") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_TRANSACTION_UNKNOWN_name, &const_TRANSACTION_UNKNOWN_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_TRANSACTION_UNKNOWN_name); + return class_entry; } diff --git a/ext/pdo_pgsql/php_pdo_pgsql_int.h b/ext/pdo_pgsql/php_pdo_pgsql_int.h index cb07a10de16f0..881b4e7046504 100644 --- a/ext/pdo_pgsql/php_pdo_pgsql_int.h +++ b/ext/pdo_pgsql/php_pdo_pgsql_int.h @@ -104,6 +104,14 @@ struct pdo_pgsql_lob_self { Oid oid; }; +enum pdo_pgsql_specific_constants { + PGSQL_TRANSACTION_IDLE = PQTRANS_IDLE, + PGSQL_TRANSACTION_ACTIVE = PQTRANS_ACTIVE, + PGSQL_TRANSACTION_INTRANS = PQTRANS_INTRANS, + PGSQL_TRANSACTION_INERROR = PQTRANS_INERROR, + PGSQL_TRANSACTION_UNKNOWN = PQTRANS_UNKNOWN +}; + php_stream *pdo_pgsql_create_lob_stream(zend_object *pdh, int lfd, Oid oid); extern const php_stream_ops pdo_pgsql_lob_stream_ops; From d15c61e84cad4a274cf748d01d0599730b951663 Mon Sep 17 00:00:00 2001 From: NickSdot <32384907+NickSdot@users.noreply.github.com> Date: Sat, 7 Jun 2025 14:31:51 +0800 Subject: [PATCH 04/44] ext/gettext: fixed typo in config.m4 (#18790) --- ext/gettext/config.m4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/gettext/config.m4 b/ext/gettext/config.m4 index ae3eaf38761a1..6235658987e5a 100644 --- a/ext/gettext/config.m4 +++ b/ext/gettext/config.m4 @@ -27,7 +27,7 @@ if test "$PHP_GETTEXT" != "no"; then dnl If libintl.h is provided by libc, it's possible that libc is musl. dnl The gettext family of functions under musl ignores the codeset dnl suffix on directories like "en_US.UTF-8"; instead they look only - dnl in "en_US". To accomodate that, we symlink some test data from one + dnl in "en_US". To accommodate that, we symlink some test data from one dnl to the other. AC_MSG_NOTICE([symlinking en_US.UTF-8 messages to en_US in case you are on musl]) _linkdest="${srcdir%/}"/ext/gettext/tests/locale/en_US From 1044558b6484d21873039cad04961013f49d08b7 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 7 Jun 2025 13:31:55 +0100 Subject: [PATCH 05/44] ext/pdo_sqlite: createCollation memory leaks fix. coming from callback arguments when its return type is incorrect. close GH-18796 --- NEWS | 4 ++++ ext/pdo_sqlite/pdo_sqlite.c | 6 ++--- ...sqlite_createcollation_wrong_callback.phpt | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt diff --git a/NEWS b/NEWS index 4c70733379d5c..8f4ae644a9299 100644 --- a/NEWS +++ b/NEWS @@ -37,6 +37,10 @@ PHP NEWS . Fixed bug #74796 (Requests through http proxy set peer name). (Jakub Zelenka) +- PDO Sqlite: + . Fixed memory leak with Pdo_Sqlite::createCollation when the callback + has an incorrect return type. (David Carlier) + - Phar: . Add missing filter cleanups on phar failure. (nielsdos) . Fixed bug GH-18642 (Signed integer overflow in ext/phar fseek). (nielsdos) diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c index ff56d04049424..493ba3f36009d 100644 --- a/ext/pdo_sqlite/pdo_sqlite.c +++ b/ext/pdo_sqlite/pdo_sqlite.c @@ -346,6 +346,9 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL); + zval_ptr_dtor(&zargs[0]); + zval_ptr_dtor(&zargs[1]); + if (!Z_ISUNDEF(retval)) { if (Z_TYPE(retval) != IS_LONG) { zend_string *func_name = get_active_function_or_method_name(); @@ -362,9 +365,6 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v } } - zval_ptr_dtor(&zargs[0]); - zval_ptr_dtor(&zargs[1]); - return ret; } diff --git a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt new file mode 100644 index 0000000000000..a9d17bb230d56 --- /dev/null +++ b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt @@ -0,0 +1,24 @@ +--TEST-- +Pdo\Sqlite::createCollation() memory leaks on wrong callback return type +--EXTENSIONS-- +pdo_sqlite +--FILE-- +exec("CREATE TABLE test (c string)"); +$db->exec("INSERT INTO test VALUES('youwontseeme')"); +$db->exec("INSERT INTO test VALUES('neither')"); +$db->createCollation('NAT', function($a, $b): string { return $a . $b; }); + +try { + $db->query("SELECT c FROM test ORDER BY c COLLATE NAT"); +} catch (\TypeError $e) { + echo $e->getMessage(), PHP_EOL; +} +?> +--EXPECT-- +PDO::query(): Return value of the callback must be of type int, string returned From ceffa70b9713dec0dad3d07faeceb7b7e508b006 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Sat, 7 Jun 2025 15:53:46 +0100 Subject: [PATCH 06/44] ext/pdo_sqlite: Fix GH-18796 test exception message. (#18798) --- .../subclasses/pdo_sqlite_createcollation_wrong_callback.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt index a9d17bb230d56..2a493c2117972 100644 --- a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt +++ b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_createcollation_wrong_callback.phpt @@ -21,4 +21,4 @@ try { } ?> --EXPECT-- -PDO::query(): Return value of the callback must be of type int, string returned +PDO::query(): Return value of the collation callback must be of type int, string returned From cb04226b4aa2960ed22cbd09e5fe826f69aca7b0 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 8 Jun 2025 11:23:31 +0200 Subject: [PATCH 07/44] Avoid making a redundant copy in php_filter_callback() (#18794) `call_user_function` already makes a copy to the call frame for its arguments, there's no need to do this ourselves. --- ext/filter/callback_filter.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ext/filter/callback_filter.c b/ext/filter/callback_filter.c index 067f16a03ea24..b6d57739b2b9b 100644 --- a/ext/filter/callback_filter.c +++ b/ext/filter/callback_filter.c @@ -19,7 +19,6 @@ void php_filter_callback(PHP_INPUT_FILTER_PARAM_DECL) { zval retval; - zval args[1]; int status; if (!option_array || !zend_is_callable(option_array, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL)) { @@ -29,8 +28,7 @@ void php_filter_callback(PHP_INPUT_FILTER_PARAM_DECL) return; } - ZVAL_COPY(&args[0], value); - status = call_user_function(NULL, NULL, option_array, &retval, 1, args); + status = call_user_function(NULL, NULL, option_array, &retval, 1, value); if (status == SUCCESS && !Z_ISUNDEF(retval)) { zval_ptr_dtor(value); @@ -39,6 +37,4 @@ void php_filter_callback(PHP_INPUT_FILTER_PARAM_DECL) zval_ptr_dtor(value); ZVAL_NULL(value); } - - zval_ptr_dtor(&args[0]); } From eac91d0453a913e2862ce948b458eb300185370f Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 8 Jun 2025 13:25:56 +0200 Subject: [PATCH 08/44] [ci skip] Fix UPGRADING formatting --- UPGRADING | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADING b/UPGRADING index 0a639508ef696..7cb73a1271d2f 100644 --- a/UPGRADING +++ b/UPGRADING @@ -552,7 +552,7 @@ PHP 8.5 UPGRADE NOTES . Now avoids creating extra string copies when converting strings for use in the collator. -MBString: +- MBString: . The parts of the code that used SSE2 have been adapted to use SIMD with ARM NEON as well. From e3cfa4bcae8b476e9d0192f938de579caed9f6a0 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 7 Jun 2025 16:35:30 +0100 Subject: [PATCH 09/44] ext/pdo_sqlite: PDO::sqliteCreateCollection return type strenghtening. Is supposed to be Pdo_Sqlite::createCollation but behavior differs in regard of return type checks. close GH-18799 --- NEWS | 2 ++ UPGRADING | 3 +++ ext/pdo_sqlite/pdo_sqlite.c | 4 ++-- ext/pdo_sqlite/sqlite_driver.c | 12 ++++++++---- ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt | 8 ++++++++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 88ba1f1e5bcc2..9e6d47ed2a003 100644 --- a/NEWS +++ b/NEWS @@ -141,6 +141,8 @@ PHP NEWS - PDO_SQLITE: . throw on null bytes / resolve GH-13952 (divinity76). . Implement GH-17321: Add setAuthorizer to Pdo\Sqlite. (nielsdos) + . PDO::sqliteCreateCollation now throws a TypeError if the callback + has a wrong return type. (David Carlier) - PGSQL: . Added pg_close_stmt to close a prepared statement while allowing diff --git a/UPGRADING b/UPGRADING index 7cb73a1271d2f..aebda12cf500e 100644 --- a/UPGRADING +++ b/UPGRADING @@ -268,6 +268,9 @@ PHP 8.5 UPGRADE NOTES - PDO_SQLITE: . SQLite PDO::quote() will now throw an exception or emit a warning, depending on the error mode, if the string contains a null byte. + . PDO::sqliteCreateCollation will now throw an exception + if the callback has the wrong return type, making it more + in line with Pdo_Sqlite::createCollation behavior. - PGSQL: . pg_copy_from also supports inputs as Iterable. diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c index 02fbf0d3e9ecc..fbbb336c1af1e 100644 --- a/ext/pdo_sqlite/pdo_sqlite.c +++ b/ext/pdo_sqlite/pdo_sqlite.c @@ -385,14 +385,14 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned", ZSTR_VAL(func_name), zend_zval_value_name(&retval)); zend_string_release(func_name); - zval_ptr_dtor(&retval); - return FAILURE; + ret = FAILURE; } if (Z_LVAL(retval) > 0) { ret = 1; } else if (Z_LVAL(retval) < 0) { ret = -1; } + zval_ptr_dtor(&retval); } return ret; diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index ddf25d4965f06..d06d255c14cd8 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -483,9 +483,16 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL); + zval_ptr_dtor(&zargs[0]); + zval_ptr_dtor(&zargs[1]); + if (!Z_ISUNDEF(retval)) { if (Z_TYPE(retval) != IS_LONG) { - convert_to_long(&retval); + zend_string *func_name = get_active_function_or_method_name(); + zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned", + ZSTR_VAL(func_name), zend_zval_value_name(&retval)); + zend_string_release(func_name); + ret = FAILURE; } if (Z_LVAL(retval) > 0) { ret = 1; @@ -495,9 +502,6 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const zval_ptr_dtor(&retval); } - zval_ptr_dtor(&zargs[0]); - zval_ptr_dtor(&zargs[1]); - return ret; } diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt index 9e4751e33aa01..fdfb8dda448fd 100644 --- a/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt +++ b/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt @@ -24,6 +24,13 @@ foreach ($result as $row) { echo $row['name'] . "\n"; } +$db->sqliteCreateCollation('MYCOLLATEBAD', function($a, $b) { return $a; }); + +try { + $db->query('SELECT name FROM test_pdo_sqlite_createcollation ORDER BY name COLLATE MYCOLLATEBAD'); +} catch (\TypeError $e) { + echo $e->getMessage(), PHP_EOL; +} ?> --EXPECT-- 1 @@ -32,3 +39,4 @@ foreach ($result as $row) { 1 10 2 +PDO::query(): Return value of the collation callback must be of type int, string returned From 1a18012be3c109457945980a5341546a16a6ac8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 9 Jun 2025 09:34:49 +0200 Subject: [PATCH 10/44] zend_vm_gen: Fix GET_OP*_OBJ_ZVAL_PTR_DEREF for ANY (#18746) Fixes php/php-src#18745 --- Zend/zend_execute.c | 9 +++++++++ Zend/zend_vm_gen.php | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 0fbfdfa07ef04..5d8d9f4caeb86 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -126,6 +126,7 @@ typedef int (ZEND_FASTCALL *incdec_t)(zval *); #define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) #define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) #define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_obj_zval_ptr_deref(op_type, node, type) _get_obj_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) #define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) #define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) @@ -537,6 +538,14 @@ static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_o return get_zval_ptr(op_type, op, type); } +static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_deref(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC) +{ + if (op_type == IS_UNUSED) { + return &EX(This); + } + return get_zval_ptr_deref(op_type, op, type); +} + static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type == IS_UNUSED) { diff --git a/Zend/zend_vm_gen.php b/Zend/zend_vm_gen.php index 5a4a31b60b8d3..dbd7da0430f1c 100755 --- a/Zend/zend_vm_gen.php +++ b/Zend/zend_vm_gen.php @@ -363,7 +363,7 @@ ); $op1_get_obj_zval_ptr_deref = array( - "ANY" => "get_obj_zval_ptr(opline->op1_type, opline->op1, \\1)", + "ANY" => "get_obj_zval_ptr_deref(opline->op1_type, opline->op1, \\1)", "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)", "VAR" => "_get_zval_ptr_var_deref(opline->op1.var EXECUTE_DATA_CC)", "CONST" => "RT_CONSTANT(opline, opline->op1)", @@ -374,7 +374,7 @@ ); $op2_get_obj_zval_ptr_deref = array( - "ANY" => "get_obj_zval_ptr(opline->op2_type, opline->op2, \\1)", + "ANY" => "get_obj_zval_ptr_deref(opline->op2_type, opline->op2, \\1)", "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)", "VAR" => "_get_zval_ptr_var_deref(opline->op2.var EXECUTE_DATA_CC)", "CONST" => "RT_CONSTANT(opline, opline->op2)", From 31b4f39d3edbfe1862e1559ae1f217c06a6e5ed4 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 7 Jun 2025 14:23:35 +0200 Subject: [PATCH 11/44] Use ZVAL_NEW_STR() for new string in php_filter_encode_html() --- ext/filter/sanitizing_filters.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/filter/sanitizing_filters.c b/ext/filter/sanitizing_filters.c index 7f8b4948d5818..d619bb0fc13a7 100644 --- a/ext/filter/sanitizing_filters.c +++ b/ext/filter/sanitizing_filters.c @@ -49,7 +49,7 @@ static void php_filter_encode_html(zval *value, const unsigned char *chars) } zval_ptr_dtor(value); - ZVAL_STR(value, smart_str_extract(&str)); + ZVAL_NEW_STR(value, smart_str_extract(&str)); } static const unsigned char hexchars[] = "0123456789ABCDEF"; From c02f6fb3feb77ca868dab2ba47702f145a7f030d Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 7 Jun 2025 14:28:44 +0200 Subject: [PATCH 12/44] Output blocks of safe chars in php_filter_encode_html() Fixes a long-standing TODO, and is faster. --- ext/filter/sanitizing_filters.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/filter/sanitizing_filters.c b/ext/filter/sanitizing_filters.c index d619bb0fc13a7..ebc20e47711db 100644 --- a/ext/filter/sanitizing_filters.c +++ b/ext/filter/sanitizing_filters.c @@ -31,6 +31,7 @@ static void php_filter_encode_html(zval *value, const unsigned char *chars) size_t len = Z_STRLEN_P(value); unsigned char *s = (unsigned char *)Z_STRVAL_P(value); unsigned char *e = s + len; + unsigned char *last_output = s; if (Z_STRLEN_P(value) == 0) { return; @@ -38,16 +39,17 @@ static void php_filter_encode_html(zval *value, const unsigned char *chars) while (s < e) { if (chars[*s]) { + smart_str_appendl(&str, (const char *) last_output, s - last_output); smart_str_appendl(&str, "&#", 2); smart_str_append_unsigned(&str, (zend_ulong)*s); smart_str_appendc(&str, ';'); - } else { - /* XXX: this needs to be optimized to work with blocks of 'safe' chars */ - smart_str_appendc(&str, *s); + last_output = s + 1; } s++; } + smart_str_appendl(&str, (const char *) last_output, s - last_output); + zval_ptr_dtor(value); ZVAL_NEW_STR(value, smart_str_extract(&str)); } From 4852a2c5cc71907e56a3d53058ddcbfa2b365a6b Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:13:46 +0200 Subject: [PATCH 13/44] pdo_dblib: Use stack local array instead of heap allocation (#18801) --- ext/pdo_dblib/dblib_driver.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ext/pdo_dblib/dblib_driver.c b/ext/pdo_dblib/dblib_driver.c index 0055dcb03b3c5..132e5c2e4dee1 100644 --- a/ext/pdo_dblib/dblib_driver.c +++ b/ext/pdo_dblib/dblib_driver.c @@ -233,9 +233,8 @@ zend_string *dblib_handle_last_id(pdo_dbh_t *dbh, const zend_string *name) pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data; RETCODE ret; - char *id = NULL; + BYTE id[32]; size_t len; - zend_string *ret_id; /* * Would use scope_identity() but it's not implemented on Sybase @@ -267,13 +266,10 @@ zend_string *dblib_handle_last_id(pdo_dbh_t *dbh, const zend_string *name) return NULL; } - id = emalloc(32); len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1); dbcancel(H->link); - ret_id = zend_string_init(id, len, 0); - efree(id); - return ret_id; + return zend_string_init((const char *) id, len, 0); } static bool dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val) From 9a9d98e02fe19c0f0df5aca9da50da5f74446515 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 4 Jun 2025 12:09:37 +0200 Subject: [PATCH 14/44] Do not delete main chunk in zend_gc Closes GH-18756. Co-authored-by: Arnaud Le Blanc --- NEWS | 1 + Zend/tests/gh18756.phpt | 13 +++++++++++++ Zend/zend_alloc.c | 2 +- ext/zend_test/test.c | 10 ++++++++++ ext/zend_test/test.stub.php | 2 ++ ext/zend_test/test_arginfo.h | 6 +++++- 6 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Zend/tests/gh18756.phpt diff --git a/NEWS b/NEWS index 5eb1694b5e816..4db7d93ff9808 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ PHP NEWS - Core: . Fixed GH-18695 (zend_ast_export() - float number is not preserved). (Oleg Efimov) + . Do not delete main chunk in zend_gc. (danog, Arnaud) - Curl: . Fix memory leak when setting a list via curl_setopt fails. (nielsdos) diff --git a/Zend/tests/gh18756.phpt b/Zend/tests/gh18756.phpt new file mode 100644 index 0000000000000..6e112d9060499 --- /dev/null +++ b/Zend/tests/gh18756.phpt @@ -0,0 +1,13 @@ +--TEST-- +Bug GH-18756: Zend MM may delete the main chunk +--EXTENSIONS-- +zend_test +--FILE-- + +==DONE== +--EXPECT-- +==DONE== diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 573fd5fa26b80..2f80bdae3cfbd 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2047,7 +2047,7 @@ ZEND_API size_t zend_mm_gc(zend_mm_heap *heap) i++; } } - if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) { + if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE && chunk != heap->main_chunk) { zend_mm_chunk *next_chunk = chunk->next; zend_mm_delete_chunk(heap, chunk); diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index a7dd604d89ef3..04ece8bd2537e 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -1516,3 +1516,13 @@ static PHP_FUNCTION(zend_test_create_throwing_resource) zend_resource *res = zend_register_resource(NULL, le_throwing_resource); ZVAL_RES(return_value, res); } + +static PHP_FUNCTION(zend_test_gh18756) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zend_mm_heap *heap = zend_mm_startup(); + zend_mm_gc(heap); + zend_mm_gc(heap); + zend_mm_shutdown(heap, true, false); +} diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index c9477eef52712..f9cb93b5a1ccb 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -262,6 +262,8 @@ function zend_test_cast_fread($stream): void {} function zend_test_is_zend_ptr(int $addr): bool {} function zend_test_log_err_debug(string $str): void {} + + function zend_test_gh18756(): void {} } namespace ZendTestNS { diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 5947a6587bbed..c7e3df5c58d24 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9ddaf4d659226c55d49221c71702fa373d42695e */ + * Stub hash: 2f161861ab09b6b5b594dc2db7c2c9df49d76aa7 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -162,6 +162,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_log_err_debug, 0, 1, I ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0) ZEND_END_ARG_INFO() +#define arginfo_zend_test_gh18756 arginfo_zend_test_void_return + #define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_is_pcre_bundled #define arginfo_ZendTestNS2_namespaced_deprecated_func arginfo_zend_test_void_return @@ -292,6 +294,7 @@ static ZEND_FUNCTION(zend_test_set_fmode); static ZEND_FUNCTION(zend_test_cast_fread); static ZEND_FUNCTION(zend_test_is_zend_ptr); static ZEND_FUNCTION(zend_test_log_err_debug); +static ZEND_FUNCTION(zend_test_gh18756); static ZEND_FUNCTION(ZendTestNS2_namespaced_func); static ZEND_FUNCTION(ZendTestNS2_namespaced_deprecated_func); static ZEND_FUNCTION(ZendTestNS2_ZendSubNS_namespaced_func); @@ -372,6 +375,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_cast_fread, arginfo_zend_test_cast_fread) ZEND_FE(zend_test_is_zend_ptr, arginfo_zend_test_is_zend_ptr) ZEND_FE(zend_test_log_err_debug, arginfo_zend_test_log_err_debug) + ZEND_FE(zend_test_gh18756, arginfo_zend_test_gh18756) ZEND_NS_FALIAS("ZendTestNS2", namespaced_func, ZendTestNS2_namespaced_func, arginfo_ZendTestNS2_namespaced_func) ZEND_NS_DEP_FALIAS("ZendTestNS2", namespaced_deprecated_func, ZendTestNS2_namespaced_deprecated_func, arginfo_ZendTestNS2_namespaced_deprecated_func) ZEND_NS_FALIAS("ZendTestNS2", namespaced_aliased_func, zend_test_void_return, arginfo_ZendTestNS2_namespaced_aliased_func) From ef92e06de19b51f5655b80ab41e5abd849ca6ffa Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 7 Jun 2025 00:33:34 +0200 Subject: [PATCH 15/44] Fix memory leak on php_odbc_fetch_hash() failure The array is initialized but not freed. Closes GH-18787. --- NEWS | 3 +++ ext/odbc/php_odbc.c | 1 + 2 files changed, 4 insertions(+) diff --git a/NEWS b/NEWS index 4db7d93ff9808..2956cb8c6af0f 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,9 @@ PHP NEWS . Fix memory leak in intl_datetime_decompose() on failure. (nielsdos) . Fix memory leak in locale lookup on failure. (nielsdos) +- ODBC: + . Fix memory leak on php_odbc_fetch_hash() failure. (nielsdos) + - OpenSSL: . Fix memory leak of X509_STORE in php_openssl_setup_verify() on failure. (nielsdos) diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index 579b5e989bd3a..77ba85fe12ae8 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -1370,6 +1370,7 @@ static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type) if (rc == SQL_ERROR) { odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData"); efree(buf); + zval_ptr_dtor(return_value); RETURN_FALSE; } From 786090b35d2c339912c76588dacf32698f2e2d31 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 7 Jun 2025 00:36:08 +0200 Subject: [PATCH 16/44] pdo_odbc: Fix memory leak if WideCharToMultiByte() fails Closes GH-18788. --- NEWS | 3 +++ ext/pdo_odbc/odbc_stmt.c | 1 + 2 files changed, 4 insertions(+) diff --git a/NEWS b/NEWS index 2956cb8c6af0f..2208cd6e59aad 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,9 @@ PHP NEWS . Add missing filter cleanups on phar failure. (nielsdos) . Fixed bug GH-18642 (Signed integer overflow in ext/phar fseek). (nielsdos) +- PDO ODBC: + . Fix memory leak if WideCharToMultiByte() fails. (nielsdos) + - PGSQL: . Fix warning not being emitted when failure to cancel a query with pg_cancel_query(). (Girgias) diff --git a/ext/pdo_odbc/odbc_stmt.c b/ext/pdo_odbc/odbc_stmt.c index 4bf7162ea06e6..9f7ab24f8fadc 100644 --- a/ext/pdo_odbc/odbc_stmt.c +++ b/ext/pdo_odbc/odbc_stmt.c @@ -104,6 +104,7 @@ static int pdo_odbc_ucs22utf8(pdo_stmt_t *stmt, int is_unicode, zval *result) zend_string *str = zend_string_alloc(ret, 0); ret = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) Z_STRVAL_P(result), Z_STRLEN_P(result)/sizeof(WCHAR), ZSTR_VAL(str), ZSTR_LEN(str), NULL, NULL); if (ret == 0) { + zend_string_efree(str); return PDO_ODBC_CONV_FAIL; } From b3c8afe272a6919248986c703c2e1defc73ff707 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Thu, 5 Jun 2025 19:37:46 +0200 Subject: [PATCH 17/44] Fix GH-18743: Incompatibility in Inline TLS Assembly on Alpine 3.22 GAS started checking the relocation for tlsgd: it must use the %rdi register. However, the inline assembly now uses %rax instead. Fix it by changing the "=a" output register to "=D". Source: https://github.com/bminor/binutils-gdb/blob/ec181e1710e37007a8d95c284609bfaa5868d086/gas/config/tc-i386.c#L6793 gottpoff is unaffected. Closes GH-18779. --- NEWS | 4 ++++ ext/opcache/jit/zend_jit_x86.dasc | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 2208cd6e59aad..bf705c1da0b4d 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,10 @@ PHP NEWS - ODBC: . Fix memory leak on php_odbc_fetch_hash() failure. (nielsdos) +- Opcache: + . Fixed bug GH-18743 (Incompatibility in Inline TLS Assembly on Alpine 3.22). + (nielsdos, Arnaud) + - OpenSSL: . Fix memory leak of X509_STORE in php_openssl_setup_verify() on failure. (nielsdos) diff --git a/ext/opcache/jit/zend_jit_x86.dasc b/ext/opcache/jit/zend_jit_x86.dasc index 1f1abb59a1c24..7061f6b2b73ad 100644 --- a/ext/opcache/jit/zend_jit_x86.dasc +++ b/ext/opcache/jit/zend_jit_x86.dasc @@ -2910,7 +2910,7 @@ static int zend_jit_setup(void) __asm__( "leaq _tsrm_ls_cache@tlsgd(%%rip), %0\n" - : "=a" (ti)); + : "=D" (ti)); tsrm_tls_offset = ti[1]; tsrm_tls_index = ti[0] * 8; #elif defined(__FreeBSD__) @@ -2918,7 +2918,7 @@ static int zend_jit_setup(void) __asm__( "leaq _tsrm_ls_cache@tlsgd(%%rip), %0\n" - : "=a" (ti)); + : "=D" (ti)); tsrm_tls_offset = ti[1]; /* Index is offset by 1 on FreeBSD (https://github.com/freebsd/freebsd-src/blob/bf56e8b9c8639ac4447d223b83cdc128107cc3cd/libexec/rtld-elf/rtld.c#L5260) */ tsrm_tls_index = (ti[0] + 1) * 8; @@ -2927,7 +2927,7 @@ static int zend_jit_setup(void) __asm__( "leaq _tsrm_ls_cache@tlsgd(%%rip), %0\n" - : "=a" (ti)); + : "=D" (ti)); tsrm_tls_offset = ti[1]; tsrm_tls_index = ti[0] * 16; #endif From 4f0554fa549e02b172afcbd17bac20cac1cd5b47 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Wed, 4 Jun 2025 20:07:13 +0200 Subject: [PATCH 18/44] Properly handle __debugInfo() returning an array reference Currently, this fails because the type is IS_REFERENCE instead of IS_ARRAY, but this could be confusing because a function return value is normally dereferenced automatically in a lot of cases. Closes GH-18762. --- NEWS | 1 + Zend/tests/__debugInfo_reference.phpt | 22 ++++++++++++++++++++++ Zend/zend_object_handlers.c | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 Zend/tests/__debugInfo_reference.phpt diff --git a/NEWS b/NEWS index 9e6d47ed2a003..deae7fdbecc7a 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,7 @@ PHP NEWS . Drop support for -z CLI/CGI flag. (nielsdos) . Fixed GH-17956 - development server 404 page does not adapt to mobiles. (pascalchevrel) + . Properly handle __debugInfo() returning an array reference. (nielsdos) - CURL: . Added CURLFOLLOW_ALL, CURLFOLLOW_OBEYCODE and CURLFOLLOW_FIRSTONLY diff --git a/Zend/tests/__debugInfo_reference.phpt b/Zend/tests/__debugInfo_reference.phpt new file mode 100644 index 0000000000000..8b5519f408915 --- /dev/null +++ b/Zend/tests/__debugInfo_reference.phpt @@ -0,0 +1,22 @@ +--TEST-- +__debugInfo with reference return +--FILE-- + 1]; + + public function &__debugInfo(): array + { + return $this->tmp; + } +} + +var_dump(new Test); + +?> +--EXPECT-- +object(Test)#1 (1) { + ["x"]=> + int(1) +} diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index ba26ac7128a3d..f79023ade1c25 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -206,6 +206,9 @@ ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) / } zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval); + if (UNEXPECTED(Z_ISREF(retval))) { + zend_unwrap_reference(&retval); + } if (Z_TYPE(retval) == IS_ARRAY) { if (!Z_REFCOUNTED(retval)) { *is_temp = 1; From b41a8aaffda6ab1cb1070b3516d9f445cd8d4685 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:55:39 +0200 Subject: [PATCH 19/44] [ci skip] Fix NEWS location --- NEWS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS b/NEWS index deae7fdbecc7a..67f4d43e62edf 100644 --- a/NEWS +++ b/NEWS @@ -14,7 +14,6 @@ PHP NEWS . Drop support for -z CLI/CGI flag. (nielsdos) . Fixed GH-17956 - development server 404 page does not adapt to mobiles. (pascalchevrel) - . Properly handle __debugInfo() returning an array reference. (nielsdos) - CURL: . Added CURLFOLLOW_ALL, CURLFOLLOW_OBEYCODE and CURLFOLLOW_FIRSTONLY @@ -54,6 +53,7 @@ PHP NEWS evaluation) and GH-18464 (Recursion protection for deprecation constants not released on bailout). (DanielEScherzer and ilutov) . Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko) + . Properly handle __debugInfo() returning an array reference. (nielsdos) - Curl: . Added curl_multi_get_handles(). (timwolla) From 7b6c0b99bbd2d2f4545d681d039856f448392c5e Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 12:44:41 +0200 Subject: [PATCH 20/44] zend_alloc: Fix compilation with ZEND_MM_CUSTOM=0 (#18808) The poison feature relies on ZEND_MM_CUSTOM=1. If ZEND_MM_CUSTOM=0, the build fails. To fix this, move some `#endif`. --- Zend/zend_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 92873be7bfcef..74c8ae9c4eff4 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2400,7 +2400,6 @@ static void zend_mm_check_leaks(zend_mm_heap *heap) static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); static void tracked_free_all(zend_mm_heap *heap); static void *poison_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); -#endif static void zend_mm_check_freelists(zend_mm_heap *heap) { @@ -2411,6 +2410,7 @@ static void zend_mm_check_freelists(zend_mm_heap *heap) } } } +#endif ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent) { @@ -3041,7 +3041,6 @@ static void tracked_free_all(zend_mm_heap *heap) { free(ptr); } ZEND_HASH_FOREACH_END(); } -#endif static void* poison_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { @@ -3236,6 +3235,7 @@ static void poison_enable(zend_mm_heap *heap, char *parameters) zend_mm_set_custom_handlers_ex(heap, poison_malloc, poison_free, poison_realloc, poison_gc, poison_shutdown); } +#endif static void alloc_globals_ctor(zend_alloc_globals *alloc_globals) { From 186a8116beaf1148911032016e539e0ed52524f0 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 12:16:57 +0200 Subject: [PATCH 21/44] Fix test conflict between copy_variation2-win32-mb.phpt and copy_variation2-win32.phpt Closes GH-18809. --- .../tests/file/copy_variation2-win32-mb.phpt | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/ext/standard/tests/file/copy_variation2-win32-mb.phpt b/ext/standard/tests/file/copy_variation2-win32-mb.phpt index 4251a24e54cf7..67d84ee3e32c1 100644 --- a/ext/standard/tests/file/copy_variation2-win32-mb.phpt +++ b/ext/standard/tests/file/copy_variation2-win32-mb.phpt @@ -22,24 +22,24 @@ fclose($file_handle); $dest_files = array( /* File names containing special(non-alpha numeric) characters */ - "_copy_variation2.tmp", - "@copy_variation2.tmp", - "#copy_variation2.tmp", - "+copy_variation2.tmp", - "?copy_variation2.tmp", - ">copy_variation2.tmp", - "!copy_variation2.tmp", - "©_variation2.tmp", - "(copy_variation2.tmp", - ":copy_variation2.tmp", - ";copy_variation2.tmp", - "=copy_variation2.tmp", - "[copy_variation2.tmp", - "^copy_variation2.tmp", - "{copy_variation2.tmp", - "|copy_variation2.tmp", - "~copy_variation2.tmp", - "\$copy_variation2.tmp" + "_copy_variation2_mb.tmp", + "@copy_variation2_mb.tmp", + "#copy_variation2_mb.tmp", + "+copy_variation2_mb.tmp", + "?copy_variation2_mb.tmp", + ">copy_variation2_mb.tmp", + "!copy_variation2_mb.tmp", + "©_variation2_mb.tmp", + "(copy_variation2_mb.tmp", + ":copy_variation2_mb.tmp", + ";copy_variation2_mb.tmp", + "=copy_variation2_mb.tmp", + "[copy_variation2_mb.tmp", + "^copy_variation2_mb.tmp", + "{copy_variation2_mb.tmp", + "|copy_variation2_mb.tmp", + "~copy_variation2_mb.tmp", + "\$copy_variation2_mb.tmp" ); echo "Size of the source file before copy operation => "; @@ -90,28 +90,28 @@ Size of the source file before copy operation => int(1500) -- Iteration 1 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/_copy_variation2.tmp +Destination file name => %s/_copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 2 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/@copy_variation2.tmp +Destination file name => %s/@copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 3 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/#copy_variation2.tmp +Destination file name => %s/#copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 4 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/+copy_variation2.tmp +Destination file name => %s/+copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) @@ -130,21 +130,21 @@ Existence of destination file => bool(false) -- Iteration 7 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/!copy_variation2.tmp +Destination file name => %s/!copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 8 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/©_variation2.tmp +Destination file name => %s/©_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 9 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/(copy_variation2.tmp +Destination file name => %s/(copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) @@ -157,35 +157,35 @@ Existence of destination file => bool(false) -- Iteration 11 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/;copy_variation2.tmp +Destination file name => %s/;copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 12 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/=copy_variation2.tmp +Destination file name => %s/=copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 13 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/[copy_variation2.tmp +Destination file name => %s/[copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 14 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/^copy_variation2.tmp +Destination file name => %s/^copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 15 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/{copy_variation2.tmp +Destination file name => %s/{copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) @@ -198,14 +198,14 @@ Existence of destination file => bool(false) -- Iteration 17 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/~copy_variation2.tmp +Destination file name => %s/~copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) -- Iteration 18 -- Copy operation => bool(true) Existence of destination file => bool(true) -Destination file name => %s/$copy_variation2.tmp +Destination file name => %s/$copy_variation2_mb.tmp Size of source file => int(1500) Size of destination file => int(1500) *** Done *** From d11f9717fdb10bccc4e17bd20508fb7b6d5c9359 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:34:22 +0200 Subject: [PATCH 22/44] zend_alloc: Fix compile with ZEND_MM_STAT=0 Closes GH-18811. --- NEWS | 1 + Zend/zend_alloc.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/NEWS b/NEWS index bf705c1da0b4d..11fb787662c4a 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ PHP NEWS . Fixed GH-18695 (zend_ast_export() - float number is not preserved). (Oleg Efimov) . Do not delete main chunk in zend_gc. (danog, Arnaud) + . Fix compile issues with zend_alloc and some non-default options. (nielsdos) - Curl: . Fix memory leak when setting a list via curl_setopt fails. (nielsdos) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 2f80bdae3cfbd..47e9967a1e29f 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2274,7 +2274,9 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent) /* Make sure the heap free below does not use tracked_free(). */ heap->custom_heap.std._free = free; } +#if ZEND_MM_STAT heap->size = 0; +#endif } if (full) { @@ -2820,6 +2822,7 @@ static zend_always_inline zval *tracked_get_size_zv(zend_mm_heap *heap, void *pt } static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t add_size) { +#if ZEND_MM_STAT if (add_size > heap->limit - heap->size && !heap->overflow) { #if ZEND_DEBUG zend_mm_safe_error(heap, @@ -2831,6 +2834,7 @@ static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t ad heap->limit, add_size); #endif } +#endif } static void *tracked_malloc(size_t size) @@ -2844,7 +2848,9 @@ static void *tracked_malloc(size_t size) } tracked_add(heap, ptr, size); +#if ZEND_MM_STAT heap->size += size; +#endif return ptr; } @@ -2855,7 +2861,9 @@ static void tracked_free(void *ptr) { zend_mm_heap *heap = AG(mm_heap); zval *size_zv = tracked_get_size_zv(heap, ptr); +#if ZEND_MM_STAT heap->size -= Z_LVAL_P(size_zv); +#endif zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) size_zv); free(ptr); } @@ -2880,7 +2888,9 @@ static void *tracked_realloc(void *ptr, size_t new_size) { ptr = __zend_realloc(ptr, new_size); tracked_add(heap, ptr, new_size); +#if ZEND_MM_STAT heap->size += new_size - old_size; +#endif return ptr; } From 53231a81dda7427b0d4e68d9cec66ad78b2688b3 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 8 Jun 2025 14:07:47 +0100 Subject: [PATCH 23/44] ext/pdo_sqlite: adding Pdo_Sqlite::ATTR_BUSY_STATEMENT allow to check if a statement is still running before reusage. close GH-18804 --- NEWS | 3 ++- UPGRADING | 6 ++++++ build/php.m4 | 2 +- ext/pdo_sqlite/pdo_sqlite.c | 10 +++------- ext/pdo_sqlite/pdo_sqlite.stub.php | 3 +++ ext/pdo_sqlite/pdo_sqlite_arginfo.h | 8 +++++++- ext/pdo_sqlite/php_pdo_sqlite_int.h | 3 ++- ext/pdo_sqlite/sqlite_driver.c | 10 +++------- ext/pdo_sqlite/sqlite_statement.c | 15 ++++++++++----- .../subclasses/pdo_sqlite_constants.phpt | 2 ++ .../subclasses/pdo_sqlite_getattr_busy.phpt | 19 +++++++++++++++++++ 11 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 ext/pdo_sqlite/tests/subclasses/pdo_sqlite_getattr_busy.phpt diff --git a/NEWS b/NEWS index 67f4d43e62edf..1c28ce97b7bd6 100644 --- a/NEWS +++ b/NEWS @@ -53,7 +53,6 @@ PHP NEWS evaluation) and GH-18464 (Recursion protection for deprecation constants not released on bailout). (DanielEScherzer and ilutov) . Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko) - . Properly handle __debugInfo() returning an array reference. (nielsdos) - Curl: . Added curl_multi_get_handles(). (timwolla) @@ -144,6 +143,8 @@ PHP NEWS . Implement GH-17321: Add setAuthorizer to Pdo\Sqlite. (nielsdos) . PDO::sqliteCreateCollation now throws a TypeError if the callback has a wrong return type. (David Carlier) + . Added Pdo_Sqlite::ATTR_BUSY_STATEMENT constant to check + if a statement is currently executing. (David Carlier) - PGSQL: . Added pg_close_stmt to close a prepared statement while allowing diff --git a/UPGRADING b/UPGRADING index aebda12cf500e..b38fd2c7a2deb 100644 --- a/UPGRADING +++ b/UPGRADING @@ -194,6 +194,9 @@ PHP 8.5 UPGRADE NOTES IntlListFormatter::WIDTH_NARROW widths. It is supported from icu 67. +- PDO_Sqlite: + . Added class constant Pdo_Sqlite::ATTR_BUSY_STATEMENT. + - SOAP: . Enumeration cases are now dumped in __getTypes(). @@ -424,6 +427,9 @@ PHP 8.5 UPGRADE NOTES - PCRE: . Upgraded to pcre2lib from 10.44 to 10.45. +- PDO_Sqlite: + . Increased minimum release version support from 3.7.7 to 3.7.17. + - Readline: . The return types of readline_add_history(), readline_clear_history(), and readline_callback_handler_install() have been changed to true, rather diff --git a/build/php.m4 b/build/php.m4 index 640f01008009d..aa49766fedd7f 100644 --- a/build/php.m4 +++ b/build/php.m4 @@ -1923,7 +1923,7 @@ dnl dnl Common setup macro for SQLite library. dnl AC_DEFUN([PHP_SETUP_SQLITE], [ -PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.7], [ +PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.17], [ PHP_EVAL_INCLINE([$SQLITE_CFLAGS]) PHP_EVAL_LIBLINE([$SQLITE_LIBS], [$1]) ]) diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c index fbbb336c1af1e..023e35a2bc33c 100644 --- a/ext/pdo_sqlite/pdo_sqlite.c +++ b/ext/pdo_sqlite/pdo_sqlite.c @@ -385,14 +385,10 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned", ZSTR_VAL(func_name), zend_zval_value_name(&retval)); zend_string_release(func_name); - ret = FAILURE; + zval_ptr_dtor(&retval); + return FAILURE; } - if (Z_LVAL(retval) > 0) { - ret = 1; - } else if (Z_LVAL(retval) < 0) { - ret = -1; - } - zval_ptr_dtor(&retval); + ret = ZEND_NORMALIZE_BOOL(Z_LVAL(retval)); } return ret; diff --git a/ext/pdo_sqlite/pdo_sqlite.stub.php b/ext/pdo_sqlite/pdo_sqlite.stub.php index 3832683598ed5..4cb6c14eae0a4 100644 --- a/ext/pdo_sqlite/pdo_sqlite.stub.php +++ b/ext/pdo_sqlite/pdo_sqlite.stub.php @@ -33,6 +33,9 @@ class Sqlite extends \PDO /** @cvalue PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES */ public const int ATTR_EXTENDED_RESULT_CODES = UNKNOWN; + /** @cvalue PDO_SQLITE_ATTR_BUSY_STATEMENT */ + public const int ATTR_BUSY_STATEMENT = UNKNOWN; + /** @cvalue SQLITE_OK */ public const int OK = UNKNOWN; diff --git a/ext/pdo_sqlite/pdo_sqlite_arginfo.h b/ext/pdo_sqlite/pdo_sqlite_arginfo.h index 75de256e55c7b..ec826bc4bbc5a 100644 --- a/ext/pdo_sqlite/pdo_sqlite_arginfo.h +++ b/ext/pdo_sqlite/pdo_sqlite_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: f8cd6b3c6aa662d76dca3d0a28d61acfb5a611b5 */ + * Stub hash: ae1e62d72c3c8290c9f39f21b583e980ea9b8eb2 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Pdo_Sqlite_createAggregate, 0, 3, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0) @@ -110,6 +110,12 @@ static zend_class_entry *register_class_Pdo_Sqlite(zend_class_entry *class_entry zend_declare_typed_class_constant(class_entry, const_ATTR_EXTENDED_RESULT_CODES_name, &const_ATTR_EXTENDED_RESULT_CODES_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(const_ATTR_EXTENDED_RESULT_CODES_name); + zval const_ATTR_BUSY_STATEMENT_value; + ZVAL_LONG(&const_ATTR_BUSY_STATEMENT_value, PDO_SQLITE_ATTR_BUSY_STATEMENT); + zend_string *const_ATTR_BUSY_STATEMENT_name = zend_string_init_interned("ATTR_BUSY_STATEMENT", sizeof("ATTR_BUSY_STATEMENT") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_ATTR_BUSY_STATEMENT_name, &const_ATTR_BUSY_STATEMENT_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_ATTR_BUSY_STATEMENT_name); + zval const_OK_value; ZVAL_LONG(&const_OK_value, SQLITE_OK); zend_string *const_OK_name = zend_string_init_interned("OK", sizeof("OK") - 1, 1); diff --git a/ext/pdo_sqlite/php_pdo_sqlite_int.h b/ext/pdo_sqlite/php_pdo_sqlite_int.h index 4a39781f85c96..8acb95015e79a 100644 --- a/ext/pdo_sqlite/php_pdo_sqlite_int.h +++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h @@ -73,7 +73,8 @@ extern const struct pdo_stmt_methods sqlite_stmt_methods; enum { PDO_SQLITE_ATTR_OPEN_FLAGS = PDO_ATTR_DRIVER_SPECIFIC, PDO_SQLITE_ATTR_READONLY_STATEMENT, - PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES + PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES, + PDO_SQLITE_ATTR_BUSY_STATEMENT }; typedef int pdo_sqlite_create_collation_callback(void*, int, const void*, int, const void*); diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index d06d255c14cd8..2c907a34f489b 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -492,14 +492,10 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned", ZSTR_VAL(func_name), zend_zval_value_name(&retval)); zend_string_release(func_name); - ret = FAILURE; - } - if (Z_LVAL(retval) > 0) { - ret = 1; - } else if (Z_LVAL(retval) < 0) { - ret = -1; + zval_ptr_dtor(&retval); + return FAILURE; } - zval_ptr_dtor(&retval); + ret = ZEND_NORMALIZE_BOOL(Z_LVAL(retval)); } return ret; diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c index c0e327450232f..64c8c8a86dd9a 100644 --- a/ext/pdo_sqlite/sqlite_statement.c +++ b/ext/pdo_sqlite/sqlite_statement.c @@ -375,13 +375,18 @@ static int pdo_sqlite_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval case PDO_SQLITE_ATTR_READONLY_STATEMENT: ZVAL_FALSE(val); -#if SQLITE_VERSION_NUMBER >= 3007004 - if (sqlite3_stmt_readonly(S->stmt)) { - ZVAL_TRUE(val); - } -#endif + if (sqlite3_stmt_readonly(S->stmt)) { + ZVAL_TRUE(val); + } break; + case PDO_SQLITE_ATTR_BUSY_STATEMENT: + ZVAL_FALSE(val); + + if (sqlite3_stmt_busy(S->stmt)) { + ZVAL_TRUE(val); + } + break; default: return 0; } diff --git a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_constants.phpt b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_constants.phpt index 243240bef4662..d1db58b1323eb 100644 --- a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_constants.phpt +++ b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_constants.phpt @@ -13,6 +13,7 @@ var_dump(Pdo\Sqlite::OPEN_READWRITE); var_dump(Pdo\Sqlite::OPEN_CREATE); var_dump(Pdo\Sqlite::ATTR_READONLY_STATEMENT); var_dump(Pdo\Sqlite::ATTR_EXTENDED_RESULT_CODES); +var_dump(Pdo\Sqlite::ATTR_BUSY_STATEMENT); ?> --EXPECTF-- @@ -24,3 +25,4 @@ int(%d) int(%d) int(%d) int(%d) +int(%d) diff --git a/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_getattr_busy.phpt b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_getattr_busy.phpt new file mode 100644 index 0000000000000..230fb7390ae50 --- /dev/null +++ b/ext/pdo_sqlite/tests/subclasses/pdo_sqlite_getattr_busy.phpt @@ -0,0 +1,19 @@ +--TEST-- +Pdo\Sqlite::ATTR_BUSY_STATEMENT usage +--EXTENSIONS-- +pdo_sqlite +--FILE-- +query('CREATE TABLE test_busy (a string);'); +$db->query('INSERT INTO test_busy VALUES ("interleaved"), ("statements")'); +$st = $db->prepare('SELECT a FROM test_busy'); +var_dump($st->getAttribute(Pdo\Sqlite::ATTR_BUSY_STATEMENT)); +$st->execute(); +var_dump($st->getAttribute(Pdo\Sqlite::ATTR_BUSY_STATEMENT)); +?> +--EXPECTF-- +bool(false) +bool(true) From 931ee4bf6292e99b9342d2b171253167cbe51453 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 20:04:24 +0200 Subject: [PATCH 24/44] [ci skip] Re-add accidentally removed NEWS entry --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 1c28ce97b7bd6..9fe60cbb875cc 100644 --- a/NEWS +++ b/NEWS @@ -53,6 +53,7 @@ PHP NEWS evaluation) and GH-18464 (Recursion protection for deprecation constants not released on bailout). (DanielEScherzer and ilutov) . Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko) + . Properly handle __debugInfo() returning an array reference. (nielsdos) - Curl: . Added curl_multi_get_handles(). (timwolla) From e1181475e1102ae5e6e7cf711381420e0cbc6ab6 Mon Sep 17 00:00:00 2001 From: DanielEScherzer Date: Mon, 9 Jun 2025 12:22:07 -0700 Subject: [PATCH 25/44] release-process: update pre-release cycle docs (#18805) https://wiki.php.net/rfc/release_cycle_update --- docs/release-process.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/release-process.md b/docs/release-process.md index 5d603a22f078e..6eec8ff9f6435 100644 --- a/docs/release-process.md +++ b/docs/release-process.md @@ -18,14 +18,14 @@ PHP on the fourth Thursday of November each year. Following the GA release, we publish patch-level releases every four weeks, with at least one release candidate (RC) published two weeks before each patch-level release. -Each major and minor version undergoes a 24-week pre-release cycle before GA -release. The pre-release cycle begins on the second Thursday of June with the -first alpha release of the new major/minor version. The pre-release cycle -consists of at least: +Each major and minor version undergoes a 20-week pre-release cycle before GA +release. The pre-release cycle begins on the second Thursday of July with the +first alpha release of the new major/minor version (usually; count back from the +GA release date). The pre-release cycle consists of at least: - 3 alpha releases - 3 beta releases -- 6 release candidates +- 4 release candidates Feature freeze for the next major/minor occurs with the first beta release. From fe3bea090e598cc7d543dc6086c7a65f6e6787f1 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:17:33 +0200 Subject: [PATCH 26/44] Fix technically incorrect sizeof This doesn't actually matter because both `*sal` and `**sal` are pointer sized, but this makes analysers happy. Fixes bug #68866. Closes GH-18816. --- main/network.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/network.c b/main/network.c index d4938a4a08c1e..8de81a6271a2f 100644 --- a/main/network.c +++ b/main/network.c @@ -227,7 +227,7 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka for (n = 1; (sai = sai->ai_next) != NULL; n++) ; - *sal = safe_emalloc((n + 1), sizeof(*sal), 0); + *sal = safe_emalloc((n + 1), sizeof(**sal), 0); sai = res; sap = *sal; @@ -266,7 +266,7 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka in = *((struct in_addr *) host_info->h_addr); } - *sal = safe_emalloc(2, sizeof(*sal), 0); + *sal = safe_emalloc(2, sizeof(**sal), 0); sap = *sal; *sap = emalloc(sizeof(struct sockaddr_in)); (*sap)->sa_family = AF_INET; From 3e37bcedf4df26efed5db89626d06b1897eb168f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 10 Jun 2025 09:05:37 +0200 Subject: [PATCH 27/44] [skip ci] Trim trailing whitespace in zend_compile.c Introduced in 5544be7018fe64584945c49fffcda20402bece73. Trimming to simplify the diff of the pipe operator RFC implementation. --- Zend/zend_compile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 0669d106f15e9..379a97733d0ed 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -8361,10 +8361,10 @@ static zend_op_array *zend_compile_func_decl_ex( "nodiscard", sizeof("nodiscard")-1 ); - + if (nodiscard_attribute) { op_array->fn_flags |= ZEND_ACC_NODISCARD; - } + } } /* Do not leak the class scope into free standing functions, even if they are dynamically From 2036c7158d8f3d2bc57d105dcddf647ebca0c5db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 10 Jun 2025 09:12:32 +0200 Subject: [PATCH 28/44] [skip ci] Add T_VOID_CAST constant to UPGRADING see 8779e2a6031b78bb0e3cac980410eb038b9932c4 --- UPGRADING | 3 +++ 1 file changed, 3 insertions(+) diff --git a/UPGRADING b/UPGRADING index b38fd2c7a2deb..f642166de5150 100644 --- a/UPGRADING +++ b/UPGRADING @@ -474,6 +474,9 @@ PHP 8.5 UPGRADE NOTES - SHUT_WR. - SHUT_RDWR. +- Tokenizer: + . T_VOID_CAST. + ======================================== 11. Changes to INI File Handling ======================================== From 1c09c0c8323342aaaecbe444b3afa45efbd72ced Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Tue, 10 Jun 2025 02:59:43 -0500 Subject: [PATCH 29/44] RFC: Pipe operator (#17118) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gina Peter Banyard Co-authored-by: Arnaud Le Blanc Co-authored-by: Tim Düsterhus --- NEWS | 1 + UPGRADING | 3 + Zend/tests/pipe_operator/ast.phpt | 109 ++++++++++++++++++ Zend/tests/pipe_operator/call_by_ref.phpt | 37 ++++++ .../pipe_operator/call_prefer_by_ref.phpt | 17 +++ .../tests/pipe_operator/complex_ordering.phpt | 20 ++++ .../compound_userland_calls.phpt | 19 +++ .../pipe_operator/exception_interruption.phpt | 37 ++++++ .../pipe_operator/function_not_found.phpt | 15 +++ Zend/tests/pipe_operator/generators.phpt | 30 +++++ .../pipe_operator/mixed_callable_call.phpt | 80 +++++++++++++ .../pipe_operator/namespaced_functions.phpt | 22 ++++ Zend/tests/pipe_operator/optimizations.phpt | 89 ++++++++++++++ .../pipe_operator/optional_parameters.phpt | 15 +++ .../pipe_operator/precedence_addition.phpt | 16 +++ .../pipe_operator/precedence_coalesce.phpt | 17 +++ .../pipe_operator/precedence_comparison.phpt | 16 +++ .../pipe_operator/precedence_ternary.phpt | 36 ++++++ .../pipe_operator/simple_builtin_call.phpt | 11 ++ .../pipe_operator/simple_userland_call.phpt | 15 +++ .../pipe_operator/too_many_parameters.phpt | 21 ++++ Zend/tests/pipe_operator/type_mismatch.phpt | 20 ++++ Zend/tests/pipe_operator/void_return.phpt | 21 ++++ Zend/tests/pipe_operator/wrapped_chains.phpt | 21 ++++ Zend/zend_ast.c | 1 + Zend/zend_ast.h | 1 + Zend/zend_compile.c | 54 +++++++++ Zend/zend_language_parser.y | 4 + Zend/zend_language_scanner.l | 4 + ext/tokenizer/tokenizer_data.c | 1 + ext/tokenizer/tokenizer_data.stub.php | 5 + ext/tokenizer/tokenizer_data_arginfo.h | 3 +- 32 files changed, 760 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/pipe_operator/ast.phpt create mode 100644 Zend/tests/pipe_operator/call_by_ref.phpt create mode 100644 Zend/tests/pipe_operator/call_prefer_by_ref.phpt create mode 100644 Zend/tests/pipe_operator/complex_ordering.phpt create mode 100644 Zend/tests/pipe_operator/compound_userland_calls.phpt create mode 100644 Zend/tests/pipe_operator/exception_interruption.phpt create mode 100644 Zend/tests/pipe_operator/function_not_found.phpt create mode 100644 Zend/tests/pipe_operator/generators.phpt create mode 100644 Zend/tests/pipe_operator/mixed_callable_call.phpt create mode 100644 Zend/tests/pipe_operator/namespaced_functions.phpt create mode 100644 Zend/tests/pipe_operator/optimizations.phpt create mode 100644 Zend/tests/pipe_operator/optional_parameters.phpt create mode 100644 Zend/tests/pipe_operator/precedence_addition.phpt create mode 100644 Zend/tests/pipe_operator/precedence_coalesce.phpt create mode 100644 Zend/tests/pipe_operator/precedence_comparison.phpt create mode 100644 Zend/tests/pipe_operator/precedence_ternary.phpt create mode 100644 Zend/tests/pipe_operator/simple_builtin_call.phpt create mode 100644 Zend/tests/pipe_operator/simple_userland_call.phpt create mode 100644 Zend/tests/pipe_operator/too_many_parameters.phpt create mode 100644 Zend/tests/pipe_operator/type_mismatch.phpt create mode 100644 Zend/tests/pipe_operator/void_return.phpt create mode 100644 Zend/tests/pipe_operator/wrapped_chains.phpt diff --git a/NEWS b/NEWS index 9fe60cbb875cc..b1100f7672226 100644 --- a/NEWS +++ b/NEWS @@ -54,6 +54,7 @@ PHP NEWS released on bailout). (DanielEScherzer and ilutov) . Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko) . Properly handle __debugInfo() returning an array reference. (nielsdos) + . Added the pipe (|>) operator. (crell) - Curl: . Added curl_multi_get_handles(). (timwolla) diff --git a/UPGRADING b/UPGRADING index f642166de5150..7025c9778e7ac 100644 --- a/UPGRADING +++ b/UPGRADING @@ -144,6 +144,8 @@ PHP 8.5 UPGRADE NOTES RFC: https://wiki.php.net/rfc/attributes-on-constants . The #[\Deprecated] attribute can now be used on constants. RFC: https://wiki.php.net/rfc/attributes-on-constants + . Added the pipe (|>) operator. + RFC: https://wiki.php.net/rfc/pipe-operator-v3 - Curl: . Added support for share handles that are persisted across multiple PHP @@ -476,6 +478,7 @@ PHP 8.5 UPGRADE NOTES - Tokenizer: . T_VOID_CAST. + . T_PIPE. ======================================== 11. Changes to INI File Handling diff --git a/Zend/tests/pipe_operator/ast.phpt b/Zend/tests/pipe_operator/ast.phpt new file mode 100644 index 0000000000000..e8c088dabfdbd --- /dev/null +++ b/Zend/tests/pipe_operator/ast.phpt @@ -0,0 +1,109 @@ +--TEST-- +A pipe operator displays as a pipe operator when outputting syntax, with correct parens. +--FILE-- + baz() . quux()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && (foo() . bar()) |> baz() . quux()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() . (bar() |> baz()) . quux()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() . bar() |> (baz() . quux())); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && (foo() . bar() |> baz()) . quux()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() . (bar() |> baz() . quux())); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +print "<, which binds lower\n"; + +try { + assert(false && foo() < bar() |> baz()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && (foo() < bar()) |> baz()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() < (bar() |> baz())); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() |> bar() < baz()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && (foo() |> bar()) < baz()); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +try { + assert(false && foo() |> (bar() < baz())); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + + + +print "misc examples\n"; + +try { + assert(false && foo() |> (bar() |> baz(...))); +} catch (AssertionError $e) { + echo $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +Concat, which binds higher +assert(false && foo() . bar() |> baz() . quux()) +assert(false && foo() . bar() |> baz() . quux()) +assert(false && foo() . (bar() |> baz()) . quux()) +assert(false && foo() . bar() |> baz() . quux()) +assert(false && (foo() . bar() |> baz()) . quux()) +assert(false && foo() . (bar() |> baz() . quux())) +<, which binds lower +assert(false && foo() < bar() |> baz()) +assert(false && (foo() < bar()) |> baz()) +assert(false && foo() < bar() |> baz()) +assert(false && foo() |> bar() < baz()) +assert(false && foo() |> bar() < baz()) +assert(false && foo() |> (bar() < baz())) +misc examples +assert(false && foo() |> (bar() |> baz(...))) diff --git a/Zend/tests/pipe_operator/call_by_ref.phpt b/Zend/tests/pipe_operator/call_by_ref.phpt new file mode 100644 index 0000000000000..026089b0a6547 --- /dev/null +++ b/Zend/tests/pipe_operator/call_by_ref.phpt @@ -0,0 +1,37 @@ +--TEST-- +Pipe operator rejects by-reference functions. +--FILE-- + _modify(...); + var_dump($res1); +} catch (\Error $e) { + echo $e->getMessage(), PHP_EOL; +} + +// Complex variables. +try { + $a = ['foo' => 'beep']; + $res2 = $a |> _append(...); + var_dump($res2); +} catch (\Error $e) { + echo $e->getMessage(), PHP_EOL; +} + + +?> +--EXPECTF-- +_modify(): Argument #1 ($a) could not be passed by reference +_append(): Argument #1 ($a) could not be passed by reference diff --git a/Zend/tests/pipe_operator/call_prefer_by_ref.phpt b/Zend/tests/pipe_operator/call_prefer_by_ref.phpt new file mode 100644 index 0000000000000..31750ac280d71 --- /dev/null +++ b/Zend/tests/pipe_operator/call_prefer_by_ref.phpt @@ -0,0 +1,17 @@ +--TEST-- +Pipe operator accepts prefer-by-reference functions. +--FILE-- + array_multisort(...); + var_dump($r); +} catch (\Error $e) { + echo $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +bool(true) diff --git a/Zend/tests/pipe_operator/complex_ordering.phpt b/Zend/tests/pipe_operator/complex_ordering.phpt new file mode 100644 index 0000000000000..49f3d47ade4b9 --- /dev/null +++ b/Zend/tests/pipe_operator/complex_ordering.phpt @@ -0,0 +1,20 @@ +--TEST-- +Functions are executed in the expected order +--FILE-- + (bar() ? baz(...) : quux(...)) + |> var_dump(...); + +?> +--EXPECT-- +foo +bar +quux +int(1) diff --git a/Zend/tests/pipe_operator/compound_userland_calls.phpt b/Zend/tests/pipe_operator/compound_userland_calls.phpt new file mode 100644 index 0000000000000..922c3c5ac23d8 --- /dev/null +++ b/Zend/tests/pipe_operator/compound_userland_calls.phpt @@ -0,0 +1,19 @@ +--TEST-- +Pipe operator chains +--FILE-- + '_test1' |> '_test2'; + +var_dump($res1); +?> +--EXPECT-- +int(12) diff --git a/Zend/tests/pipe_operator/exception_interruption.phpt b/Zend/tests/pipe_operator/exception_interruption.phpt new file mode 100644 index 0000000000000..6711b652e7c6b --- /dev/null +++ b/Zend/tests/pipe_operator/exception_interruption.phpt @@ -0,0 +1,37 @@ +--TEST-- +A pipe interrupted by an exception, to demonstrate correct order of execution. +--FILE-- + (bar() ? baz(...) : quux(...)) + |> var_dump(...); +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +try { + $result = foo() + |> (throw new Exception('Break')) + |> (bar() ? baz(...) : quux(...)) + |> var_dump(...); +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +?> +--EXPECTF-- +foo +bar +quux +Exception: Oops +foo +Exception: Break diff --git a/Zend/tests/pipe_operator/function_not_found.phpt b/Zend/tests/pipe_operator/function_not_found.phpt new file mode 100644 index 0000000000000..747f53ce3e3d1 --- /dev/null +++ b/Zend/tests/pipe_operator/function_not_found.phpt @@ -0,0 +1,15 @@ +--TEST-- +Pipe operator throws normally on missing function +--FILE-- + '_test'; +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +Error: Call to undefined function _test() diff --git a/Zend/tests/pipe_operator/generators.phpt b/Zend/tests/pipe_operator/generators.phpt new file mode 100644 index 0000000000000..9607af581fcdc --- /dev/null +++ b/Zend/tests/pipe_operator/generators.phpt @@ -0,0 +1,30 @@ +--TEST-- +Generators +--FILE-- + map_incr(...) |> iterator_to_array(...); + +var_dump($result); +?> +--EXPECT-- +array(3) { + [0]=> + int(2) + [1]=> + int(3) + [2]=> + int(4) +} diff --git a/Zend/tests/pipe_operator/mixed_callable_call.phpt b/Zend/tests/pipe_operator/mixed_callable_call.phpt new file mode 100644 index 0000000000000..55bae626f1890 --- /dev/null +++ b/Zend/tests/pipe_operator/mixed_callable_call.phpt @@ -0,0 +1,80 @@ +--TEST-- +Pipe operator handles all callable styles +--FILE-- + times3(...) + |> 'times5' + |> $test->times7(...) + |> [$test, 'times11'] + |> StaticTest::times13(...) + |> [StaticTest::class, 'times17'] + |> new Times23() + |> $times29 + |> fn($x) => times2($x) +; + +var_dump($res1); +?> +--EXPECT-- +int(340510170) diff --git a/Zend/tests/pipe_operator/namespaced_functions.phpt b/Zend/tests/pipe_operator/namespaced_functions.phpt new file mode 100644 index 0000000000000..70ce85c0a25c6 --- /dev/null +++ b/Zend/tests/pipe_operator/namespaced_functions.phpt @@ -0,0 +1,22 @@ +--TEST-- +Pipe operator handles namespaces +--FILE-- + test(...); + + 5 |> \Beep\test(...); +} +?> +--EXPECT-- +5 +5 diff --git a/Zend/tests/pipe_operator/optimizations.phpt b/Zend/tests/pipe_operator/optimizations.phpt new file mode 100644 index 0000000000000..afdc528337c13 --- /dev/null +++ b/Zend/tests/pipe_operator/optimizations.phpt @@ -0,0 +1,89 @@ +--TEST-- +Pipe operator optimizes away most callables +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.opt_debug_level=0x20000 +--EXTENSIONS-- +opcache +--FILE-- + _test1(...) + |> $o->foo(...) + |> Other::bar(...) +; + +var_dump($res1); +?> +--EXPECTF-- +$_main: + ; (lines=18, args=0, vars=2, tmps=2) + ; (after optimizer) + ; %s:1-27 +0000 V2 = NEW 0 string("Other") +0001 DO_FCALL +0002 ASSIGN CV0($o) V2 +0003 INIT_FCALL 1 %d string("_test1") +0004 SEND_VAL int(5) 1 +0005 T2 = DO_UCALL +0006 INIT_METHOD_CALL 1 CV0($o) string("foo") +0007 SEND_VAL_EX T2 1 +0008 V3 = DO_FCALL +0009 T2 = QM_ASSIGN V3 +0010 INIT_STATIC_METHOD_CALL 1 string("Other") string("bar") +0011 SEND_VAL T2 1 +0012 V2 = DO_UCALL +0013 ASSIGN CV1($res1) V2 +0014 INIT_FCALL 1 %d string("var_dump") +0015 SEND_VAR CV1($res1) 1 +0016 DO_ICALL +0017 RETURN int(1) +LIVE RANGES: + 2: 0001 - 0002 (new) + 2: 0010 - 0011 (tmp/var) + +_test1: + ; (lines=4, args=1, vars=1, tmps=1) + ; (after optimizer) + ; %s:3-5 +0000 CV0($a) = RECV 1 +0001 T1 = ADD CV0($a) int(1) +0002 VERIFY_RETURN_TYPE T1 +0003 RETURN T1 + +Other::foo: + ; (lines=4, args=1, vars=1, tmps=1) + ; (after optimizer) + ; %s:8-10 +0000 CV0($a) = RECV 1 +0001 T1 = ADD CV0($a) CV0($a) +0002 VERIFY_RETURN_TYPE T1 +0003 RETURN T1 + +Other::bar: + ; (lines=4, args=1, vars=1, tmps=1) + ; (after optimizer) + ; %s:12-14 +0000 CV0($a) = RECV 1 +0001 T1 = SUB CV0($a) int(1) +0002 VERIFY_RETURN_TYPE T1 +0003 RETURN T1 +int(11) diff --git a/Zend/tests/pipe_operator/optional_parameters.phpt b/Zend/tests/pipe_operator/optional_parameters.phpt new file mode 100644 index 0000000000000..53cce2e9972e8 --- /dev/null +++ b/Zend/tests/pipe_operator/optional_parameters.phpt @@ -0,0 +1,15 @@ +--TEST-- +Pipe operator accepts optional-parameter functions +--FILE-- + '_test'; + +var_dump($res1); +?> +--EXPECT-- +int(8) diff --git a/Zend/tests/pipe_operator/precedence_addition.phpt b/Zend/tests/pipe_operator/precedence_addition.phpt new file mode 100644 index 0000000000000..4fd64639b4518 --- /dev/null +++ b/Zend/tests/pipe_operator/precedence_addition.phpt @@ -0,0 +1,16 @@ +--TEST-- +Pipe binds lower than addition +--FILE-- + '_test1'; + +var_dump($res1); +?> +--EXPECT-- +int(14) diff --git a/Zend/tests/pipe_operator/precedence_coalesce.phpt b/Zend/tests/pipe_operator/precedence_coalesce.phpt new file mode 100644 index 0000000000000..daf699d9fe85d --- /dev/null +++ b/Zend/tests/pipe_operator/precedence_coalesce.phpt @@ -0,0 +1,17 @@ +--TEST-- +Pipe binds higher than coalesce +--FILE-- + get_username(...) + ?? 'default'; + +var_dump($user); +?> +--EXPECT-- +string(1) "5" diff --git a/Zend/tests/pipe_operator/precedence_comparison.phpt b/Zend/tests/pipe_operator/precedence_comparison.phpt new file mode 100644 index 0000000000000..84f71b74058a0 --- /dev/null +++ b/Zend/tests/pipe_operator/precedence_comparison.phpt @@ -0,0 +1,16 @@ +--TEST-- +Pipe binds higher than comparison +--FILE-- + _test1(...) == 10 ; +var_dump($res1); + +?> +--EXPECTF-- +bool(true) diff --git a/Zend/tests/pipe_operator/precedence_ternary.phpt b/Zend/tests/pipe_operator/precedence_ternary.phpt new file mode 100644 index 0000000000000..207d15dab9d65 --- /dev/null +++ b/Zend/tests/pipe_operator/precedence_ternary.phpt @@ -0,0 +1,36 @@ +--TEST-- +Pipe binds higher than ternary +--FILE-- + is_odd(...) ? 'odd' : 'even'; +var_dump($res1); + +// The pipe binds first, resulting in bool ? int : string, which is well-understood. +$x = true; +$y = 'beep'; +$z = 'default'; +$ret3 = $x ? $y |> strlen(...) : $z; +var_dump($ret3); + + +?> +--EXPECT-- +string(3) "odd" +int(4) diff --git a/Zend/tests/pipe_operator/simple_builtin_call.phpt b/Zend/tests/pipe_operator/simple_builtin_call.phpt new file mode 100644 index 0000000000000..72f5968dd0b65 --- /dev/null +++ b/Zend/tests/pipe_operator/simple_builtin_call.phpt @@ -0,0 +1,11 @@ +--TEST-- +Pipe operator supports built-in functions +--FILE-- + 'strlen'; + +var_dump($res1); +?> +--EXPECT-- +int(5) diff --git a/Zend/tests/pipe_operator/simple_userland_call.phpt b/Zend/tests/pipe_operator/simple_userland_call.phpt new file mode 100644 index 0000000000000..7f311f9a10474 --- /dev/null +++ b/Zend/tests/pipe_operator/simple_userland_call.phpt @@ -0,0 +1,15 @@ +--TEST-- +Pipe operator supports user-defined functions +--FILE-- + '_test'; + +var_dump($res1); +?> +--EXPECT-- +int(6) diff --git a/Zend/tests/pipe_operator/too_many_parameters.phpt b/Zend/tests/pipe_operator/too_many_parameters.phpt new file mode 100644 index 0000000000000..b36046bde05c2 --- /dev/null +++ b/Zend/tests/pipe_operator/too_many_parameters.phpt @@ -0,0 +1,21 @@ +--TEST-- +Pipe operator fails on multi-parameter functions +--FILE-- + '_test'; +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + + +?> +--EXPECTF-- +ArgumentCountError: Too few arguments to function %s, 1 passed in %s on line %s and exactly 2 expected diff --git a/Zend/tests/pipe_operator/type_mismatch.phpt b/Zend/tests/pipe_operator/type_mismatch.phpt new file mode 100644 index 0000000000000..2cee15bb47a0d --- /dev/null +++ b/Zend/tests/pipe_operator/type_mismatch.phpt @@ -0,0 +1,20 @@ +--TEST-- +Pipe operator respects types +--FILE-- + '_test'; + var_dump($res1); +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +?> +--EXPECTF-- +TypeError: _test(): Argument #1 ($a) must be of type int, string given, called in %s on line %d diff --git a/Zend/tests/pipe_operator/void_return.phpt b/Zend/tests/pipe_operator/void_return.phpt new file mode 100644 index 0000000000000..0dbba519297cd --- /dev/null +++ b/Zend/tests/pipe_operator/void_return.phpt @@ -0,0 +1,21 @@ +--TEST-- +Pipe operator fails void return chaining in strict mode +--FILE-- + 'nonReturnFunction' + |> 'strlen'; + var_dump($result); +} +catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +TypeError: strlen(): Argument #1 ($string) must be of type string, null given diff --git a/Zend/tests/pipe_operator/wrapped_chains.phpt b/Zend/tests/pipe_operator/wrapped_chains.phpt new file mode 100644 index 0000000000000..e2b6f39f7f342 --- /dev/null +++ b/Zend/tests/pipe_operator/wrapped_chains.phpt @@ -0,0 +1,21 @@ +--TEST-- +Pipe operator chains saved as a closure +--FILE-- + $x |> '_test1' |> '_test2'; + +$res1 = $func(5); + +var_dump($res1); +?> +--EXPECT-- +int(12) diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 6a2826160e9c0..beecf51216a94 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -2518,6 +2518,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio case ZEND_AST_GREATER_EQUAL: BINARY_OP(" >= ", 180, 181, 181); case ZEND_AST_AND: BINARY_OP(" && ", 130, 130, 131); case ZEND_AST_OR: BINARY_OP(" || ", 120, 120, 121); + case ZEND_AST_PIPE: BINARY_OP(" |> ", 183, 183, 184); case ZEND_AST_ARRAY_ELEM: if (ast->child[1]) { zend_ast_export_ex(str, ast->child[1], 80, indent); diff --git a/Zend/zend_ast.h b/Zend/zend_ast.h index 9348c35f6cc07..c82ca66c9f573 100644 --- a/Zend/zend_ast.h +++ b/Zend/zend_ast.h @@ -154,6 +154,7 @@ enum _zend_ast_kind { ZEND_AST_MATCH_ARM, ZEND_AST_NAMED_ARG, ZEND_AST_PARENT_PROPERTY_HOOK_CALL, + ZEND_AST_PIPE, /* 3 child nodes */ ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT, diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 379a97733d0ed..2bc0cf7b703d9 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6427,6 +6427,57 @@ static bool can_match_use_jumptable(zend_ast_list *arms) { return 1; } +static void zend_compile_pipe(znode *result, zend_ast *ast) +{ + zend_ast *operand_ast = ast->child[0]; + zend_ast *callable_ast = ast->child[1]; + + /* Compile the left hand side down to a value first. */ + znode operand_result; + zend_compile_expr(&operand_result, operand_ast); + + /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references + * always fail. They will already fail in complex cases like arrays, + * so those don't need a wrapper. */ + znode wrapped_operand_result; + if (operand_result.op_type & (IS_CV|IS_VAR)) { + zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL); + } else { + wrapped_operand_result = operand_result; + } + + /* Turn the operand into a function parameter list. */ + zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result)); + + zend_ast *fcall_ast; + znode callable_result; + + /* Turn $foo |> bar(...) into bar($foo). */ + if (callable_ast->kind == ZEND_AST_CALL + && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) { + fcall_ast = zend_ast_create(ZEND_AST_CALL, + callable_ast->child[0], arg_list_ast); + /* Turn $foo |> bar::baz(...) into bar::baz($foo). */ + } else if (callable_ast->kind == ZEND_AST_STATIC_CALL + && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) { + fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL, + callable_ast->child[0], callable_ast->child[1], arg_list_ast); + /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */ + } else if (callable_ast->kind == ZEND_AST_METHOD_CALL + && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) { + fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL, + callable_ast->child[0], callable_ast->child[1], arg_list_ast); + /* Turn $foo |> $expr into ($expr)($foo) */ + } else { + zend_compile_expr(&callable_result, callable_ast); + callable_ast = zend_ast_create_znode(&callable_result); + fcall_ast = zend_ast_create(ZEND_AST_CALL, + callable_ast, arg_list_ast); + } + + zend_compile_expr(result, fcall_ast); +} + static void zend_compile_match(znode *result, zend_ast *ast) { zend_ast *expr_ast = ast->child[0]; @@ -11769,6 +11820,9 @@ static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */ case ZEND_AST_MATCH: zend_compile_match(result, ast); return; + case ZEND_AST_PIPE: + zend_compile_pipe(result, ast); + return; default: ZEND_ASSERT(0 /* not supported */); } diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index 08b2ac6b3f39b..816b8126cbf25 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -71,6 +71,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %left T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG %nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP %nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL +%left T_PIPE %left '.' %left T_SL T_SR %left '+' '-' @@ -237,6 +238,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %token T_COALESCE "'??'" %token T_POW "'**'" %token T_POW_EQUAL "'**='" +%token T_PIPE "'|>'" /* We need to split the & token in two to avoid a shift/reduce conflict. For T1&$v and T1&T2, * with only one token lookahead, bison does not know whether to reduce T1 as a complete type, * or shift to continue parsing an intersection type. */ @@ -1292,6 +1294,8 @@ expr: { $$ = zend_ast_create_binary_op(ZEND_IS_EQUAL, $1, $3); } | expr T_IS_NOT_EQUAL expr { $$ = zend_ast_create_binary_op(ZEND_IS_NOT_EQUAL, $1, $3); } + | expr T_PIPE expr + { $$ = zend_ast_create(ZEND_AST_PIPE, $1, $3); } | expr '<' expr { $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER, $1, $3); } | expr T_IS_SMALLER_OR_EQUAL expr diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index 4c883b81c5f7d..5e377249422a5 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -1861,6 +1861,10 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_ RETURN_TOKEN(T_COALESCE_EQUAL); } +"|>" { + RETURN_TOKEN(T_PIPE); +} + "||" { RETURN_TOKEN(T_BOOLEAN_OR); } diff --git a/ext/tokenizer/tokenizer_data.c b/ext/tokenizer/tokenizer_data.c index a1e131032bcfb..0900c51d3d95a 100644 --- a/ext/tokenizer/tokenizer_data.c +++ b/ext/tokenizer/tokenizer_data.c @@ -173,6 +173,7 @@ char *get_token_type_name(int token_type) case T_COALESCE: return "T_COALESCE"; case T_POW: return "T_POW"; case T_POW_EQUAL: return "T_POW_EQUAL"; + case T_PIPE: return "T_PIPE"; case T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG: return "T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG"; case T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG: return "T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG"; case T_BAD_CHARACTER: return "T_BAD_CHARACTER"; diff --git a/ext/tokenizer/tokenizer_data.stub.php b/ext/tokenizer/tokenizer_data.stub.php index c1e1fd254dfaa..57c8edad8acb6 100644 --- a/ext/tokenizer/tokenizer_data.stub.php +++ b/ext/tokenizer/tokenizer_data.stub.php @@ -742,6 +742,11 @@ * @cvalue T_POW_EQUAL */ const T_POW_EQUAL = UNKNOWN; +/** + * @var int + * @cvalue T_PIPE + */ +const T_PIPE = UNKNOWN; /** * @var int * @cvalue T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG diff --git a/ext/tokenizer/tokenizer_data_arginfo.h b/ext/tokenizer/tokenizer_data_arginfo.h index 9c488d19f1890..3a3cdaa468133 100644 --- a/ext/tokenizer/tokenizer_data_arginfo.h +++ b/ext/tokenizer/tokenizer_data_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 19d25d22098f46283b517352cbb302db962b50fd */ + * Stub hash: c5235344b7c651d27c2c33c90696a418a9c96837 */ static void register_tokenizer_data_symbols(int module_number) { @@ -151,6 +151,7 @@ static void register_tokenizer_data_symbols(int module_number) REGISTER_LONG_CONSTANT("T_COALESCE", T_COALESCE, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_POW", T_POW, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_POW_EQUAL", T_POW_EQUAL, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("T_PIPE", T_PIPE, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG", T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG", T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_BAD_CHARACTER", T_BAD_CHARACTER, CONST_PERSISTENT); From 7d24cce78abed57a9be9771dd4bc78d3f4cec3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Tue, 10 Jun 2025 10:07:11 +0200 Subject: [PATCH 30/44] Update Lexbor Cherry-pick https://github.com/lexbor/lexbor/commit/b2dbadcf16ce08588fe7a0f624d64c190b1e83a5 Adding support for IDNA URL serialization. --- ext/lexbor/lexbor/url/url.c | 27 +++++++++++++++++++++++---- ext/lexbor/lexbor/url/url.h | 4 ++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ext/lexbor/lexbor/url/url.c b/ext/lexbor/lexbor/url/url.c index dcea861f7cb66..bbb3b5bbd3cb7 100644 --- a/ext/lexbor/lexbor/url/url.c +++ b/ext/lexbor/lexbor/url/url.c @@ -4442,9 +4442,9 @@ lxb_url_api_hash_set(lxb_url_t *url, lxb_url_parser_t *parser, return status; } -lxb_status_t -lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx, - bool exclude_fragment) +static lxb_status_t +lxb_url_serialize_body(lxb_unicode_idna_t *idna, const lxb_url_t *url, lexbor_serialize_cb_f cb, + void *ctx, bool exclude_fragment) { lxb_status_t status; const lexbor_str_t *str; @@ -4484,7 +4484,12 @@ lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx, lexbor_serialize_write(cb, at_str.data, at_str.length, ctx, status); } - status = lxb_url_serialize_host(&url->host, cb, ctx); + if (idna != NULL) { + status = lxb_url_serialize_host_unicode(idna, &url->host, cb, ctx); + } else { + status = lxb_url_serialize_host(&url->host, cb, ctx); + } + if (status != LXB_STATUS_OK) { return status; } @@ -4529,6 +4534,20 @@ lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx, return LXB_STATUS_OK; } +lxb_status_t +lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx, + bool exclude_fragment) +{ + return lxb_url_serialize_body(NULL, url, cb, ctx, exclude_fragment); +} + +lxb_status_t +lxb_url_serialize_idna(lxb_unicode_idna_t *idna, const lxb_url_t *url, lexbor_serialize_cb_f cb, + void *ctx, bool exclude_fragment) +{ + return lxb_url_serialize_body(idna, url, cb, ctx, exclude_fragment); +} + lxb_status_t lxb_url_serialize_scheme(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx) diff --git a/ext/lexbor/lexbor/url/url.h b/ext/lexbor/lexbor/url/url.h index 2bf2d7c3f5918..58952a0f910b6 100644 --- a/ext/lexbor/lexbor/url/url.h +++ b/ext/lexbor/lexbor/url/url.h @@ -403,6 +403,10 @@ LXB_API lxb_status_t lxb_url_serialize(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx, bool exclude_fragment); +LXB_API lxb_status_t +lxb_url_serialize_idna(lxb_unicode_idna_t *idna, const lxb_url_t *url, lexbor_serialize_cb_f cb, + void *ctx, bool exclude_fragment); + LXB_API lxb_status_t lxb_url_serialize_scheme(const lxb_url_t *url, lexbor_serialize_cb_f cb, void *ctx); From 3399235bec275516f6b87714087526d36d6f6976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Tue, 10 Jun 2025 10:18:22 +0200 Subject: [PATCH 31/44] Add Uri\WhatWg classes to ext/uri (#18672) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Relates to #14461 and https://wiki.php.net/rfc/url_parsing_api Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Co-authored-by: Tim Düsterhus --- UPGRADING.INTERNALS | 3 + Zend/zend_exceptions.c | 61 ++-- Zend/zend_exceptions.h | 2 + Zend/zend_string.h | 2 + build/gen_stub.php | 2 + ext/uri/config.m4 | 4 +- ext/uri/config.w32 | 4 +- ext/uri/php_lexbor.c | 639 ++++++++++++++++++++++++++++++++++++++ ext/uri/php_lexbor.h | 30 ++ ext/uri/php_uri.c | 614 +++++++++++++++++++++++++++++++++++- ext/uri/php_uri.h | 3 + ext/uri/php_uri.stub.php | 110 +++++++ ext/uri/php_uri_arginfo.h | 290 ++++++++++++++++- ext/uri/php_uri_common.c | 169 ++++++++++ ext/uri/php_uri_common.h | 138 ++++++++ ext/uri/tests/003.phpt | 32 ++ ext/uri/tests/004.phpt | 25 ++ ext/uri/tests/005.phpt | 38 +++ ext/uri/tests/006.phpt | 30 ++ ext/uri/tests/007.phpt | 63 ++++ ext/uri/tests/008.phpt | 34 ++ ext/uri/tests/009.phpt | 29 ++ ext/uri/tests/010.phpt | 48 +++ ext/uri/tests/011.phpt | 22 ++ ext/uri/tests/012.phpt | 49 +++ ext/uri/tests/013.phpt | 87 ++++++ ext/uri/tests/014.phpt | 12 + ext/uri/tests/015.phpt | 18 ++ ext/uri/tests/018.phpt | 50 +++ ext/uri/tests/019.phpt | 55 ++++ ext/uri/tests/022.phpt | 14 + ext/uri/tests/023.phpt | 31 ++ ext/uri/tests/024.phpt | 29 ++ ext/uri/tests/025.phpt | 29 ++ ext/uri/tests/026.phpt | 67 ++++ ext/uri/tests/027.phpt | 36 +++ ext/uri/tests/028.phpt | 37 +++ ext/uri/tests/029.phpt | 40 +++ ext/uri/tests/030.phpt | 31 ++ ext/uri/tests/031.phpt | 98 ++++++ ext/uri/tests/032.phpt | 13 + ext/uri/tests/033.phpt | 15 + ext/uri/tests/034.phpt | 14 + ext/uri/tests/035.phpt | 24 ++ ext/uri/tests/036.phpt | 24 ++ ext/uri/tests/038.phpt | 26 ++ ext/uri/tests/039.phpt | 34 ++ ext/uri/tests/040.phpt | 32 ++ ext/uri/tests/041.phpt | 26 ++ ext/uri/tests/042.phpt | 29 ++ ext/uri/tests/043.phpt | 71 +++++ ext/uri/tests/045.phpt | 16 + ext/uri/tests/046.phpt | 30 ++ ext/uri/tests/047.phpt | 27 ++ ext/uri/tests/049.phpt | 13 + ext/uri/tests/050.phpt | 16 + ext/uri/tests/051.phpt | 35 +++ ext/uri/tests/052.phpt | 28 ++ ext/uri/tests/053.phpt | 63 ++++ 59 files changed, 3565 insertions(+), 46 deletions(-) create mode 100644 ext/uri/php_lexbor.c create mode 100644 ext/uri/php_lexbor.h create mode 100644 ext/uri/php_uri_common.c create mode 100644 ext/uri/php_uri_common.h create mode 100644 ext/uri/tests/003.phpt create mode 100644 ext/uri/tests/004.phpt create mode 100644 ext/uri/tests/005.phpt create mode 100644 ext/uri/tests/006.phpt create mode 100644 ext/uri/tests/007.phpt create mode 100644 ext/uri/tests/008.phpt create mode 100644 ext/uri/tests/009.phpt create mode 100644 ext/uri/tests/010.phpt create mode 100644 ext/uri/tests/011.phpt create mode 100644 ext/uri/tests/012.phpt create mode 100644 ext/uri/tests/013.phpt create mode 100644 ext/uri/tests/014.phpt create mode 100644 ext/uri/tests/015.phpt create mode 100644 ext/uri/tests/018.phpt create mode 100644 ext/uri/tests/019.phpt create mode 100644 ext/uri/tests/022.phpt create mode 100644 ext/uri/tests/023.phpt create mode 100644 ext/uri/tests/024.phpt create mode 100644 ext/uri/tests/025.phpt create mode 100644 ext/uri/tests/026.phpt create mode 100644 ext/uri/tests/027.phpt create mode 100644 ext/uri/tests/028.phpt create mode 100644 ext/uri/tests/029.phpt create mode 100644 ext/uri/tests/030.phpt create mode 100644 ext/uri/tests/031.phpt create mode 100644 ext/uri/tests/032.phpt create mode 100644 ext/uri/tests/033.phpt create mode 100644 ext/uri/tests/034.phpt create mode 100644 ext/uri/tests/035.phpt create mode 100644 ext/uri/tests/036.phpt create mode 100644 ext/uri/tests/038.phpt create mode 100644 ext/uri/tests/039.phpt create mode 100644 ext/uri/tests/040.phpt create mode 100644 ext/uri/tests/041.phpt create mode 100644 ext/uri/tests/042.phpt create mode 100644 ext/uri/tests/043.phpt create mode 100644 ext/uri/tests/045.phpt create mode 100644 ext/uri/tests/046.phpt create mode 100644 ext/uri/tests/047.phpt create mode 100644 ext/uri/tests/049.phpt create mode 100644 ext/uri/tests/050.phpt create mode 100644 ext/uri/tests/051.phpt create mode 100644 ext/uri/tests/052.phpt create mode 100644 ext/uri/tests/053.phpt diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 574d5f0827ff7..e4cf9e9c94b0d 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -34,6 +34,9 @@ PHP 8.5 INTERNALS UPGRADE NOTES . Added ZEND_NONSTRING attribute macro for character arrays that do not represent strings. This allows to silence the GCC 15.x `-Wunterminated-string-initialization` warning. + . Added the zend_update_exception_properties() function for instantiating + Exception child classes. It updates the $message, $code, and $previous + properties. ======================== 2. Build system changes diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 7777c5fa62e48..ab9c815718a0d 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -329,24 +329,15 @@ ZEND_COLD ZEND_METHOD(Exception, __clone) } /* }}} */ -/* {{{ Exception constructor */ -ZEND_METHOD(Exception, __construct) +ZEND_API zend_result zend_update_exception_properties(INTERNAL_FUNCTION_PARAMETERS, zend_string *message, zend_long code, zval *previous) { - zend_string *message = NULL; - zend_long code = 0; - zval tmp, *object, *previous = NULL; - - object = ZEND_THIS; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) { - RETURN_THROWS(); - } + zval tmp, *object = ZEND_THIS; if (message) { ZVAL_STR_COPY(&tmp, message); zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp); if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); + return FAILURE; } } @@ -354,7 +345,7 @@ ZEND_METHOD(Exception, __construct) ZVAL_LONG(&tmp, code); zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp); if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); + return FAILURE; } } @@ -362,9 +353,27 @@ ZEND_METHOD(Exception, __construct) Z_ADDREF_P(previous); zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous); if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); + return FAILURE; } } + + return SUCCESS; +} + +/* {{{ Exception constructor */ +ZEND_METHOD(Exception, __construct) +{ + zend_string *message = NULL; + zend_long code = 0; + zval *previous = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) { + RETURN_THROWS(); + } + + if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) { + RETURN_THROWS(); + } } /* }}} */ @@ -401,28 +410,8 @@ ZEND_METHOD(ErrorException, __construct) object = ZEND_THIS; - if (message) { - ZVAL_STR_COPY(&tmp, message); - zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_MESSAGE_OFF, ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp); - if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); - } - } - - if (code) { - ZVAL_LONG(&tmp, code); - zend_update_property_num_checked(NULL, Z_OBJ_P(object), ZEND_EXCEPTION_CODE_OFF, ZSTR_KNOWN(ZEND_STR_CODE), &tmp); - if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); - } - } - - if (previous) { - Z_ADDREF_P(previous); - zend_update_property_num_checked(zend_ce_exception, Z_OBJ_P(object), ZEND_EXCEPTION_PREVIOUS_OFF, ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous); - if (UNEXPECTED(EG(exception))) { - RETURN_THROWS(); - } + if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) { + RETURN_THROWS(); } ZVAL_LONG(&tmp, severity); diff --git a/Zend/zend_exceptions.h b/Zend/zend_exceptions.h index d0138021d1ea3..86dc379cce871 100644 --- a/Zend/zend_exceptions.h +++ b/Zend/zend_exceptions.h @@ -69,6 +69,8 @@ ZEND_API zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, extern ZEND_API void (*zend_throw_exception_hook)(zend_object *ex); +ZEND_API zend_result zend_update_exception_properties(INTERNAL_FUNCTION_PARAMETERS, zend_string *message, zend_long code, zval *previous); + /* show an exception using zend_error(severity,...), severity should be E_ERROR */ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity); ZEND_NORETURN void zend_exception_uncaught_error(const char *prefix, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2); diff --git a/Zend/zend_string.h b/Zend/zend_string.h index e9e2b947a6c91..0b2a484016ec3 100644 --- a/Zend/zend_string.h +++ b/Zend/zend_string.h @@ -597,7 +597,9 @@ EMPTY_SWITCH_DEFAULT_CASE() _(ZEND_STR_HOST, "host") \ _(ZEND_STR_PORT, "port") \ _(ZEND_STR_USER, "user") \ + _(ZEND_STR_USERNAME, "username") \ _(ZEND_STR_PASS, "pass") \ + _(ZEND_STR_PASSWORD, "password") \ _(ZEND_STR_PATH, "path") \ _(ZEND_STR_QUERY, "query") \ _(ZEND_STR_FRAGMENT, "fragment") \ diff --git a/build/gen_stub.php b/build/gen_stub.php index 13ef9e60f334d..0e87cdd9a0b40 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -3055,6 +3055,8 @@ class PropertyInfo extends VariableLike private const PHP_85_KNOWN = [ "self" => "ZEND_STR_SELF", "parent" => "ZEND_STR_PARENT", + "username" => "ZEND_STR_USERNAME", + "password" => "ZEND_STR_PASSWORD", ]; /** diff --git a/ext/uri/config.m4 b/ext/uri/config.m4 index f29bbe58bd32e..08dc044d8d29f 100644 --- a/ext/uri/config.m4 +++ b/ext/uri/config.m4 @@ -2,7 +2,9 @@ dnl Configure options dnl PHP_INSTALL_HEADERS([ext/uri], m4_normalize([ + php_lexbor.h php_uri.h + php_uri_common.h ])) AC_DEFINE([URI_ENABLE_ANSI], [1], [Define to 1 for enabling ANSI support of uriparser.]) @@ -15,6 +17,6 @@ $URIPARSER_DIR/src/UriMemory.c $URIPARSER_DIR/src/UriNormalize.c $URIPARSER_DIR/ $URIPARSER_DIR/src/UriParse.c $URIPARSER_DIR/src/UriParseBase.c $URIPARSER_DIR/src/UriQuery.c \ $URIPARSER_DIR/src/UriRecompose.c $URIPARSER_DIR/src/UriResolve.c $URIPARSER_DIR/src/UriShorten.c" -PHP_NEW_EXTENSION(uri, [php_uri.c $URIPARSER_SOURCES], [no],,[-I$ext_srcdir/$URIPARSER_DIR/include -DURI_STATIC_BUILD -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1]) +PHP_NEW_EXTENSION(uri, [php_lexbor.c php_uri.c php_uri_common.c $URIPARSER_SOURCES], [no],,[-I$ext_srcdir/$URIPARSER_DIR/include -DURI_STATIC_BUILD -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1]) PHP_ADD_EXTENSION_DEP(uri, lexbor) PHP_ADD_BUILD_DIR($ext_builddir/$URIPARSER_DIR/src $ext_builddir/$URIPARSER_DIR/include) diff --git a/ext/uri/config.w32 b/ext/uri/config.w32 index 962f7bee0660e..9c6af0cc5fa7b 100644 --- a/ext/uri/config.w32 +++ b/ext/uri/config.w32 @@ -1,4 +1,4 @@ -EXTENSION("uri", "php_uri.c", false /* never shared */, "/I ext/lexbor /I ext/uri/uriparser/include /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); +EXTENSION("uri", "php_lexbor.c php_uri.c php_uri_common.c", false /* never shared */, "/I ext/lexbor /I ext/uri/uriparser/include /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); AC_DEFINE("URI_ENABLE_ANSI", 1, "Define to 1 for enabling ANSI support of uriparser.") AC_DEFINE("URI_NO_UNICODE", 1, "Define to 1 for disabling unicode support of uriparser.") @@ -6,4 +6,4 @@ ADD_FLAG("CFLAGS_URI", "/D URI_STATIC_BUILD"); ADD_EXTENSION_DEP('uri', 'lexbor'); ADD_SOURCES("ext/uri/uriparser/src", "UriCommon.c UriCompare.c UriEscape.c UriFile.c UriIp4.c UriIp4Base.c UriMemory.c UriNormalize.c UriNormalizeBase.c UriParse.c UriParseBase.c UriQuery.c UriRecompose.c UriShorten.c", "uri"); -PHP_INSTALL_HEADERS("ext/uri", "php_uri.h uriparser/src uriparser/include"); +PHP_INSTALL_HEADERS("ext/uri", "php_lexbor.h php_uri.h php_uri_common.h uriparser/src uriparser/include"); diff --git a/ext/uri/php_lexbor.c b/ext/uri/php_lexbor.c new file mode 100644 index 0000000000000..82f3919bb6a97 --- /dev/null +++ b/ext/uri/php_lexbor.c @@ -0,0 +1,639 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Máté Kocsis | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_lexbor.h" +#include "php_uri_common.h" +#include "Zend/zend_enum.h" +#include "Zend/zend_smart_str.h" +#include "Zend/zend_exceptions.h" +#ifdef HAVE_ARPA_INET_H +#include +#endif + +ZEND_TLS lxb_url_parser_t lexbor_parser; +ZEND_TLS unsigned short int lexbor_urls; + +#define LEXBOR_MAX_URL_COUNT 500 +#define LEXBOR_MRAW_BYTE_SIZE 8192 + +static zend_always_inline void zval_string_or_null_to_lexbor_str(zval *value, lexbor_str_t *lexbor_str) +{ + if (Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) > 0) { + lexbor_str->data = (lxb_char_t *) Z_STRVAL_P(value); + lexbor_str->length = Z_STRLEN_P(value); + } else { + ZEND_ASSERT(Z_ISNULL_P(value) || (Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) == 0)); + lexbor_str->data = (lxb_char_t *) ""; + lexbor_str->length = 0; + } +} + +static zend_always_inline void zval_long_or_null_to_lexbor_str(zval *value, lexbor_str_t *lexbor_str) +{ + if (Z_TYPE_P(value) == IS_LONG) { + ZVAL_STR(value, zend_long_to_str(Z_LVAL_P(value))); + lexbor_str_init_append(lexbor_str, lexbor_parser.mraw, (const lxb_char_t *) Z_STRVAL_P(value), Z_STRLEN_P(value)); + zval_ptr_dtor_str(value); + } else { + ZEND_ASSERT(Z_ISNULL_P(value)); + lexbor_str->data = (lxb_char_t *) ""; + lexbor_str->length = 0; + } +} + +static void lexbor_cleanup_parser(void) +{ + if (++lexbor_urls % LEXBOR_MAX_URL_COUNT == 0) { + lexbor_mraw_clean(lexbor_parser.mraw); + lexbor_urls = 0; + } + + lxb_url_parser_clean(&lexbor_parser); +} + +/** + * Creates a Uri\WhatWg\UrlValidationError class by mapping error codes listed in + * https://url.spec.whatwg.org/#writing to a Uri\WhatWg\UrlValidationErrorType enum. + * The result is passed by reference to the errors parameter. + * + * When errors is NULL, the caller is not interested in the additional error information, + * so the function does nothing. + */ +static void fill_errors(zval *errors) +{ + if (errors == NULL) { + return; + } + + ZEND_ASSERT(Z_ISUNDEF_P(errors)); + + array_init(errors); + + if (lexbor_parser.log == NULL) { + return; + } + + lexbor_plog_entry_t *lxb_error; + while ((lxb_error = lexbor_array_obj_pop(&lexbor_parser.log->list)) != NULL) { + zval error; + object_init_ex(&error, uri_whatwg_url_validation_error_ce); + zend_update_property_string(uri_whatwg_url_validation_error_ce, Z_OBJ(error), ZEND_STRL("context"), (const char *) lxb_error->data); + + zend_string *error_str; + zval failure; + switch (lxb_error->id) { + case LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII: + error_str = ZSTR_INIT_LITERAL("DomainToAscii", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_DOMAIN_TO_UNICODE: + error_str = ZSTR_INIT_LITERAL("DomainToUnicode", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_DOMAIN_INVALID_CODE_POINT: + error_str = ZSTR_INIT_LITERAL("DomainInvalidCodePoint", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT: + error_str = ZSTR_INIT_LITERAL("HostInvalidCodePoint", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_EMPTY_PART: + error_str = ZSTR_INIT_LITERAL("Ipv4EmptyPart", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_TOO_MANY_PARTS: + error_str = ZSTR_INIT_LITERAL("Ipv4TooManyParts", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_NON_NUMERIC_PART: + error_str = ZSTR_INIT_LITERAL("Ipv4NonNumericPart", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_NON_DECIMAL_PART: + error_str = ZSTR_INIT_LITERAL("Ipv4NonDecimalPart", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_OUT_OF_RANGE_PART: + error_str = ZSTR_INIT_LITERAL("Ipv4OutOfRangePart", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_UNCLOSED: + error_str = ZSTR_INIT_LITERAL("Ipv6Unclosed", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_INVALID_COMPRESSION: + error_str = ZSTR_INIT_LITERAL("Ipv6InvalidCompression", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_TOO_MANY_PIECES: + error_str = ZSTR_INIT_LITERAL("Ipv6TooManyPieces", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_MULTIPLE_COMPRESSION: + error_str = ZSTR_INIT_LITERAL("Ipv6MultipleCompression", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_INVALID_CODE_POINT: + error_str = ZSTR_INIT_LITERAL("Ipv6InvalidCodePoint", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV6_TOO_FEW_PIECES: + error_str = ZSTR_INIT_LITERAL("Ipv6TooFewPieces", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_MANY_PIECES: + error_str = ZSTR_INIT_LITERAL("Ipv4InIpv6TooManyPieces", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_INVALID_CODE_POINT: + error_str = ZSTR_INIT_LITERAL("Ipv4InIpv6InvalidCodePoint", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_OUT_OF_RANGE_PART: + error_str = ZSTR_INIT_LITERAL("Ipv4InIpv6OutOfRangePart", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_FEW_PARTS: + error_str = ZSTR_INIT_LITERAL("Ipv4InIpv6TooFewParts", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_INVALID_URL_UNIT: + error_str = ZSTR_INIT_LITERAL("InvalidUrlUnit", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_SPECIAL_SCHEME_MISSING_FOLLOWING_SOLIDUS: + error_str = ZSTR_INIT_LITERAL("SpecialSchemeMissingFollowingSolidus", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_MISSING_SCHEME_NON_RELATIVE_URL: + error_str = ZSTR_INIT_LITERAL("MissingSchemeNonRelativeUrl", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_INVALID_REVERSE_SOLIDUS: + error_str = ZSTR_INIT_LITERAL("InvalidReverseSoldius", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_INVALID_CREDENTIALS: + error_str = ZSTR_INIT_LITERAL("InvalidCredentials", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_HOST_MISSING: + error_str = ZSTR_INIT_LITERAL("HostMissing", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_PORT_OUT_OF_RANGE: + error_str = ZSTR_INIT_LITERAL("PortOutOfRange", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_PORT_INVALID: + error_str = ZSTR_INIT_LITERAL("PortInvalid", false); + ZVAL_TRUE(&failure); + break; + case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER: + error_str = ZSTR_INIT_LITERAL("FileInvalidWindowsDriveLetter", false); + ZVAL_FALSE(&failure); + break; + case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER_HOST: + error_str = ZSTR_INIT_LITERAL("FileInvalidWindowsDriveLetterHost", false); + ZVAL_FALSE(&failure); + break; + EMPTY_SWITCH_DEFAULT_CASE() + } + + zval error_type; + zend_enum_new(&error_type, uri_whatwg_url_validation_error_type_ce, error_str, NULL); + zend_update_property_ex(uri_whatwg_url_validation_error_ce, Z_OBJ(error), ZSTR_KNOWN(ZEND_STR_TYPE), &error_type); + zend_string_release_ex(error_str, false); + zval_ptr_dtor(&error_type); + + zend_update_property(uri_whatwg_url_validation_error_ce, Z_OBJ(error), ZEND_STRL("failure"), &failure); + + add_next_index_zval(errors, &error); + } +} + +static void throw_invalid_url_exception(zval *errors) +{ + ZEND_ASSERT(errors != NULL && Z_TYPE_P(errors) == IS_ARRAY); + + zval exception; + + object_init_ex(&exception, uri_whatwg_invalid_url_exception_ce); + + zval value; + ZVAL_STRING(&value, "URL parsing failed"); + zend_update_property_ex(uri_whatwg_invalid_url_exception_ce, Z_OBJ(exception), ZSTR_KNOWN(ZEND_STR_MESSAGE), &value); + zval_ptr_dtor_str(&value); + + zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ(exception), ZEND_STRL("errors"), errors); + + zend_throw_exception_object(&exception); +} + +static void throw_invalid_url_exception_during_write(zval *errors) +{ + fill_errors(errors); + throw_invalid_url_exception(errors); +} + +static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx) +{ + smart_str *uri_str = ctx; + + if (data != NULL && length > 0) { + smart_str_appendl(uri_str, (const char *) data, length); + } + + return LXB_STATUS_OK; +} + +static zend_result lexbor_read_scheme(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + ZEND_ASSERT(lexbor_uri->scheme.type != LXB_URL_SCHEMEL_TYPE__UNDEF); + + ZVAL_STRINGL(retval, (const char *) lexbor_uri->scheme.name.data, lexbor_uri->scheme.name.length); + + return SUCCESS; +} + +static zend_result lexbor_write_scheme(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_protocol_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_username(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->username.length) { + ZVAL_STRINGL(retval, (const char *) lexbor_uri->username.data, lexbor_uri->username.length); + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_username_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_password(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->password.length > 0) { + ZVAL_STRINGL(retval, (const char *) lexbor_uri->password.data, lexbor_uri->password.length); + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_password(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_password_set(lexbor_uri, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result init_idna(void) +{ + if (lexbor_parser.idna != NULL) { + return SUCCESS; + } + + lexbor_parser.idna = lxb_unicode_idna_create(); + + return lxb_unicode_idna_init(lexbor_parser.idna) == LXB_STATUS_OK ? SUCCESS : FAILURE; +} + +static zend_result lexbor_read_host(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV4) { + smart_str host_str = {0}; + + lxb_url_serialize_host_ipv4(lexbor_uri->host.u.ipv4, lexbor_serialize_callback, &host_str); + + ZVAL_NEW_STR(retval, smart_str_extract(&host_str)); + } else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV6) { + smart_str host_str = {0}; + + smart_str_appendc(&host_str, '['); + lxb_url_serialize_host_ipv6(lexbor_uri->host.u.ipv6, lexbor_serialize_callback, &host_str); + smart_str_appendc(&host_str, ']'); + + ZVAL_NEW_STR(retval, smart_str_extract(&host_str)); + } else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE_EMPTY && lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) { + switch (read_mode) { + case URI_COMPONENT_READ_NORMALIZED_UNICODE: { + smart_str host_str = {0}; + if (init_idna() == FAILURE) { + return FAILURE; + } + lxb_url_serialize_host_unicode(lexbor_parser.idna, &lexbor_uri->host, lexbor_serialize_callback, &host_str); + lxb_unicode_idna_clean(lexbor_parser.idna); + + ZVAL_NEW_STR(retval, smart_str_extract(&host_str)); + break; + } + case URI_COMPONENT_READ_NORMALIZED_ASCII: + ZEND_FALLTHROUGH; + case URI_COMPONENT_READ_RAW: + ZVAL_STRINGL(retval, (const char *) lexbor_uri->host.u.domain.data, lexbor_uri->host.u.domain.length); + break; + EMPTY_SWITCH_DEFAULT_CASE() + } + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_host(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_hostname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_port(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->has_port) { + ZVAL_LONG(retval, lexbor_uri->port); + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_port(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_long_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_port_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_path(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->path.str.length) { + ZVAL_STRINGL(retval, (const char *) lexbor_uri->path.str.data, lexbor_uri->path.str.length); + } else { + ZVAL_EMPTY_STRING(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_path(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_pathname_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_query(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->query.length) { + ZVAL_STRINGL(retval, (const char *) lexbor_uri->query.data, lexbor_uri->query.length); + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_query(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_search_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +static zend_result lexbor_read_fragment(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + + if (lexbor_uri->fragment.length) { + ZVAL_STRINGL(retval, (const char *) lexbor_uri->fragment.data, lexbor_uri->fragment.length); + } else { + ZVAL_NULL(retval); + } + + return SUCCESS; +} + +static zend_result lexbor_write_fragment(struct uri_internal_t *internal_uri, zval *value, zval *errors) +{ + lxb_url_t *lexbor_uri = internal_uri->uri; + lexbor_str_t str = {0}; + + zval_string_or_null_to_lexbor_str(value, &str); + + if (lxb_url_api_hash_set(lexbor_uri, &lexbor_parser, str.data, str.length) != LXB_STATUS_OK) { + throw_invalid_url_exception_during_write(errors); + + return FAILURE; + } + + return SUCCESS; +} + +zend_result lexbor_request_init(void) +{ + lexbor_mraw_t *mraw = lexbor_mraw_create(); + lxb_status_t status = lexbor_mraw_init(mraw, LEXBOR_MRAW_BYTE_SIZE); + if (status != LXB_STATUS_OK) { + lexbor_mraw_destroy(mraw, true); + return FAILURE; + } + + status = lxb_url_parser_init(&lexbor_parser, mraw); + if (status != LXB_STATUS_OK) { + lxb_url_parser_destroy(&lexbor_parser, false); + lexbor_mraw_destroy(mraw, true); + return FAILURE; + } + + lexbor_urls = 0; + + return SUCCESS; +} + +void lexbor_request_shutdown(void) +{ + lxb_url_parser_memory_destroy(&lexbor_parser); + lxb_url_parser_destroy(&lexbor_parser, false); + + lexbor_urls = 0; +} + +lxb_url_t *lexbor_parse_uri_ex(const zend_string *uri_str, const lxb_url_t *lexbor_base_url, zval *errors, bool silent) +{ + lexbor_cleanup_parser(); + + lxb_url_t *url = lxb_url_parse(&lexbor_parser, lexbor_base_url, (unsigned char *) ZSTR_VAL(uri_str), ZSTR_LEN(uri_str)); + fill_errors(errors); + + if (url == NULL && !silent) { + throw_invalid_url_exception(errors); + } + + return url; +} + +static void *lexbor_parse_uri(const zend_string *uri_str, const void *base_url, zval *errors, bool silent) +{ + return lexbor_parse_uri_ex(uri_str, base_url, errors, silent); +} + +static void *lexbor_clone_uri(void *uri) +{ + lxb_url_t *lexbor_uri = (lxb_url_t *) uri; + + return lxb_url_clone(lexbor_parser.mraw, lexbor_uri); +} + +static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment) +{ + lxb_url_t *lexbor_uri = (lxb_url_t *) uri; + smart_str uri_str = {0}; + + switch (recomposition_mode) { + case URI_RECOMPOSITION_RAW_UNICODE: + ZEND_FALLTHROUGH; + case URI_RECOMPOSITION_NORMALIZED_UNICODE: + if (init_idna() == FAILURE) { + return NULL; + } + lxb_url_serialize_idna(lexbor_parser.idna, lexbor_uri, lexbor_serialize_callback, &uri_str, exclude_fragment); + lxb_unicode_idna_clean(lexbor_parser.idna); + break; + case URI_RECOMPOSITION_RAW_ASCII: + ZEND_FALLTHROUGH; + case URI_RECOMPOSITION_NORMALIZED_ASCII: + lxb_url_serialize(lexbor_uri, lexbor_serialize_callback, &uri_str, exclude_fragment); + break; + EMPTY_SWITCH_DEFAULT_CASE() + } + + return smart_str_extract(&uri_str); +} + +static void lexbor_free_uri(void *uri) +{ +} + +const uri_handler_t lexbor_uri_handler = { + .name = URI_PARSER_WHATWG, + .parse_uri = lexbor_parse_uri, + .clone_uri = lexbor_clone_uri, + .uri_to_string = lexbor_uri_to_string, + .free_uri = lexbor_free_uri, + { + .scheme = {.read_func = lexbor_read_scheme, .write_func = lexbor_write_scheme}, + .username = {.read_func = lexbor_read_username, .write_func = lexbor_write_username}, + .password = {.read_func = lexbor_read_password, .write_func = lexbor_write_password}, + .host = {.read_func = lexbor_read_host, .write_func = lexbor_write_host}, + .port = {.read_func = lexbor_read_port, .write_func = lexbor_write_port}, + .path = {.read_func = lexbor_read_path, .write_func = lexbor_write_path}, + .query = {.read_func = lexbor_read_query, .write_func = lexbor_write_query}, + .fragment = {.read_func = lexbor_read_fragment, .write_func = lexbor_write_fragment}, + } +}; diff --git a/ext/uri/php_lexbor.h b/ext/uri/php_lexbor.h new file mode 100644 index 0000000000000..30d68b5cdf681 --- /dev/null +++ b/ext/uri/php_lexbor.h @@ -0,0 +1,30 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Máté Kocsis | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_LEXBOR_H +#define PHP_LEXBOR_H + +#include "php_uri_common.h" +#include "lexbor/url/url.h" + +extern const uri_handler_t lexbor_uri_handler; + +lxb_url_t *lexbor_parse_uri_ex(const zend_string *uri_str, const lxb_url_t *lexbor_base_url, zval *errors, bool silent); + +zend_result lexbor_request_init(void); +void lexbor_request_shutdown(void); + +#endif diff --git a/ext/uri/php_uri.c b/ext/uri/php_uri.c index bd5c622ed3765..5b2e21b1625a3 100644 --- a/ext/uri/php_uri.c +++ b/ext/uri/php_uri.c @@ -22,16 +22,23 @@ #include "Zend/zend_interfaces.h" #include "Zend/zend_exceptions.h" #include "Zend/zend_attributes.h" -#include "main/php_ini.h" +#include "Zend/zend_enum.h" #include "ext/standard/info.h" #include "php_uri.h" +#include "php_uri_common.h" +#include "php_lexbor.h" #include "php_uri_arginfo.h" #include "uriparser/src/UriConfig.h" +zend_class_entry *uri_whatwg_url_ce; +zend_object_handlers uri_whatwg_uri_object_handlers; +zend_class_entry *uri_comparison_mode_ce; zend_class_entry *uri_exception_ce; -zend_class_entry *invalid_uri_exception_ce; -zend_class_entry *whatwg_invalid_url_exception_ce; +zend_class_entry *uri_invalid_uri_exception_ce; +zend_class_entry *uri_whatwg_invalid_url_exception_ce; +zend_class_entry *uri_whatwg_url_validation_error_type_ce; +zend_class_entry *uri_whatwg_url_validation_error_ce; #define URIPARSER_VERSION PACKAGE_VERSION @@ -40,12 +47,603 @@ static const zend_module_dep uri_deps[] = { ZEND_MOD_END }; +static zend_array uri_handlers; + +static uri_handler_t *uri_handler_by_name(const char *handler_name, size_t handler_name_len) +{ + return zend_hash_str_find_ptr(&uri_handlers, handler_name, handler_name_len); +} + +static HashTable *uri_get_debug_properties(zend_object *object) +{ + uri_internal_t *internal_uri = uri_internal_from_obj(object); + ZEND_ASSERT(internal_uri != NULL); + + HashTable *std_properties = zend_std_get_properties(object); + HashTable *result = zend_array_dup(std_properties); + + if (UNEXPECTED(internal_uri->uri == NULL)) { + return result; + } + + const uri_property_handlers_t property_handlers = internal_uri->handler->property_handlers; + + zval tmp; + if (property_handlers.scheme.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_SCHEME), &tmp); + } + + if (property_handlers.username.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_USERNAME), &tmp); + } + + if (property_handlers.password.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PASSWORD), &tmp); + } + + if (property_handlers.host.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_HOST), &tmp); + } + + if (property_handlers.port.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PORT), &tmp); + } + + if (property_handlers.path.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_PATH), &tmp); + } + + if (property_handlers.query.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_QUERY), &tmp); + } + + if (property_handlers.fragment.read_func(internal_uri, URI_COMPONENT_READ_RAW, &tmp) == SUCCESS) { + zend_hash_update(result, ZSTR_KNOWN(ZEND_STR_FRAGMENT), &tmp); + } + + return result; +} + +PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct) +{ + zend_string *message = NULL; + zval *errors = NULL; + zend_long code = 0; + zval *previous = NULL; + + ZEND_PARSE_PARAMETERS_START(0, 4) + Z_PARAM_OPTIONAL + Z_PARAM_STR(message) + Z_PARAM_ARRAY(errors) + Z_PARAM_LONG(code) + Z_PARAM_OBJECT_OF_CLASS_OR_NULL(previous, zend_ce_throwable) + ZEND_PARSE_PARAMETERS_END(); + + if (zend_update_exception_properties(INTERNAL_FUNCTION_PARAM_PASSTHRU, message, code, previous) == FAILURE) { + RETURN_THROWS(); + } + + if (errors == NULL) { + zval tmp; + ZVAL_EMPTY_ARRAY(&tmp); + zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp); + } else { + zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), errors); + } + if (EG(exception)) { + RETURN_THROWS(); + } +} + +PHP_METHOD(Uri_WhatWg_UrlValidationError, __construct) +{ + zend_string *context; + zval *type; + bool failure; + + ZEND_PARSE_PARAMETERS_START(3, 3) + Z_PARAM_STR(context) + Z_PARAM_OBJECT_OF_CLASS(type, uri_whatwg_url_validation_error_type_ce) + Z_PARAM_BOOL(failure) + ZEND_PARSE_PARAMETERS_END(); + + zend_update_property_str(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("context"), context); + if (EG(exception)) { + RETURN_THROWS(); + } + + zend_update_property_ex(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZSTR_KNOWN(ZEND_STR_TYPE), type); + if (EG(exception)) { + RETURN_THROWS(); + } + + zval failure_zv; + ZVAL_BOOL(&failure_zv, failure); + zend_update_property(uri_whatwg_url_validation_error_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("failure"), &failure_zv); + if (EG(exception)) { + RETURN_THROWS(); + } +} + +/** + * Pass the errors parameter by ref to errors_zv for userland, and frees it if + * it is not not needed anymore. + */ +static zend_result pass_errors_by_ref_and_free(zval *errors_zv, zval *errors) +{ + ZEND_ASSERT(Z_TYPE_P(errors) == IS_UNDEF || Z_TYPE_P(errors) == IS_ARRAY); + + /* There was no error during parsing */ + if (Z_ISUNDEF_P(errors)) { + return SUCCESS; + } + + /* The errors parameter is an array, but the pass-by ref argument stored by + * errors_zv was not passed - the URI implementation either doesn't support + * returning additional error information, or the caller is not interested in it */ + if (errors_zv == NULL) { + zval_ptr_dtor(errors); + return SUCCESS; + } + + ZEND_TRY_ASSIGN_REF_ARR(errors_zv, Z_ARRVAL_P(errors)); + if (EG(exception)) { + zval_ptr_dtor(errors); + return FAILURE; + } + + return SUCCESS; +} + +PHPAPI void php_uri_instantiate_uri( + INTERNAL_FUNCTION_PARAMETERS, const uri_handler_t *handler, const zend_string *uri_str, const zend_object *base_url_object, + bool should_throw, bool should_update_this_object, zval *errors_zv +) { + zval errors; + ZVAL_UNDEF(&errors); + + void *base_url = NULL; + if (base_url_object != NULL) { + uri_internal_t *internal_base_url = uri_internal_from_obj(base_url_object); + URI_ASSERT_INITIALIZATION(internal_base_url); + base_url = internal_base_url->uri; + } + + void *uri = handler->parse_uri(uri_str, base_url, should_throw || errors_zv != NULL ? &errors : NULL, !should_throw); + if (UNEXPECTED(uri == NULL)) { + if (should_throw) { + zval_ptr_dtor(&errors); + RETURN_THROWS(); + } else { + if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) { + RETURN_THROWS(); + } + RETURN_NULL(); + } + } + + if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) { + RETURN_THROWS(); + } + + uri_object_t *uri_object; + if (should_update_this_object) { + uri_object = Z_URI_OBJECT_P(ZEND_THIS); + } else { + if (EX(func)->common.fn_flags & ZEND_ACC_STATIC) { + object_init_ex(return_value, Z_CE_P(ZEND_THIS)); + } else { + object_init_ex(return_value, Z_OBJCE_P(ZEND_THIS)); + } + uri_object = Z_URI_OBJECT_P(return_value); + } + + uri_object->internal.handler = handler; + uri_object->internal.uri = uri; +} + +static void create_whatwg_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor) +{ + zend_string *uri_str; + zend_object *base_url_object = NULL; + zval *errors = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 3) + Z_PARAM_PATH_STR(uri_str) + Z_PARAM_OPTIONAL + Z_PARAM_OBJ_OF_CLASS_OR_NULL(base_url_object, uri_whatwg_url_ce) + Z_PARAM_ZVAL(errors) + ZEND_PARSE_PARAMETERS_END(); + + php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, &lexbor_uri_handler, uri_str, base_url_object, is_constructor, is_constructor, errors); +} + +PHP_METHOD(Uri_WhatWg_Url, parse) +{ + create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false); +} + +PHP_METHOD(Uri_WhatWg_Url, __construct) +{ + create_whatwg_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, true); +} + +static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, zend_object *that_object, zend_object *comparison_mode) +{ + zend_object *this_object = Z_OBJ_P(ZEND_THIS); + uri_internal_t *this_internal_uri = uri_internal_from_obj(this_object); + URI_ASSERT_INITIALIZATION(this_internal_uri); + + uri_internal_t *that_internal_uri = uri_internal_from_obj(that_object); + URI_ASSERT_INITIALIZATION(that_internal_uri); + + if (this_object->ce != that_object->ce && + !instanceof_function(this_object->ce, that_object->ce) && + !instanceof_function(that_object->ce, this_object->ce) + ) { + RETURN_FALSE; + } + + bool exclude_fragment = true; + if (comparison_mode) { + zval *case_name = zend_enum_fetch_case_name(comparison_mode); + exclude_fragment = zend_string_equals_literal(Z_STR_P(case_name), "ExcludeFragment"); + } + + zend_string *this_str = this_internal_uri->handler->uri_to_string( + this_internal_uri->uri, URI_RECOMPOSITION_NORMALIZED_ASCII, exclude_fragment); + if (this_str == NULL) { + zend_throw_exception_ex(NULL, 0, "Cannot recompose %s to string", ZSTR_VAL(this_object->ce->name)); + RETURN_THROWS(); + } + + zend_string *that_str = that_internal_uri->handler->uri_to_string( + that_internal_uri->uri, URI_RECOMPOSITION_NORMALIZED_ASCII, exclude_fragment); + if (that_str == NULL) { + zend_string_release(this_str); + zend_throw_exception_ex(NULL, 0, "Cannot recompose %s to string", ZSTR_VAL(that_object->ce->name)); + RETURN_THROWS(); + } + + RETVAL_BOOL(zend_string_equals(this_str, that_str)); + + zend_string_release(this_str); + zend_string_release(that_str); +} + +static void uri_unserialize(INTERNAL_FUNCTION_PARAMETERS, const char *handler_name) +{ + HashTable *data; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ARRAY_HT(data) + ZEND_PARSE_PARAMETERS_END(); + + zend_object *object = Z_OBJ_P(ZEND_THIS); + + /* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */ + if (zend_hash_num_elements(data) != 2) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + /* Unserialize state: "uri" key in the first array */ + zval *arr = zend_hash_index_find(data, 0); + if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + /* Verify the expected number of elements inside the first array, this implicitly ensures that no additional elements are present. */ + if (zend_hash_num_elements(Z_ARRVAL_P(arr)) != 1) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + zval *uri_zv = zend_hash_str_find_ind(Z_ARRVAL_P(arr), ZEND_STRL(URI_SERIALIZED_PROPERTY_NAME)); + if (uri_zv == NULL || Z_TYPE_P(uri_zv) != IS_STRING) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + uri_internal_t *internal_uri = uri_internal_from_obj(object); + internal_uri->handler = uri_handler_by_name(handler_name, strlen(handler_name)); + if (internal_uri->uri != NULL) { + internal_uri->handler->free_uri(internal_uri->uri); + } + internal_uri->uri = internal_uri->handler->parse_uri(Z_STR_P(uri_zv), NULL, NULL, true); + if (internal_uri->uri == NULL) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + /* Unserialize regular properties: second array */ + arr = zend_hash_index_find(data, 1); + if (arr == NULL || Z_TYPE_P(arr) != IS_ARRAY) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } + + /* Verify that there is no regular property in the second array, because the URI classes have no properties and they are final. */ + if (zend_hash_num_elements(Z_ARRVAL_P(arr)) > 0) { + zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(object->ce->name)); + RETURN_THROWS(); + } +} + +PHP_METHOD(Uri_WhatWg_Url, getScheme) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_SCHEME, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withScheme) +{ + uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_SCHEME); +} + +PHP_METHOD(Uri_WhatWg_Url, getUsername) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_USERNAME, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withUsername) +{ + uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_USERNAME); +} + +PHP_METHOD(Uri_WhatWg_Url, getPassword) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PASSWORD, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withPassword) +{ + uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PASSWORD); +} + +PHP_METHOD(Uri_WhatWg_Url, getAsciiHost) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, getUnicodeHost) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST, URI_COMPONENT_READ_NORMALIZED_UNICODE); +} + +PHP_METHOD(Uri_WhatWg_Url, withHost) +{ + uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_HOST); +} + +PHP_METHOD(Uri_WhatWg_Url, getPort) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PORT, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withPort) +{ + uri_write_component_long_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PORT); +} + +PHP_METHOD(Uri_WhatWg_Url, getPath) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PATH, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withPath) +{ + uri_write_component_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_PATH); +} + +PHP_METHOD(Uri_WhatWg_Url, getQuery) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_QUERY, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withQuery) +{ + uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_QUERY); +} + +PHP_METHOD(Uri_WhatWg_Url, getFragment) +{ + uri_read_component(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_FRAGMENT, URI_COMPONENT_READ_NORMALIZED_ASCII); +} + +PHP_METHOD(Uri_WhatWg_Url, withFragment) +{ + uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PROPERTY_NAME_FRAGMENT); +} + +PHP_METHOD(Uri_WhatWg_Url, equals) +{ + zend_object *that_object; + zend_object *comparison_mode = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_OBJ_OF_CLASS(that_object, uri_whatwg_url_ce) + Z_PARAM_OPTIONAL + Z_PARAM_OBJ_OF_CLASS(comparison_mode, uri_comparison_mode_ce) + ZEND_PARSE_PARAMETERS_END(); + + uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, that_object, comparison_mode); +} + +PHP_METHOD(Uri_WhatWg_Url, toUnicodeString) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zend_object *this_object = Z_OBJ_P(ZEND_THIS); + uri_internal_t *internal_uri = uri_internal_from_obj(this_object); + URI_ASSERT_INITIALIZATION(internal_uri); + + RETURN_STR(internal_uri->handler->uri_to_string(internal_uri->uri, URI_RECOMPOSITION_RAW_UNICODE, false)); +} + +PHP_METHOD(Uri_WhatWg_Url, toAsciiString) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zend_object *this_object = Z_OBJ_P(ZEND_THIS); + uri_internal_t *internal_uri = uri_internal_from_obj(this_object); + URI_ASSERT_INITIALIZATION(internal_uri); + + RETURN_STR(internal_uri->handler->uri_to_string(internal_uri->uri, URI_RECOMPOSITION_RAW_ASCII, false)); +} + +PHP_METHOD(Uri_WhatWg_Url, resolve) +{ + zend_string *uri_str; + zval *errors = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_PATH_STR(uri_str) + Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(errors) + ZEND_PARSE_PARAMETERS_END(); + + zend_object *this_object = Z_OBJ_P(ZEND_THIS); + uri_internal_t *internal_uri = uri_internal_from_obj(this_object); + URI_ASSERT_INITIALIZATION(internal_uri); + + php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, internal_uri->handler, uri_str, this_object, true, false, errors); +} + +PHP_METHOD(Uri_WhatWg_Url, __serialize) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zend_object *this_object = Z_OBJ_P(ZEND_THIS); + uri_internal_t *internal_uri = uri_internal_from_obj(this_object); + URI_ASSERT_INITIALIZATION(internal_uri); + + /* Serialize state: "uri" key in the first array */ + zend_string *uri_str = internal_uri->handler->uri_to_string(internal_uri->uri, URI_RECOMPOSITION_RAW_ASCII, false); + if (uri_str == NULL) { + zend_throw_exception_ex(NULL, 0, "Cannot recompose %s to string", ZSTR_VAL(this_object->ce->name)); + RETURN_THROWS(); + } + zval tmp; + ZVAL_STR(&tmp, uri_str); + + array_init(return_value); + + zval arr; + array_init(&arr); + zend_hash_str_add_new(Z_ARRVAL(arr), ZEND_STRL(URI_SERIALIZED_PROPERTY_NAME), &tmp); + zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); + + /* Serialize regular properties: second array */ + ZVAL_ARR(&arr, this_object->handlers->get_properties(this_object)); + Z_ADDREF(arr); + zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &arr); +} + +PHP_METHOD(Uri_WhatWg_Url, __unserialize) +{ + uri_unserialize(INTERNAL_FUNCTION_PARAM_PASSTHRU, URI_PARSER_WHATWG); +} + +PHP_METHOD(Uri_WhatWg_Url, __debugInfo) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + zend_object *object = Z_OBJ_P(ZEND_THIS); + + RETURN_ARR(uri_get_debug_properties(object)); +} + +static zend_object *uri_create_object_handler(zend_class_entry *class_type) +{ + uri_object_t *uri_object = zend_object_alloc(sizeof(uri_object_t), class_type); + + zend_object_std_init(&uri_object->std, class_type); + object_properties_init(&uri_object->std, class_type); + + return &uri_object->std; +} + +static void uri_free_obj_handler(zend_object *object) +{ + uri_object_t *uri_object = uri_object_from_obj(object); + + if (UNEXPECTED(uri_object->internal.uri != NULL)) { + uri_object->internal.handler->free_uri(uri_object->internal.uri); + uri_object->internal.handler = NULL; + uri_object->internal.uri = NULL; + } + + zend_object_std_dtor(&uri_object->std); +} + +zend_object *uri_clone_obj_handler(zend_object *object) +{ + uri_object_t *uri_object = uri_object_from_obj(object); + uri_internal_t *internal_uri = uri_internal_from_obj(object); + + URI_ASSERT_INITIALIZATION(internal_uri); + + zend_object *new_object = uri_create_object_handler(object->ce); + ZEND_ASSERT(new_object != NULL); + uri_object_t *new_uri_object = uri_object_from_obj(new_object); + + new_uri_object->internal.handler = internal_uri->handler; + + void *uri = internal_uri->handler->clone_uri(internal_uri->uri); + ZEND_ASSERT(uri != NULL); + + new_uri_object->internal.uri = uri; + + zend_objects_clone_members(&new_uri_object->std, &uri_object->std); + + return &new_uri_object->std; +} + +PHPAPI void php_uri_implementation_set_object_handlers(zend_class_entry *ce, zend_object_handlers *object_handlers) +{ + ce->create_object = uri_create_object_handler; + ce->default_object_handlers = object_handlers; + memcpy(object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); + object_handlers->offset = XtOffsetOf(uri_object_t, std); + object_handlers->free_obj = uri_free_obj_handler; + object_handlers->clone_obj = uri_clone_obj_handler; +} + +zend_result uri_handler_register(const uri_handler_t *uri_handler) +{ + zend_string *key = zend_string_init_interned(uri_handler->name, strlen(uri_handler->name), 1); + + ZEND_ASSERT(uri_handler->name != NULL); + ZEND_ASSERT(uri_handler->parse_uri != NULL); + ZEND_ASSERT(uri_handler->clone_uri != NULL); + ZEND_ASSERT(uri_handler->uri_to_string != NULL); + ZEND_ASSERT(uri_handler->free_uri != NULL); + + zend_result result = zend_hash_add_ptr(&uri_handlers, key, (void *) uri_handler) != NULL ? SUCCESS : FAILURE; + + zend_string_release_ex(key, true); + + return result; +} static PHP_MINIT_FUNCTION(uri) { + uri_whatwg_url_ce = register_class_Uri_WhatWg_Url(); + php_uri_implementation_set_object_handlers(uri_whatwg_url_ce, &uri_whatwg_uri_object_handlers); + + uri_comparison_mode_ce = register_class_Uri_UriComparisonMode(); uri_exception_ce = register_class_Uri_UriException(zend_ce_exception); - invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce); - whatwg_invalid_url_exception_ce = register_class_Uri_WhatWg_InvalidUrlException(invalid_uri_exception_ce); + uri_invalid_uri_exception_ce = register_class_Uri_InvalidUriException(uri_exception_ce); + uri_whatwg_invalid_url_exception_ce = register_class_Uri_WhatWg_InvalidUrlException(uri_invalid_uri_exception_ce); + uri_whatwg_url_validation_error_ce = register_class_Uri_WhatWg_UrlValidationError(); + uri_whatwg_url_validation_error_type_ce = register_class_Uri_WhatWg_UrlValidationErrorType(); + + zend_hash_init(&uri_handlers, 4, NULL, NULL, true); + + if (uri_handler_register(&lexbor_uri_handler) == FAILURE) { + return FAILURE; + } return SUCCESS; } @@ -60,18 +658,24 @@ static PHP_MINFO_FUNCTION(uri) static PHP_MSHUTDOWN_FUNCTION(uri) { + zend_hash_destroy(&uri_handlers); return SUCCESS; } PHP_RINIT_FUNCTION(uri) { + if (lexbor_request_init() == FAILURE) { + return FAILURE; + } return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(uri) { + lexbor_request_shutdown(); + return SUCCESS; } diff --git a/ext/uri/php_uri.h b/ext/uri/php_uri.h index 79dfced4a721a..9e22c227cbf83 100644 --- a/ext/uri/php_uri.h +++ b/ext/uri/php_uri.h @@ -17,8 +17,11 @@ #ifndef PHP_URI_H #define PHP_URI_H +#include "php_uri_common.h" + extern zend_module_entry uri_module_entry; #define phpext_uri_ptr &uri_module_entry +PHPAPI void php_uri_implementation_set_object_handlers(zend_class_entry *ce, zend_object_handlers *object_handlers); #endif diff --git a/ext/uri/php_uri.stub.php b/ext/uri/php_uri.stub.php index 926be1fbb8267..ef49e4ba6f968 100644 --- a/ext/uri/php_uri.stub.php +++ b/ext/uri/php_uri.stub.php @@ -12,6 +12,12 @@ class UriException extends \Exception class InvalidUriException extends \Uri\UriException { } + + enum UriComparisonMode + { + case IncludeFragment; + case ExcludeFragment; + } } namespace Uri\WhatWg { @@ -19,5 +25,109 @@ class InvalidUriException extends \Uri\UriException class InvalidUrlException extends \Uri\InvalidUriException { public readonly array $errors; + + public function __construct(string $message = "", array $errors = [], int $code = 0, ?\Throwable $previous = null) {} + } + + enum UrlValidationErrorType + { + case DomainToAscii; + case DomainToUnicode; + case DomainInvalidCodePoint; + case HostInvalidCodePoint; + case Ipv4EmptyPart; + case Ipv4TooManyParts; + case Ipv4NonNumericPart; + case Ipv4NonDecimalPart; + case Ipv4OutOfRangePart; + case Ipv6Unclosed; + case Ipv6InvalidCompression; + case Ipv6TooManyPieces; + case Ipv6MultipleCompression; + case Ipv6InvalidCodePoint; + case Ipv6TooFewPieces; + case Ipv4InIpv6TooManyPieces; + case Ipv4InIpv6InvalidCodePoint; + case Ipv4InIpv6OutOfRangePart; + case Ipv4InIpv6TooFewParts; + case InvalidUrlUnit; + case SpecialSchemeMissingFollowingSolidus; + case MissingSchemeNonRelativeUrl; + case InvalidReverseSoldius; + case InvalidCredentials; + case HostMissing; + case PortOutOfRange; + case PortInvalid; + case FileInvalidWindowsDriveLetter; + case FileInvalidWindowsDriveLetterHost; + } + + /** @strict-properties */ + final readonly class UrlValidationError + { + public string $context; + public \Uri\WhatWg\UrlValidationErrorType $type; + public bool $failure; + + public function __construct(string $context, \Uri\WhatWg\UrlValidationErrorType $type, bool $failure) {} + } + + /** @strict-properties */ + final readonly class Url + { + /** @param array $errors */ + public static function parse(string $uri, ?\Uri\WhatWg\Url $baseUrl = null, &$errors = null): ?static {} + + /** @param array $softErrors */ + public function __construct(string $uri, ?\Uri\WhatWg\Url $baseUrl = null, &$softErrors = null) {} + + public function getScheme(): string {} + + public function withScheme(string $scheme): static {} + + public function getUsername(): ?string {} + + public function withUsername(?string $username): static {} + + public function getPassword(): ?string {} + + public function withPassword(#[\SensitiveParameter] ?string $password): static {} + + public function getAsciiHost(): ?string {} + + public function getUnicodeHost(): ?string {} + + public function withHost(?string $host): static {} + + public function getPort(): ?int {} + + public function withPort(?int $port): static {} + + public function getPath(): string {} + + public function withPath(string $path): static {} + + public function getQuery(): ?string {} + + public function withQuery(?string $query): static {} + + public function getFragment(): ?string {} + + public function withFragment(?string $fragment): static {} + + public function equals(\Uri\WhatWg\Url $url, \Uri\UriComparisonMode $comparisonMode = \Uri\UriComparisonMode::ExcludeFragment): bool {} + + public function toAsciiString(): string {} + + public function toUnicodeString(): string {} + + /** @param array $softErrors */ + public function resolve(string $uri, &$softErrors = null): static {} + + public function __serialize(): array {} + + public function __unserialize(array $data): void {} + + public function __debugInfo(): array {} } } diff --git a/ext/uri/php_uri_arginfo.h b/ext/uri/php_uri_arginfo.h index 124a6b3719849..0ae755a9f70dc 100644 --- a/ext/uri/php_uri_arginfo.h +++ b/ext/uri/php_uri_arginfo.h @@ -1,5 +1,175 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 35460b24cf237585dabdc9813212c343034cf591 */ + * Stub hash: 1945c28deef13c2af552b18c2a5a6c7798d4aeec */ + +ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Uri_WhatWg_InvalidUrlException___construct, 0, 0, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, errors, IS_ARRAY, 0, "[]") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0") + ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null") +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Uri_WhatWg_UrlValidationError___construct, 0, 0, 3) + ZEND_ARG_TYPE_INFO(0, context, IS_STRING, 0) + ZEND_ARG_OBJ_INFO(0, type, Uri\\WhatWg\\\125rlValidationErrorType, 0) + ZEND_ARG_TYPE_INFO(0, failure, _IS_BOOL, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_parse, 0, 1, IS_STATIC, 1) + ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0) + ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, baseUrl, Uri\\WhatWg\\\125rl, 1, "null") + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, errors, "null") +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Uri_WhatWg_Url___construct, 0, 0, 1) + ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0) + ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, baseUrl, Uri\\WhatWg\\\125rl, 1, "null") + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, softErrors, "null") +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_getScheme, 0, 0, IS_STRING, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withScheme, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, scheme, IS_STRING, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_getUsername, 0, 0, IS_STRING, 1) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withUsername, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, username, IS_STRING, 1) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_getPassword arginfo_class_Uri_WhatWg_Url_getUsername + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withPassword, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 1) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_getAsciiHost arginfo_class_Uri_WhatWg_Url_getUsername + +#define arginfo_class_Uri_WhatWg_Url_getUnicodeHost arginfo_class_Uri_WhatWg_Url_getUsername + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withHost, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 1) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_getPort, 0, 0, IS_LONG, 1) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withPort, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, port, IS_LONG, 1) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_getPath arginfo_class_Uri_WhatWg_Url_getScheme + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withPath, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_getQuery arginfo_class_Uri_WhatWg_Url_getUsername + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withQuery, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, query, IS_STRING, 1) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_getFragment arginfo_class_Uri_WhatWg_Url_getUsername + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_withFragment, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, fragment, IS_STRING, 1) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_equals, 0, 1, _IS_BOOL, 0) + ZEND_ARG_OBJ_INFO(0, url, Uri\\WhatWg\\\125rl, 0) + ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, comparisonMode, Uri\\\125riComparisonMode, 0, "Uri\\UriComparisonMode::ExcludeFragment") +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url_toAsciiString arginfo_class_Uri_WhatWg_Url_getScheme + +#define arginfo_class_Uri_WhatWg_Url_toUnicodeString arginfo_class_Uri_WhatWg_Url_getScheme + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url_resolve, 0, 1, IS_STATIC, 0) + ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0) + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, softErrors, "null") +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url___serialize, 0, 0, IS_ARRAY, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Uri_WhatWg_Url___unserialize, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0) +ZEND_END_ARG_INFO() + +#define arginfo_class_Uri_WhatWg_Url___debugInfo arginfo_class_Uri_WhatWg_Url___serialize + +ZEND_METHOD(Uri_WhatWg_InvalidUrlException, __construct); +ZEND_METHOD(Uri_WhatWg_UrlValidationError, __construct); +ZEND_METHOD(Uri_WhatWg_Url, parse); +ZEND_METHOD(Uri_WhatWg_Url, __construct); +ZEND_METHOD(Uri_WhatWg_Url, getScheme); +ZEND_METHOD(Uri_WhatWg_Url, withScheme); +ZEND_METHOD(Uri_WhatWg_Url, getUsername); +ZEND_METHOD(Uri_WhatWg_Url, withUsername); +ZEND_METHOD(Uri_WhatWg_Url, getPassword); +ZEND_METHOD(Uri_WhatWg_Url, withPassword); +ZEND_METHOD(Uri_WhatWg_Url, getAsciiHost); +ZEND_METHOD(Uri_WhatWg_Url, getUnicodeHost); +ZEND_METHOD(Uri_WhatWg_Url, withHost); +ZEND_METHOD(Uri_WhatWg_Url, getPort); +ZEND_METHOD(Uri_WhatWg_Url, withPort); +ZEND_METHOD(Uri_WhatWg_Url, getPath); +ZEND_METHOD(Uri_WhatWg_Url, withPath); +ZEND_METHOD(Uri_WhatWg_Url, getQuery); +ZEND_METHOD(Uri_WhatWg_Url, withQuery); +ZEND_METHOD(Uri_WhatWg_Url, getFragment); +ZEND_METHOD(Uri_WhatWg_Url, withFragment); +ZEND_METHOD(Uri_WhatWg_Url, equals); +ZEND_METHOD(Uri_WhatWg_Url, toAsciiString); +ZEND_METHOD(Uri_WhatWg_Url, toUnicodeString); +ZEND_METHOD(Uri_WhatWg_Url, resolve); +ZEND_METHOD(Uri_WhatWg_Url, __serialize); +ZEND_METHOD(Uri_WhatWg_Url, __unserialize); +ZEND_METHOD(Uri_WhatWg_Url, __debugInfo); + +static const zend_function_entry class_Uri_WhatWg_InvalidUrlException_methods[] = { + ZEND_ME(Uri_WhatWg_InvalidUrlException, __construct, arginfo_class_Uri_WhatWg_InvalidUrlException___construct, ZEND_ACC_PUBLIC) + ZEND_FE_END +}; + +static const zend_function_entry class_Uri_WhatWg_UrlValidationError_methods[] = { + ZEND_ME(Uri_WhatWg_UrlValidationError, __construct, arginfo_class_Uri_WhatWg_UrlValidationError___construct, ZEND_ACC_PUBLIC) + ZEND_FE_END +}; + +static const zend_function_entry class_Uri_WhatWg_Url_methods[] = { + ZEND_ME(Uri_WhatWg_Url, parse, arginfo_class_Uri_WhatWg_Url_parse, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + ZEND_ME(Uri_WhatWg_Url, __construct, arginfo_class_Uri_WhatWg_Url___construct, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getScheme, arginfo_class_Uri_WhatWg_Url_getScheme, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withScheme, arginfo_class_Uri_WhatWg_Url_withScheme, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getUsername, arginfo_class_Uri_WhatWg_Url_getUsername, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withUsername, arginfo_class_Uri_WhatWg_Url_withUsername, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getPassword, arginfo_class_Uri_WhatWg_Url_getPassword, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withPassword, arginfo_class_Uri_WhatWg_Url_withPassword, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getAsciiHost, arginfo_class_Uri_WhatWg_Url_getAsciiHost, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getUnicodeHost, arginfo_class_Uri_WhatWg_Url_getUnicodeHost, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withHost, arginfo_class_Uri_WhatWg_Url_withHost, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getPort, arginfo_class_Uri_WhatWg_Url_getPort, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withPort, arginfo_class_Uri_WhatWg_Url_withPort, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getPath, arginfo_class_Uri_WhatWg_Url_getPath, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withPath, arginfo_class_Uri_WhatWg_Url_withPath, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getQuery, arginfo_class_Uri_WhatWg_Url_getQuery, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withQuery, arginfo_class_Uri_WhatWg_Url_withQuery, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, getFragment, arginfo_class_Uri_WhatWg_Url_getFragment, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, withFragment, arginfo_class_Uri_WhatWg_Url_withFragment, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, equals, arginfo_class_Uri_WhatWg_Url_equals, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, toAsciiString, arginfo_class_Uri_WhatWg_Url_toAsciiString, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, toUnicodeString, arginfo_class_Uri_WhatWg_Url_toUnicodeString, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, resolve, arginfo_class_Uri_WhatWg_Url_resolve, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, __serialize, arginfo_class_Uri_WhatWg_Url___serialize, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, __unserialize, arginfo_class_Uri_WhatWg_Url___unserialize, ZEND_ACC_PUBLIC) + ZEND_ME(Uri_WhatWg_Url, __debugInfo, arginfo_class_Uri_WhatWg_Url___debugInfo, ZEND_ACC_PUBLIC) + ZEND_FE_END +}; static zend_class_entry *register_class_Uri_UriException(zend_class_entry *class_entry_Exception) { @@ -21,11 +191,22 @@ static zend_class_entry *register_class_Uri_InvalidUriException(zend_class_entry return class_entry; } +static zend_class_entry *register_class_Uri_UriComparisonMode(void) +{ + zend_class_entry *class_entry = zend_register_internal_enum("Uri\\UriComparisonMode", IS_UNDEF, NULL); + + zend_enum_add_case_cstr(class_entry, "IncludeFragment", NULL); + + zend_enum_add_case_cstr(class_entry, "ExcludeFragment", NULL); + + return class_entry; +} + static zend_class_entry *register_class_Uri_WhatWg_InvalidUrlException(zend_class_entry *class_entry_Uri_InvalidUriException) { zend_class_entry ce, *class_entry; - INIT_NS_CLASS_ENTRY(ce, "Uri\\WhatWg", "InvalidUrlException", NULL); + INIT_NS_CLASS_ENTRY(ce, "Uri\\WhatWg", "InvalidUrlException", class_Uri_WhatWg_InvalidUrlException_methods); class_entry = zend_register_internal_class_with_flags(&ce, class_entry_Uri_InvalidUriException, ZEND_ACC_NO_DYNAMIC_PROPERTIES); zval property_errors_default_value; @@ -36,3 +217,108 @@ static zend_class_entry *register_class_Uri_WhatWg_InvalidUrlException(zend_clas return class_entry; } + +static zend_class_entry *register_class_Uri_WhatWg_UrlValidationErrorType(void) +{ + zend_class_entry *class_entry = zend_register_internal_enum("Uri\\WhatWg\\UrlValidationErrorType", IS_UNDEF, NULL); + + zend_enum_add_case_cstr(class_entry, "DomainToAscii", NULL); + + zend_enum_add_case_cstr(class_entry, "DomainToUnicode", NULL); + + zend_enum_add_case_cstr(class_entry, "DomainInvalidCodePoint", NULL); + + zend_enum_add_case_cstr(class_entry, "HostInvalidCodePoint", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4EmptyPart", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4TooManyParts", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4NonNumericPart", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4NonDecimalPart", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4OutOfRangePart", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6Unclosed", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6InvalidCompression", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6TooManyPieces", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6MultipleCompression", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6InvalidCodePoint", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv6TooFewPieces", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4InIpv6TooManyPieces", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4InIpv6InvalidCodePoint", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4InIpv6OutOfRangePart", NULL); + + zend_enum_add_case_cstr(class_entry, "Ipv4InIpv6TooFewParts", NULL); + + zend_enum_add_case_cstr(class_entry, "InvalidUrlUnit", NULL); + + zend_enum_add_case_cstr(class_entry, "SpecialSchemeMissingFollowingSolidus", NULL); + + zend_enum_add_case_cstr(class_entry, "MissingSchemeNonRelativeUrl", NULL); + + zend_enum_add_case_cstr(class_entry, "InvalidReverseSoldius", NULL); + + zend_enum_add_case_cstr(class_entry, "InvalidCredentials", NULL); + + zend_enum_add_case_cstr(class_entry, "HostMissing", NULL); + + zend_enum_add_case_cstr(class_entry, "PortOutOfRange", NULL); + + zend_enum_add_case_cstr(class_entry, "PortInvalid", NULL); + + zend_enum_add_case_cstr(class_entry, "FileInvalidWindowsDriveLetter", NULL); + + zend_enum_add_case_cstr(class_entry, "FileInvalidWindowsDriveLetterHost", NULL); + + return class_entry; +} + +static zend_class_entry *register_class_Uri_WhatWg_UrlValidationError(void) +{ + zend_class_entry ce, *class_entry; + + INIT_NS_CLASS_ENTRY(ce, "Uri\\WhatWg", "UrlValidationError", class_Uri_WhatWg_UrlValidationError_methods); + class_entry = zend_register_internal_class_with_flags(&ce, NULL, ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES|ZEND_ACC_READONLY_CLASS); + + zval property_context_default_value; + ZVAL_UNDEF(&property_context_default_value); + zend_string *property_context_name = zend_string_init("context", sizeof("context") - 1, 1); + zend_declare_typed_property(class_entry, property_context_name, &property_context_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); + zend_string_release(property_context_name); + + zval property_type_default_value; + ZVAL_UNDEF(&property_type_default_value); + zend_string *property_type_class_Uri_WhatWg_UrlValidationErrorType = zend_string_init("Uri\\WhatWg\\\125rlValidationErrorType", sizeof("Uri\\WhatWg\\\125rlValidationErrorType")-1, 1); + zend_declare_typed_property(class_entry, ZSTR_KNOWN(ZEND_STR_TYPE), &property_type_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_CLASS(property_type_class_Uri_WhatWg_UrlValidationErrorType, 0, 0)); + + zval property_failure_default_value; + ZVAL_UNDEF(&property_failure_default_value); + zend_string *property_failure_name = zend_string_init("failure", sizeof("failure") - 1, 1); + zend_declare_typed_property(class_entry, property_failure_name, &property_failure_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_BOOL)); + zend_string_release(property_failure_name); + + return class_entry; +} + +static zend_class_entry *register_class_Uri_WhatWg_Url(void) +{ + zend_class_entry ce, *class_entry; + + INIT_NS_CLASS_ENTRY(ce, "Uri\\WhatWg", "Url", class_Uri_WhatWg_Url_methods); + class_entry = zend_register_internal_class_with_flags(&ce, NULL, ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES|ZEND_ACC_READONLY_CLASS); + + + zend_add_parameter_attribute(zend_hash_str_find_ptr(&class_entry->function_table, "withpassword", sizeof("withpassword") - 1), 0, ZSTR_KNOWN(ZEND_STR_SENSITIVEPARAMETER), 0); + + return class_entry; +} diff --git a/ext/uri/php_uri_common.c b/ext/uri/php_uri_common.c new file mode 100644 index 0000000000000..9128b942e57b8 --- /dev/null +++ b/ext/uri/php_uri_common.c @@ -0,0 +1,169 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Máté Kocsis | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "Zend/zend_interfaces.h" +#include "Zend/zend_exceptions.h" +#include "php_uri_common.h" + +const uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_internal_t *internal_uri, uri_property_name_t property_name) +{ + switch (property_name) { + case URI_PROPERTY_NAME_SCHEME: + return &internal_uri->handler->property_handlers.scheme; + case URI_PROPERTY_NAME_USERNAME: + return &internal_uri->handler->property_handlers.username; + case URI_PROPERTY_NAME_PASSWORD: + return &internal_uri->handler->property_handlers.password; + case URI_PROPERTY_NAME_HOST: + return &internal_uri->handler->property_handlers.host; + case URI_PROPERTY_NAME_PORT: + return &internal_uri->handler->property_handlers.port; + case URI_PROPERTY_NAME_PATH: + return &internal_uri->handler->property_handlers.path; + case URI_PROPERTY_NAME_QUERY: + return &internal_uri->handler->property_handlers.query; + case URI_PROPERTY_NAME_FRAGMENT: + return &internal_uri->handler->property_handlers.fragment; + EMPTY_SWITCH_DEFAULT_CASE() + } +} + +static zend_string *get_known_string_by_property_name(uri_property_name_t property_name) +{ + switch (property_name) { + case URI_PROPERTY_NAME_SCHEME: + return ZSTR_KNOWN(ZEND_STR_SCHEME); + case URI_PROPERTY_NAME_USERNAME: + return ZSTR_KNOWN(ZEND_STR_USERNAME); + case URI_PROPERTY_NAME_PASSWORD: + return ZSTR_KNOWN(ZEND_STR_PASSWORD); + case URI_PROPERTY_NAME_HOST: + return ZSTR_KNOWN(ZEND_STR_HOST); + case URI_PROPERTY_NAME_PORT: + return ZSTR_KNOWN(ZEND_STR_PORT); + case URI_PROPERTY_NAME_PATH: + return ZSTR_KNOWN(ZEND_STR_PATH); + case URI_PROPERTY_NAME_QUERY: + return ZSTR_KNOWN(ZEND_STR_QUERY); + case URI_PROPERTY_NAME_FRAGMENT: + return ZSTR_KNOWN(ZEND_STR_FRAGMENT); + EMPTY_SWITCH_DEFAULT_CASE() + } +} + +void uri_read_component(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name, uri_component_read_mode_t component_read_mode) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); + URI_ASSERT_INITIALIZATION(internal_uri); + + const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); + ZEND_ASSERT(property_handler != NULL); + + if (UNEXPECTED(property_handler->read_func(internal_uri, component_read_mode, return_value) == FAILURE)) { + zend_throw_error(NULL, "%s::$%s property cannot be retrieved", ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), + ZSTR_VAL(get_known_string_by_property_name(property_name))); + RETURN_THROWS(); + } +} + +static void uri_write_component_ex(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name, zval *property_zv) +{ + uri_internal_t *internal_uri = Z_URI_INTERNAL_P(ZEND_THIS); + URI_ASSERT_INITIALIZATION(internal_uri); + + const uri_property_handler_t *property_handler = uri_property_handler_from_internal_uri(internal_uri, property_name); + ZEND_ASSERT(property_handler != NULL); + + zend_object *new_object = uri_clone_obj_handler(Z_OBJ_P(ZEND_THIS)); + if (UNEXPECTED(EG(exception) != NULL)) { + zend_object_release(new_object); + RETURN_THROWS(); + } + + uri_internal_t *new_internal_uri = uri_internal_from_obj(new_object); + URI_ASSERT_INITIALIZATION(new_internal_uri); + if (property_handler->write_func == NULL) { + zend_readonly_property_modification_error_ex(ZSTR_VAL(Z_OBJ_P(ZEND_THIS)->ce->name), + ZSTR_VAL(get_known_string_by_property_name(property_name))); + zend_object_release(new_object); + RETURN_THROWS(); + } + + zval errors; + ZVAL_UNDEF(&errors); + if (property_handler->write_func(new_internal_uri, property_zv, &errors) == FAILURE) { + zval_ptr_dtor(&errors); + zend_object_release(new_object); + RETURN_THROWS(); + } + + ZEND_ASSERT(Z_ISUNDEF(errors)); + RETVAL_OBJ(new_object); +} + +void uri_write_component_str(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name) +{ + zend_string *value; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_PATH_STR(value) + ZEND_PARSE_PARAMETERS_END(); + + zval zv; + ZVAL_STR(&zv, value); + + uri_write_component_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, property_name, &zv); +} + +void uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name) +{ + zend_string *value; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_PATH_STR_OR_NULL(value) + ZEND_PARSE_PARAMETERS_END(); + + zval zv; + if (value == NULL) { + ZVAL_NULL(&zv); + } else { + ZVAL_STR(&zv, value); + } + + uri_write_component_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, property_name, &zv); +} + +void uri_write_component_long_or_null(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name) +{ + zend_long value; + bool value_is_null; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_LONG_OR_NULL(value, value_is_null) + ZEND_PARSE_PARAMETERS_END(); + + zval zv; + if (value_is_null) { + ZVAL_NULL(&zv); + } else { + ZVAL_LONG(&zv, value); + } + + uri_write_component_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, property_name, &zv); +} diff --git a/ext/uri/php_uri_common.h b/ext/uri/php_uri_common.h new file mode 100644 index 0000000000000..1aee1cd512472 --- /dev/null +++ b/ext/uri/php_uri_common.h @@ -0,0 +1,138 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Máté Kocsis | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_URI_COMMON_H +#define PHP_URI_COMMON_H + +extern zend_class_entry *uri_whatwg_url_ce; +extern zend_object_handlers uri_whatwg_uri_object_handlers; +extern zend_class_entry *uri_comparison_mode_ce; +extern zend_class_entry *uri_exception_ce; +extern zend_class_entry *uri_invalid_uri_exception_ce; +extern zend_class_entry *uri_whatwg_invalid_url_exception_ce; +extern zend_class_entry *uri_whatwg_url_validation_error_type_ce; +extern zend_class_entry *uri_whatwg_url_validation_error_ce; +extern zend_object *uri_clone_obj_handler(zend_object *object); + +typedef enum { + URI_RECOMPOSITION_RAW_ASCII, + URI_RECOMPOSITION_RAW_UNICODE, + URI_RECOMPOSITION_NORMALIZED_ASCII, + URI_RECOMPOSITION_NORMALIZED_UNICODE, +} uri_recomposition_mode_t; + +typedef enum { + URI_COMPONENT_READ_RAW, + URI_COMPONENT_READ_NORMALIZED_ASCII, + URI_COMPONENT_READ_NORMALIZED_UNICODE, +} uri_component_read_mode_t; + +struct uri_internal_t; + +typedef zend_result (*uri_read_t)(const struct uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval); + +typedef zend_result (*uri_write_t)(struct uri_internal_t *internal_uri, zval *value, zval *errors); + +typedef enum { + URI_PROPERTY_NAME_SCHEME, + URI_PROPERTY_NAME_USERNAME, + URI_PROPERTY_NAME_PASSWORD, + URI_PROPERTY_NAME_HOST, + URI_PROPERTY_NAME_PORT, + URI_PROPERTY_NAME_PATH, + URI_PROPERTY_NAME_QUERY, + URI_PROPERTY_NAME_FRAGMENT, +} uri_property_name_t; + +typedef struct uri_property_handler_t { + uri_read_t read_func; + uri_write_t write_func; +} uri_property_handler_t; + +typedef struct uri_property_handlers_t { + uri_property_handler_t scheme; + uri_property_handler_t username; + uri_property_handler_t password; + uri_property_handler_t host; + uri_property_handler_t port; + uri_property_handler_t path; + uri_property_handler_t query; + uri_property_handler_t fragment; +} uri_property_handlers_t; + +typedef struct uri_handler_t { + const char *name; + + /** + * Parse a URI string into a URI. + * + * If the URI string is valid, a URI is returned. In case of failure, NULL is + * returned. + * + * The errors by-ref parameter can contain errors that occurred during parsing. + * If the input value is NULL, or there were no errors, the errors parameter should + * not be modified. + * + * If the URI string is valid and the base_url URI is not NULL, the URI object + * is resolved against the base_url. + * + * If the silent parameter is true, a Uri\InvalidUriException instance must be thrown. + * If the parameter is false, the possible errors should be handled by the caller. + */ + void *(*parse_uri)(const zend_string *uri_str, const void *base_url, zval *errors, bool silent); + void *(*clone_uri)(void *uri); + zend_string *(*uri_to_string)(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment); + void (*free_uri)(void *uri); + + const uri_property_handlers_t property_handlers; +} uri_handler_t; + +typedef struct uri_internal_t { + const uri_handler_t *handler; + void *uri; +} uri_internal_t; + +typedef struct uri_object_t { + uri_internal_t internal; + zend_object std; +} uri_object_t; + +static inline uri_object_t *uri_object_from_obj(const zend_object *object) { + return (uri_object_t*)((char*)(object) - XtOffsetOf(uri_object_t, std)); +} + +static inline uri_internal_t *uri_internal_from_obj(const zend_object *object) { + return &(uri_object_from_obj(object)->internal); +} + +#define Z_URI_OBJECT_P(zv) uri_object_from_obj(Z_OBJ_P((zv))) +#define Z_URI_INTERNAL_P(zv) uri_internal_from_obj(Z_OBJ_P((zv))) + +#define URI_PARSER_WHATWG "Uri\\WhatWg\\Url" +#define URI_SERIALIZED_PROPERTY_NAME "uri" + +zend_result uri_handler_register(const uri_handler_t *uri_handler); +const uri_property_handler_t *uri_property_handler_from_internal_uri(const uri_internal_t *internal_uri, uri_property_name_t property_name); +void uri_read_component(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name, uri_component_read_mode_t component_read_mode); +void uri_write_component_str(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name); +void uri_write_component_str_or_null(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name); +void uri_write_component_long_or_null(INTERNAL_FUNCTION_PARAMETERS, uri_property_name_t property_name); + +#define URI_ASSERT_INITIALIZATION(internal_uri) do { \ + ZEND_ASSERT(internal_uri != NULL && internal_uri->uri != NULL); \ +} while (0) + +#endif diff --git a/ext/uri/tests/003.phpt b/ext/uri/tests/003.phpt new file mode 100644 index 0000000000000..bcd6e417441c2 --- /dev/null +++ b/ext/uri/tests/003.phpt @@ -0,0 +1,32 @@ +--TEST-- +Parse URL exotic URLs +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + string(8) "username" + ["password"]=> + string(8) "password" + ["host"]=> + string(18) "xn--hostname-b1aaa" + ["port"]=> + int(9090) + ["path"]=> + string(5) "/path" + ["query"]=> + string(14) "arg=va%C3%A9ue" + ["fragment"]=> + string(6) "anchor" +} +NULL diff --git a/ext/uri/tests/004.phpt b/ext/uri/tests/004.phpt new file mode 100644 index 0000000000000..04127a7ded0d3 --- /dev/null +++ b/ext/uri/tests/004.phpt @@ -0,0 +1,25 @@ +--TEST-- +Parse invalid URLs +--EXTENSIONS-- +uri +--FILE-- +getMessage() . "\n"; +} + +var_dump(Uri\WhatWg\Url::parse("")); + +var_dump(Uri\WhatWg\Url::parse("192.168/contact.html", null)); + +var_dump(Uri\WhatWg\Url::parse("http://RuPaul's Drag Race All Stars 7 Winners Cast on This Season's", null)); + +?> +--EXPECTF-- +URL parsing failed +NULL +NULL +NULL diff --git a/ext/uri/tests/005.phpt b/ext/uri/tests/005.phpt new file mode 100644 index 0000000000000..262d43a75406b --- /dev/null +++ b/ext/uri/tests/005.phpt @@ -0,0 +1,38 @@ +--TEST-- +Parse multibyte URLs +--EXTENSIONS-- +uri +--FILE-- +getAsciiHost()); +var_dump($url->getUnicodeHost()); +var_dump($url->toAsciiString()); +var_dump($url->toUnicodeString()); + +?> +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + string(8) "username" + ["password"]=> + string(8) "password" + ["host"]=> + string(18) "xn--hostname-b1aaa" + ["port"]=> + int(9090) + ["path"]=> + string(5) "/path" + ["query"]=> + string(14) "arg=va%C3%A9ue" + ["fragment"]=> + string(6) "anchor" +} +string(18) "xn--hostname-b1aaa" +string(14) "héééostname" +string(75) "http://username:password@xn--hostname-b1aaa:9090/path?arg=va%C3%A9ue#anchor" +string(71) "http://username:password@héééostname:9090/path?arg=va%C3%A9ue#anchor" diff --git a/ext/uri/tests/006.phpt b/ext/uri/tests/006.phpt new file mode 100644 index 0000000000000..0aba3e9e46b5e --- /dev/null +++ b/ext/uri/tests/006.phpt @@ -0,0 +1,30 @@ +--TEST-- +Test successful manual Uri child instance creation +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + string(8) "username" + ["password"]=> + string(8) "password" + ["host"]=> + string(11) "example.com" + ["port"]=> + int(8080) + ["path"]=> + string(5) "/path" + ["query"]=> + string(3) "q=r" + ["fragment"]=> + string(8) "fragment" +} diff --git a/ext/uri/tests/007.phpt b/ext/uri/tests/007.phpt new file mode 100644 index 0000000000000..e60e69fc113a3 --- /dev/null +++ b/ext/uri/tests/007.phpt @@ -0,0 +1,63 @@ +--TEST-- +Test URI creation errors +--EXTENSIONS-- +uri +--FILE-- +getMessage() . "\n"; + var_dump($e->errors); +} + +$failures = []; +$url = new Uri\WhatWg\Url(" https://example.org ", null, $failures); +var_dump($url->toAsciiString()); +var_dump($failures); + +?> +--EXPECTF-- +URL parsing failed +array(%d) { + [0]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(26) "password/path?q=r#fragment" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::PortInvalid) + ["failure"]=> + bool(true) + } + [1]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(36) "@username:password/path?q=r#fragment" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::InvalidCredentials) + ["failure"]=> + bool(false) + } +} +string(20) "https://example.org/" +array(2) { + [0]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(1) " " + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit) + ["failure"]=> + bool(false) + } + [1]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(21) " https://example.org " + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit) + ["failure"]=> + bool(false) + } +} diff --git a/ext/uri/tests/008.phpt b/ext/uri/tests/008.phpt new file mode 100644 index 0000000000000..f4fddcd8eb777 --- /dev/null +++ b/ext/uri/tests/008.phpt @@ -0,0 +1,34 @@ +--TEST-- +Test Uri getters +--EXTENSIONS-- +uri +--FILE-- +getScheme()); + var_dump($url->getUsername()); + var_dump($url->getPassword()); + var_dump($url->getAsciiHost()); + var_dump($url->getUnicodeHost()); + var_dump($url->getPort()); + var_dump($url->getPath()); + var_dump($url->getQuery()); + var_dump($url->getFragment()); +} + +$url = Uri\WhatWg\Url::parse("https://username:password@www.google.com:8080/pathname1/pathname2/pathname3?query=true#hash-exists"); +callWhatWgGetters($url); + +?> +--EXPECT-- +string(5) "https" +string(8) "username" +string(8) "password" +string(14) "www.google.com" +string(14) "www.google.com" +int(8080) +string(30) "/pathname1/pathname2/pathname3" +string(10) "query=true" +string(11) "hash-exists" diff --git a/ext/uri/tests/009.phpt b/ext/uri/tests/009.phpt new file mode 100644 index 0000000000000..1b279588c0167 --- /dev/null +++ b/ext/uri/tests/009.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test parsing with IANA schemes +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(16) "chrome-extension" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(0) "" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/010.phpt b/ext/uri/tests/010.phpt new file mode 100644 index 0000000000000..4ec13f652f60c --- /dev/null +++ b/ext/uri/tests/010.phpt @@ -0,0 +1,48 @@ +--TEST-- +Test parsing URIs when a base URI is present +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(14) "/path/to/file1" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(8) "test.com" + ["port"]=> + NULL + ["path"]=> + string(14) "/path/to/file1" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/011.phpt b/ext/uri/tests/011.phpt new file mode 100644 index 0000000000000..283886fb34fbb --- /dev/null +++ b/ext/uri/tests/011.phpt @@ -0,0 +1,22 @@ +--TEST-- +Test encoding and normalization +--EXTENSIONS-- +uri +--FILE-- +toAsciiString()); +var_dump(Uri\WhatWg\Url::parse("https://www.example.com:443/dir1/../dir2")->toAsciiString()); +var_dump(Uri\WhatWg\Url::parse("https://你好你好")->toAsciiString()); +var_dump(Uri\WhatWg\Url::parse("https://你好你好")->toUnicodeString()); +var_dump(Uri\WhatWg\Url::parse("https://0Xc0.0250.01")->toAsciiString()); +var_dump(Uri\WhatWg\Url::parse("HttPs://0300.0250.0000.0001/path?query=foo%20bar")->toAsciiString()); + +?> +--EXPECT-- +string(23) "http://www.example.com/" +string(28) "https://www.example.com/dir2" +string(23) "https://xn--6qqa088eba/" +string(21) "https://你好你好/" +string(20) "https://192.168.0.1/" +string(40) "https://192.168.0.1/path?query=foo%20bar" diff --git a/ext/uri/tests/012.phpt b/ext/uri/tests/012.phpt new file mode 100644 index 0000000000000..0784a74e625f0 --- /dev/null +++ b/ext/uri/tests/012.phpt @@ -0,0 +1,49 @@ +--TEST-- +Test parsing of various schemes +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(6) "mailto" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + NULL + ["port"]=> + NULL + ["path"]=> + string(15) "Joe@Example.COM" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "file" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + NULL + ["port"]=> + NULL + ["path"]=> + string(30) "/E:/Documents%20and%20Settings" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/013.phpt b/ext/uri/tests/013.phpt new file mode 100644 index 0000000000000..016fe6632782c --- /dev/null +++ b/ext/uri/tests/013.phpt @@ -0,0 +1,87 @@ +--TEST-- +Test parsing of query strings +--EXTENSIONS-- +uri +--FILE-- + + @")); + +?> +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + string(25) "foo=Hell%C3%B3+W%C3%B6rld" + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + string(27) "foo=Hell%C3%B3%20W%C3%B6rld" + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + string(30) "foobar=%27%3Cscript%3E+%2B+%40" + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(4) "http" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + string(30) "foobar=%27%3Cscript%3E%20+%20@" + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/014.phpt b/ext/uri/tests/014.phpt new file mode 100644 index 0000000000000..224b0c9b64fb4 --- /dev/null +++ b/ext/uri/tests/014.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test recomposition of URIs +--EXTENSIONS-- +uri +--FILE-- +toAsciiString()); + +?> +--EXPECT-- +string(45) "http://example.com/?foo=Hell%C3%B3+W%C3%B6rld" diff --git a/ext/uri/tests/015.phpt b/ext/uri/tests/015.phpt new file mode 100644 index 0000000000000..4df353e942186 --- /dev/null +++ b/ext/uri/tests/015.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test instantiation without calling constructor +--EXTENSIONS-- +reflection +uri +--FILE-- +newInstanceWithoutConstructor(); +} catch (ReflectionException $e) { + echo $e->getMessage() . "\n"; +} + +?> +--EXPECT-- +Class Uri\WhatWg\Url is an internal class marked as final that cannot be instantiated without invoking its constructor diff --git a/ext/uri/tests/018.phpt b/ext/uri/tests/018.phpt new file mode 100644 index 0000000000000..bf8caffb5e7ec --- /dev/null +++ b/ext/uri/tests/018.phpt @@ -0,0 +1,50 @@ +--TEST-- +Test property mutation +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#1 (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#2 (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/019.phpt b/ext/uri/tests/019.phpt new file mode 100644 index 0000000000000..df19fb14196ac --- /dev/null +++ b/ext/uri/tests/019.phpt @@ -0,0 +1,55 @@ +--TEST-- +Test IDNA support +--EXTENSIONS-- +uri +--FILE-- +getAsciiHost()); +var_dump($url->getUnicodeHost()); +var_dump($url->toAsciiString()); +var_dump($url->toUnicodeString()); + +?> +--EXPECTF-- +NULL +array(1) { + [0]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(4) "🐘" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::MissingSchemeNonRelativeUrl) + ["failure"]=> + bool(true) + } +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(12) "xn--go8h.com" + ["port"]=> + NULL + ["path"]=> + string(13) "/%F0%9F%90%98" + ["query"]=> + string(25) "%F0%9F%90%98=%F0%9F%90%98" + ["fragment"]=> + NULL +} +string(12) "xn--go8h.com" +string(8) "🐘.com" +string(59) "https://xn--go8h.com/%F0%9F%90%98?%F0%9F%90%98=%F0%9F%90%98" +string(55) "https://🐘.com/%F0%9F%90%98?%F0%9F%90%98=%F0%9F%90%98" diff --git a/ext/uri/tests/022.phpt b/ext/uri/tests/022.phpt new file mode 100644 index 0000000000000..1e920c5055f81 --- /dev/null +++ b/ext/uri/tests/022.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test extension of Uri\WhatWg\Url +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +Fatal error: Class MyWhatWgUri cannot extend final class Uri\WhatWg\Url in %s on line %d diff --git a/ext/uri/tests/023.phpt b/ext/uri/tests/023.phpt new file mode 100644 index 0000000000000..b48e2df838eef --- /dev/null +++ b/ext/uri/tests/023.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test property mutation - scheme +--EXTENSIONS-- +uri +--FILE-- +withScheme("http"); + +var_dump($url1->getScheme()); +var_dump($url2->getScheme()); + +try { + $url2->withScheme(""); +} catch (Uri\WhatWg\InvalidUrlException $e) { + echo $e->getMessage() . "\n"; +} + +try { + $url2->withScheme("http%73"); +} catch (Uri\WhatWg\InvalidUrlException $e) { + echo $e->getMessage() . "\n"; +} + +?> +--EXPECT-- +string(5) "https" +string(4) "http" +URL parsing failed +URL parsing failed diff --git a/ext/uri/tests/024.phpt b/ext/uri/tests/024.phpt new file mode 100644 index 0000000000000..907be0091d7c7 --- /dev/null +++ b/ext/uri/tests/024.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test property mutation - username +--EXTENSIONS-- +uri +--FILE-- +withUsername("user"); +$url3 = $url2->withUsername(null); +$url4 = $url3->withUsername("%75s%2Fr"); // us/r +$url5 = $url4->withUsername("u:s/r"); + +$url6 = Uri\WhatWg\Url::parse("file:///foo/bar/"); +$url6 = $url6->withUsername("user"); + +var_dump($url2->getUsername()); +var_dump($url3->getUsername()); +var_dump($url4->getUsername()); +var_dump($url5->getUsername()); +var_dump($url6->getUsername()); + +?> +--EXPECT-- +string(4) "user" +NULL +string(8) "%75s%2Fr" +string(9) "u%3As%2Fr" +NULL diff --git a/ext/uri/tests/025.phpt b/ext/uri/tests/025.phpt new file mode 100644 index 0000000000000..dafc36043bcbc --- /dev/null +++ b/ext/uri/tests/025.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test property mutation - password +--EXTENSIONS-- +uri +--FILE-- +withPassword("pass"); +$url3 = $url2->withPassword(null); +$url4 = $url3->withPassword("p%61ss"); +$url5 = $url4->withPassword("p:s/"); + +$url6 = Uri\WhatWg\Url::parse("file:///foo/bar/"); +$url6 = $url6->withUsername("pass"); + +var_dump($url2->getPassword()); +var_dump($url3->getPassword()); +var_dump($url4->getPassword()); +var_dump($url5->getPassword()); +var_dump($url6->getPassword()); + +?> +--EXPECT-- +string(4) "pass" +NULL +string(6) "p%61ss" +string(8) "p%3As%2F" +NULL diff --git a/ext/uri/tests/026.phpt b/ext/uri/tests/026.phpt new file mode 100644 index 0000000000000..4640ebebae52d --- /dev/null +++ b/ext/uri/tests/026.phpt @@ -0,0 +1,67 @@ +--TEST-- +Test property mutation - host +--EXTENSIONS-- +uri +--FILE-- +withHost("test.com"); +$url3 = $url2->withHost("t%65st.com"); // test.com +$url4 = $url3->withHost("test.com:8080"); + +var_dump($url1->getAsciiHost()); +var_dump($url2->getAsciiHost()); +var_dump($url3->getAsciiHost()); +var_dump($url4->getAsciiHost()); +var_dump($url4->getPort()); + +try { + $url4->withHost("t%3As%2Ft.com"); // t:s/t.com +} catch (Uri\WhatWg\InvalidUrlException $e) { + echo $e->getMessage() . "\n"; +} + +var_dump($url4->withHost("t:s/t.com")); + +try { + $url2->withHost(null); +} catch (Uri\WhatWg\InvalidUrlException $e) { + echo $e->getMessage() . "\n"; +} + +$url1 = Uri\WhatWg\Url::parse("ftp://foo.com?query=abc#foo"); +$url2 = $url1->withHost("test.com"); + +var_dump($url1->getAsciiHost()); +var_dump($url2->getAsciiHost()); + +?> +--EXPECTF-- +string(11) "example.com" +string(8) "test.com" +string(8) "test.com" +string(8) "test.com" +NULL +URL parsing failed +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(8) "test.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +URL parsing failed +string(7) "foo.com" +string(8) "test.com" diff --git a/ext/uri/tests/027.phpt b/ext/uri/tests/027.phpt new file mode 100644 index 0000000000000..79c121dd7f383 --- /dev/null +++ b/ext/uri/tests/027.phpt @@ -0,0 +1,36 @@ +--TEST-- +Test property mutation - port +--EXTENSIONS-- +uri +--FILE-- +withPort(22); +$url3 = $url2->withPort(null); + +var_dump($url1->getPort()); +var_dump($url2->getPort()); +var_dump($url3->getPort()); + +$url1 = Uri\WhatWg\Url::parse("ftp://foo.com:443?query=abc#foo"); +$url2 = $url1->withPort(8080); + +var_dump($url1->getPort()); +var_dump($url2->getPort()); + +$url1 = Uri\WhatWg\Url::parse("file:///foo/bar"); +$url2 = $url1->withPort(80); + +var_dump($url1->getPort()); +var_dump($url2->getPort()); + +?> +--EXPECT-- +int(8080) +int(22) +NULL +int(443) +int(8080) +NULL +NULL diff --git a/ext/uri/tests/028.phpt b/ext/uri/tests/028.phpt new file mode 100644 index 0000000000000..fd565c900e02f --- /dev/null +++ b/ext/uri/tests/028.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test property mutation - path +--EXTENSIONS-- +uri +--FILE-- +withPath("/foo"); +$url3 = $url2->withPath(""); +$url4 = $url3->withPath("t%65st"); +$url5 = $url4->withPath("/foo%2Fbar"); +$url6 = $url5->withPath("/#"); + +var_dump($url1->getPath()); +var_dump($url2->getPath()); +var_dump($url3->getPath()); +var_dump($url4->getPath()); +var_dump($url5->getPath()); +var_dump($url6->getPath()); + +$url1 = Uri\WhatWg\Url::parse("https://example.com/"); +$uri2 = $url1->withPath("/foo"); + +var_dump($url1->getPath()); +var_dump($url2->getPath()); + +?> +--EXPECT-- +string(9) "/foo/bar/" +string(4) "/foo" +string(1) "/" +string(7) "/t%65st" +string(10) "/foo%2Fbar" +string(4) "/%23" +string(1) "/" +string(4) "/foo" diff --git a/ext/uri/tests/029.phpt b/ext/uri/tests/029.phpt new file mode 100644 index 0000000000000..e23008a65ad6a --- /dev/null +++ b/ext/uri/tests/029.phpt @@ -0,0 +1,40 @@ +--TEST-- +Test property mutation - query +--EXTENSIONS-- +uri +--FILE-- +withQuery("?foo=baz"); +$url3 = $url2->withQuery(null); + +var_dump($url1->getQuery()); +var_dump($url2->getQuery()); +var_dump($url3->getQuery()); + +$url1 = Uri\WhatWg\Url::parse("https://example.com"); +$url2 = $url1->withQuery("?foo=bar&foo=baz"); +$url3 = $url1->withQuery("foo=bar&foo=baz"); +$url4 = $url3->withQuery("t%65st"); +$url5 = $url4->withQuery("foo=foo%26bar&baz=/qux%3D"); +$url6 = $url5->withQuery("#"); + +var_dump($url1->getQuery()); +var_dump($url2->getQuery()); +var_dump($url3->getQuery()); +var_dump($url4->getQuery()); +var_dump($url5->getQuery()); +var_dump($url6->getQuery()); + +?> +--EXPECT-- +string(7) "foo=bar" +string(7) "foo=baz" +NULL +NULL +string(15) "foo=bar&foo=baz" +string(15) "foo=bar&foo=baz" +string(6) "t%65st" +string(25) "foo=foo%26bar&baz=/qux%3D" +string(3) "%23" diff --git a/ext/uri/tests/030.phpt b/ext/uri/tests/030.phpt new file mode 100644 index 0000000000000..6bb85e6720c95 --- /dev/null +++ b/ext/uri/tests/030.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test property mutation - fragment +--EXTENSIONS-- +uri +--FILE-- +withFragment("#fragment2"); +$url3 = $url2->withFragment(null); +$url4 = $url3->withFragment(" "); + +var_dump($url1->getFragment()); +var_dump($url2->getFragment()); +var_dump($url3->getFragment()); +var_dump($url4->getFragment()); + +$url1 = Uri\WhatWg\Url::parse("https://example.com?abc=def"); +$url2 = $url1->withFragment("#fragment"); + +var_dump($url1->getFragment()); +var_dump($url2->getFragment()); + +?> +--EXPECT-- +string(9) "fragment1" +string(9) "fragment2" +NULL +string(3) "%20" +NULL +string(8) "fragment" diff --git a/ext/uri/tests/031.phpt b/ext/uri/tests/031.phpt new file mode 100644 index 0000000000000..0572a4ec11fe6 --- /dev/null +++ b/ext/uri/tests/031.phpt @@ -0,0 +1,98 @@ +--TEST-- +Test serialization and unserialization +--EXTENSIONS-- +uri +--FILE-- +getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":3:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}}'); // more than 2 items +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;N;i:1;a:0:{}}'); // first item is not an array +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:0:{}i:1;a:0:{}}'); // first array is empty +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:2:{s:3:"uri";s:19:"https://example.com";s:1:"a";i:1;}i:1;a:0:{}}'); // "uri" key in first array contains more than 1 item +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:1:{s:3:"uri";i:1;}i:1;a:0:{}}'); // "uri" key in first array is not a string +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:1:{s:3:"uri";s:11:"invalid-url";}i:1;a:0:{}}'); // "uri" key in first array contains invalid URL +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:1:{s:3:"uri";s:19:"https://example.com";}i:1;s:0:"";}'); // second item in not an array +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +try { + unserialize('O:14:"Uri\WhatWg\Url":2:{i:0;a:1:{s:3:"uri";s:19:"https://example.com";}i:1;a:1:{s:5:"prop1";i:123;}}'); // second array contains property +} catch (Exception $e) { + echo $e->getMessage() . "\n"; +} + +?> +--EXPECTF-- +string(162) "O:14:"Uri\WhatWg\Url":2:{i:0;a:1:{s:3:"uri";s:98:"https://username:password@www.google.com:8080/pathname1/pathname2/pathname3?query=true#hash-exists";}i:1;a:0:{}}" +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + string(8) "username" + ["password"]=> + string(8) "password" + ["host"]=> + string(14) "www.google.com" + ["port"]=> + int(8080) + ["path"]=> + string(30) "/pathname1/pathname2/pathname3" + ["query"]=> + string(10) "query=true" + ["fragment"]=> + string(11) "hash-exists" +} +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object +Invalid serialization data for Uri\WhatWg\Url object diff --git a/ext/uri/tests/032.phpt b/ext/uri/tests/032.phpt new file mode 100644 index 0000000000000..93bb80bcdb72a --- /dev/null +++ b/ext/uri/tests/032.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test JSON encoding +--EXTENSIONS-- +uri +--FILE-- + +--EXPECT-- +string(2) "{}" diff --git a/ext/uri/tests/033.phpt b/ext/uri/tests/033.phpt new file mode 100644 index 0000000000000..5b74af9b74f74 --- /dev/null +++ b/ext/uri/tests/033.phpt @@ -0,0 +1,15 @@ +--TEST-- +Test var_export +--EXTENSIONS-- +uri +--FILE-- + +--EXPECT-- +\Uri\WhatWg\Url::__set_state(array( +)) diff --git a/ext/uri/tests/034.phpt b/ext/uri/tests/034.phpt new file mode 100644 index 0000000000000..ccb0d9f6e5347 --- /dev/null +++ b/ext/uri/tests/034.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test array cast +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +array(%d) { +} diff --git a/ext/uri/tests/035.phpt b/ext/uri/tests/035.phpt new file mode 100644 index 0000000000000..6760e5dc0fb7a --- /dev/null +++ b/ext/uri/tests/035.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test URI parsing containing null bytes +--EXTENSIONS-- +uri +--FILE-- +getMessage() . "\n"; +} + +$url = new Uri\WhatWg\Url("https://example.com"); +try { + $url->withHost("exam\0ple.com"); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +?> +--EXPECT-- +Uri\WhatWg\Url::__construct(): Argument #1 ($uri) must not contain any null bytes +Uri\WhatWg\Url::withHost(): Argument #1 ($host) must not contain any null bytes diff --git a/ext/uri/tests/036.phpt b/ext/uri/tests/036.phpt new file mode 100644 index 0000000000000..adc4041db5506 --- /dev/null +++ b/ext/uri/tests/036.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test URI equality checks +--EXTENSIONS-- +uri +--FILE-- +equals(new Uri\WhatWg\Url("https://example.com"))); // true: identical URIs +var_dump(new Uri\WhatWg\Url("https://example.com#foo")->equals(new Uri\WhatWg\Url("https://example.com#bar"), Uri\UriComparisonMode::ExcludeFragment)); // true: fragment differs, but fragment is excluded +var_dump(new Uri\WhatWg\Url("https://example.com#foo")->equals(new Uri\WhatWg\Url("https://example.com#bar"), Uri\UriComparisonMode::IncludeFragment)); // false: fragment differs and fragment is included +var_dump(new Uri\WhatWg\Url("https://example.com/foo/..")->equals(new Uri\WhatWg\Url("https://example.com"))); // true: both URIs are https://example.com/ after normalization +var_dump(new Uri\WhatWg\Url("https://example.com/foo/..")->equals(new Uri\WhatWg\Url("https://example.com/"))); // true: both URIs are https://example.com/ after normalization +var_dump(new Uri\WhatWg\Url("http://example%2ecom/foo%2fb%61r")->equals(new Uri\WhatWg\Url("http://example%2ecom/foo/bar"))); // false: WHATWG doesn't percent-decode the path during normalization +var_dump(new Uri\WhatWg\Url("http://example%2ecom/foo/b%61r")->equals(new Uri\WhatWg\Url("http://example.com/foo/b%61r"))); // true: WHATWG percent-decodes the host during normalization + +?> +--EXPECT-- +bool(true) +bool(true) +bool(false) +bool(true) +bool(true) +bool(false) +bool(true) diff --git a/ext/uri/tests/038.phpt b/ext/uri/tests/038.phpt new file mode 100644 index 0000000000000..06171b258d6f8 --- /dev/null +++ b/ext/uri/tests/038.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test toString() +--EXTENSIONS-- +uri +--FILE-- +toUnicodeString()); +var_dump($url1->toAsciiString()); +var_dump($url2->toUnicodeString()); +var_dump($url2->toAsciiString()); +var_dump($url3->toUnicodeString()); +var_dump($url3->toAsciiString()); + +?> +--EXPECT-- +string(20) "https://example.com/" +string(20) "https://example.com/" +string(20) "https://example.com/" +string(20) "https://example.com/" +string(20) "https://example.com/" +string(20) "https://example.com/" diff --git a/ext/uri/tests/039.phpt b/ext/uri/tests/039.phpt new file mode 100644 index 0000000000000..6bf57cde97d95 --- /dev/null +++ b/ext/uri/tests/039.phpt @@ -0,0 +1,34 @@ +--TEST-- +Test percent-encoding of different URI components +--EXTENSIONS-- +uri +--FILE-- +getScheme()); + var_dump($url->getUsername()); + var_dump($url->getPassword()); + var_dump($url->getAsciiHost()); + var_dump($url->getUnicodeHost()); + var_dump($url->getPort()); + var_dump($url->getPath()); + var_dump($url->getQuery()); + var_dump($url->getFragment()); +} + +$url = Uri\WhatWg\Url::parse("http://%61pple:p%61ss@ex%61mple.com/foob%61r?%61bc=%61bc#%61bc"); +callWhatWgGetters($url); + +?> +--EXPECT-- +string(4) "http" +string(7) "%61pple" +string(6) "p%61ss" +string(11) "example.com" +string(11) "example.com" +NULL +string(9) "/foob%61r" +string(11) "%61bc=%61bc" +string(5) "%61bc" diff --git a/ext/uri/tests/040.phpt b/ext/uri/tests/040.phpt new file mode 100644 index 0000000000000..6bd66fd396f21 --- /dev/null +++ b/ext/uri/tests/040.phpt @@ -0,0 +1,32 @@ +--TEST-- +Test HTTP URL validation +--EXTENSIONS-- +uri +--FILE-- +toAsciiString()); + +?> +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(1) "/" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +string(20) "https://example.com/" diff --git a/ext/uri/tests/041.phpt b/ext/uri/tests/041.phpt new file mode 100644 index 0000000000000..5cfcec2628dab --- /dev/null +++ b/ext/uri/tests/041.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test relative URI parsing +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +NULL +array(%d) { + [0]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(15) "?query#fragment" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::MissingSchemeNonRelativeUrl) + ["failure"]=> + bool(true) + } +} diff --git a/ext/uri/tests/042.phpt b/ext/uri/tests/042.phpt new file mode 100644 index 0000000000000..caf63366e2b19 --- /dev/null +++ b/ext/uri/tests/042.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test URN parsing +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(3) "urn" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + NULL + ["port"]=> + NULL + ["path"]=> + string(41) "uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/043.phpt b/ext/uri/tests/043.phpt new file mode 100644 index 0000000000000..d9f17d45024c9 --- /dev/null +++ b/ext/uri/tests/043.phpt @@ -0,0 +1,71 @@ +--TEST-- +Test reference resolution +--EXTENSIONS-- +uri +--FILE-- + +--EXPECTF-- +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(14) "/without-base/" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(11) "example.com" + ["port"]=> + NULL + ["path"]=> + string(10) "/with-base" + ["query"]=> + NULL + ["fragment"]=> + NULL +} +object(Uri\WhatWg\Url)#%d (%d) { + ["scheme"]=> + string(5) "https" + ["username"]=> + NULL + ["password"]=> + NULL + ["host"]=> + string(8) "test.com" + ["port"]=> + NULL + ["path"]=> + string(18) "/with-base-in-vain" + ["query"]=> + NULL + ["fragment"]=> + NULL +} diff --git a/ext/uri/tests/045.phpt b/ext/uri/tests/045.phpt new file mode 100644 index 0000000000000..13137d6a42b69 --- /dev/null +++ b/ext/uri/tests/045.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test percent-decoding of reserved characters in the path +--EXTENSIONS-- +uri +--FILE-- +toAsciiString()); +var_dump($url->getPath()); + +?> +--EXPECT-- +string(33) "https://example.com/foo/bar%2Fbaz" +string(14) "/foo/bar%2Fbaz" diff --git a/ext/uri/tests/046.phpt b/ext/uri/tests/046.phpt new file mode 100644 index 0000000000000..cf283ad5297e5 --- /dev/null +++ b/ext/uri/tests/046.phpt @@ -0,0 +1,30 @@ +--TEST-- +Test special path variants +--EXTENSIONS-- +uri +--FILE-- +toAsciiString()); +var_dump($url->getPath()); + +$url = new Uri\Whatwg\Url("https://example.com"); + +var_dump($url->toAsciiString()); +var_dump($url->getPath()); + +$url = new Uri\Whatwg\Url("https://example.com/"); + +var_dump($url->toAsciiString()); +var_dump($url->getPath()); + +?> +--EXPECT-- +string(26) "mailto:johndoe@example.com" +string(19) "johndoe@example.com" +string(20) "https://example.com/" +string(1) "/" +string(20) "https://example.com/" +string(1) "/" diff --git a/ext/uri/tests/047.phpt b/ext/uri/tests/047.phpt new file mode 100644 index 0000000000000..4ab3a0584de79 --- /dev/null +++ b/ext/uri/tests/047.phpt @@ -0,0 +1,27 @@ +--TEST-- +Test IP addresses +--EXTENSIONS-- +uri +--FILE-- +getAsciiHost()); +var_dump($url->toAsciiString()); + +$url = new Uri\WhatWg\Url("https://[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]"); +var_dump($url->getAsciiHost()); +var_dump($url->toAsciiString()); + +$url = new Uri\WhatWg\Url("https://[0:0::1]"); +var_dump($url->getAsciiHost()); +var_dump($url->toAsciiString()); + +?> +--EXPECT-- +string(11) "192.168.0.1" +string(20) "https://192.168.0.1/" +string(26) "[2001:db8:1::ab9:c0a8:102]" +string(35) "https://[2001:db8:1::ab9:c0a8:102]/" +string(5) "[::1]" +string(14) "https://[::1]/" diff --git a/ext/uri/tests/049.phpt b/ext/uri/tests/049.phpt new file mode 100644 index 0000000000000..41e6eaeea3cf9 --- /dev/null +++ b/ext/uri/tests/049.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test percent-encoding normalization - special case +--EXTENSIONS-- +uri +--FILE-- +getPath()); + +?> +--EXPECT-- +string(14) "/foo/bar%2Fbaz" diff --git a/ext/uri/tests/050.phpt b/ext/uri/tests/050.phpt new file mode 100644 index 0000000000000..12af66721cf65 --- /dev/null +++ b/ext/uri/tests/050.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test resolve() method - success cases +--EXTENSIONS-- +uri +--FILE-- +resolve("/foo/")->toAsciiString()); +var_dump($url->resolve("https://test.com/foo")->toAsciiString()); + +?> +--EXPECTF-- +string(24) "https://example.com/foo/" +string(20) "https://test.com/foo" diff --git a/ext/uri/tests/051.phpt b/ext/uri/tests/051.phpt new file mode 100644 index 0000000000000..5911f8767567c --- /dev/null +++ b/ext/uri/tests/051.phpt @@ -0,0 +1,35 @@ +--TEST-- +Test resolve() method - error cases +--EXTENSIONS-- +uri +--FILE-- +resolve("https://1.2.3.4.5"); +} catch (Uri\WhatWg\InvalidUrlException $e) { + echo $e->getMessage() . "\n"; +} + +$softErrors = []; + +var_dump($url->resolve(" /foo", $softErrors)->toAsciiString()); +var_dump($softErrors); + +?> +--EXPECTF-- +URL parsing failed +string(23) "https://example.com/foo" +array(%d) { + [0]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(5) " /foo" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit) + ["failure"]=> + bool(false) + } +} diff --git a/ext/uri/tests/052.phpt b/ext/uri/tests/052.phpt new file mode 100644 index 0000000000000..af7d05b893ea5 --- /dev/null +++ b/ext/uri/tests/052.phpt @@ -0,0 +1,28 @@ +--TEST-- +Test UrlValidationError constructor error handling +--EXTENSIONS-- +uri +--FILE-- +__construct('bar', Uri\WhatWg\UrlValidationErrorType::HostMissing, false); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +var_dump($r); + +?> +--EXPECTF-- +Cannot modify readonly property Uri\WhatWg\UrlValidationError::$context +object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(3) "foo" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::DomainInvalidCodePoint) + ["failure"]=> + bool(true) +} diff --git a/ext/uri/tests/053.phpt b/ext/uri/tests/053.phpt new file mode 100644 index 0000000000000..93ff77b15c0a5 --- /dev/null +++ b/ext/uri/tests/053.phpt @@ -0,0 +1,63 @@ +--TEST-- +Test InvalidUrlException constructor error handling +--EXTENSIONS-- +uri +--FILE-- +__construct("foo"); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + $r->__construct("bar", []); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + $r->__construct("baz", [], false); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + $r->__construct("qax", [], false, null); +} catch (Error $e) { + echo $e->getMessage() . "\n"; +} + +var_dump($r->getMessage()); +var_dump($r->errors); +var_dump($r->getCode()); +var_dump($r->getPrevious()::class); + +?> +--EXPECTF-- +Cannot modify readonly property Uri\WhatWg\InvalidUrlException::$errors +Cannot modify readonly property Uri\WhatWg\InvalidUrlException::$errors +Cannot modify readonly property Uri\WhatWg\InvalidUrlException::$errors +Cannot modify readonly property Uri\WhatWg\InvalidUrlException::$errors +string(3) "qax" +array(%d) { + [%d]=> + object(Uri\WhatWg\UrlValidationError)#%d (%d) { + ["context"]=> + string(3) "abc" + ["type"]=> + enum(Uri\WhatWg\UrlValidationErrorType::DomainInvalidCodePoint) + ["failure"]=> + bool(true) + } +} +int(1) +string(9) "Exception" From 5f9a0b568b18d74391807d56afaee69e9e4f8aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 10 Jun 2025 15:28:34 +0200 Subject: [PATCH 32/44] gen_stub: Fix undefined variable warning (#18821) > PHP Warning: Undefined variable $code in build/gen_stub.php on line 5322 Introduced in php/php-src#18735. --- build/gen_stub.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/gen_stub.php b/build/gen_stub.php index 0e87cdd9a0b40..ff86106e8a2a4 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -5313,6 +5313,8 @@ function generateGlobalConstantAttributeInitialization( $constInfos, "", static function (ConstInfo $constInfo) use ($allConstInfos, $phpVersionIdMinimumCompatibility) { + $code = ""; + if ($constInfo->attributes === []) { return null; } From 8f3e5553f386b66ad77fb562d641a8fd8c2fcefb Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 10 Jun 2025 17:33:42 +0200 Subject: [PATCH 33/44] Use zval_try_get_string_func() in concat_function() (#18815) This allows a cheaper exception check and also does not need a release call. This shrinks concat_function() on x86-64 with GCC 15.1.1 from 3443 bytes to 3332 bytes. --- Zend/zend_operators.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 140734d1b9602..7e456ff68246a 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -1989,9 +1989,8 @@ ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval } } ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT); - op1_string = zval_get_string_func(op1); - if (UNEXPECTED(EG(exception))) { - zend_string_release(op1_string); + op1_string = zval_try_get_string_func(op1); + if (UNEXPECTED(!op1_string)) { if (orig_op1 != result) { ZVAL_UNDEF(result); } @@ -2023,10 +2022,9 @@ ZEND_API zend_result ZEND_FASTCALL concat_function(zval *result, zval *op1, zval free_op1_string = true; } ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT); - op2_string = zval_get_string_func(op2); - if (UNEXPECTED(EG(exception))) { + op2_string = zval_try_get_string_func(op2); + if (UNEXPECTED(!op2_string)) { zend_string_release(op1_string); - zend_string_release(op2_string); if (orig_op1 != result) { ZVAL_UNDEF(result); } From 594221fff2d635528c58bd6273e923e56ff53c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 10 Jun 2025 19:15:35 +0200 Subject: [PATCH 34/44] cli: Fix tests/bug80092.phpt expectation for `PHP_BUILD_PROVIDER` (#18824) see afc5738154b8e0e7f8bcb5d6a521514bb495a0c0 see 40d88cacc1db11787aa2fde6d0ee4b6064746d94 --- sapi/cli/tests/bug80092.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapi/cli/tests/bug80092.phpt b/sapi/cli/tests/bug80092.phpt index 350b46b3f57a6..1fb2e8664cc1e 100644 --- a/sapi/cli/tests/bug80092.phpt +++ b/sapi/cli/tests/bug80092.phpt @@ -43,5 +43,5 @@ foreach (explode("\n", $output) as $line) { preloaded PHP %s Copyright (c) The PHP Group -Zend Engine %s +%AZend Engine %s %A with Zend OPcache %a From eb151e39b029ced2e40e013718d305a67b81d205 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 14:47:02 +0200 Subject: [PATCH 35/44] Properly handle reference return value from __toString() It's possible to return a reference from __toString(), but this is not handled and results in a (confusing) error telling that the return value must be a string. Properly handle this by unwrapping the reference. Closes GH-18810. --- NEWS | 1 + ...tal_uncaught_error_reference_tostring.phpt | 19 +++++++++++++++++++ .../string_cast_reference_tostring.phpt | 17 +++++++++++++++++ Zend/zend_exceptions.c | 3 +++ Zend/zend_object_handlers.c | 4 ++++ sapi/phpdbg/phpdbg_prompt.c | 4 ++++ 6 files changed, 48 insertions(+) create mode 100644 Zend/tests/exceptions/exception_fatal_uncaught_error_reference_tostring.phpt create mode 100644 Zend/tests/type_casts/string_cast_reference_tostring.phpt diff --git a/NEWS b/NEWS index b1100f7672226..bf2b1937d7945 100644 --- a/NEWS +++ b/NEWS @@ -54,6 +54,7 @@ PHP NEWS released on bailout). (DanielEScherzer and ilutov) . Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko) . Properly handle __debugInfo() returning an array reference. (nielsdos) + . Properly handle reference return value from __toString(). (nielsdos) . Added the pipe (|>) operator. (crell) - Curl: diff --git a/Zend/tests/exceptions/exception_fatal_uncaught_error_reference_tostring.phpt b/Zend/tests/exceptions/exception_fatal_uncaught_error_reference_tostring.phpt new file mode 100644 index 0000000000000..6222a0895baf2 --- /dev/null +++ b/Zend/tests/exceptions/exception_fatal_uncaught_error_reference_tostring.phpt @@ -0,0 +1,19 @@ +--TEST-- +Exception fatal uncaught error with reference __toString +--FILE-- +field; + } +} + +// Must not be caught to trigger the issue! +throw new MyException; + +?> +--EXPECTF-- +Fatal error: Uncaught my string + thrown in %s on line %d diff --git a/Zend/tests/type_casts/string_cast_reference_tostring.phpt b/Zend/tests/type_casts/string_cast_reference_tostring.phpt new file mode 100644 index 0000000000000..96b47a4fba9bc --- /dev/null +++ b/Zend/tests/type_casts/string_cast_reference_tostring.phpt @@ -0,0 +1,17 @@ +--TEST-- +String cast with reference __toString +--FILE-- +field; + } +} + +echo new MyClass; + +?> +--EXPECT-- +my string diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index ab9c815718a0d..212fe3cb7ab66 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -969,6 +969,9 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp); if (!EG(exception)) { + if (UNEXPECTED(Z_ISREF(tmp))) { + zend_unwrap_reference(&tmp); + } if (Z_TYPE(tmp) != IS_STRING) { zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name)); } else { diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index f79023ade1c25..7b804e7afe95a 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -2440,8 +2440,12 @@ ZEND_API zend_result zend_std_cast_object_tostring(zend_object *readobj, zval *w zend_call_known_instance_method_with_0_params(ce->__tostring, readobj, &retval); zend_object_release(readobj); if (EXPECTED(Z_TYPE(retval) == IS_STRING)) { +is_string: ZVAL_COPY_VALUE(writeobj, &retval); return SUCCESS; + } else if (Z_ISREF(retval)) { + zend_unwrap_reference(&retval); + goto is_string; } zval_ptr_dtor(&retval); if (!EG(exception)) { diff --git a/sapi/phpdbg/phpdbg_prompt.c b/sapi/phpdbg/phpdbg_prompt.c index 92c139fa52abe..84bd7a076acec 100644 --- a/sapi/phpdbg/phpdbg_prompt.c +++ b/sapi/phpdbg/phpdbg_prompt.c @@ -702,6 +702,10 @@ static inline void phpdbg_handle_exception(void) /* {{{ */ EG(exception) = NULL; msg = ZSTR_EMPTY_ALLOC(); } else { + if (UNEXPECTED(Z_ISREF(tmp))) { + zend_unwrap_reference(&tmp); + } + ZEND_ASSERT(Z_TYPE(tmp) == IS_STRING); zend_update_property_string(zend_get_exception_base(ex), ex, ZEND_STRL("string"), Z_STRVAL(tmp)); zval_ptr_dtor(&tmp); msg = zval_get_string(zend_read_property_ex(zend_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), /* silent */ true, &rv)); From 0cd3ebfc40c6f4aa90fad8bab05b09f274465385 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 9 Jun 2025 17:49:27 +0200 Subject: [PATCH 36/44] Fix 'phpdbg --help' segfault on shutdown with USE_ZEND_ALLOC=0 This hack not only breaks the handling of custom allocators, but also breaks if zend_alloc is compiled with USE_CUSTOM_MM. This hack is just no good, if you want leak information then use ASAN. Closes GH-18813. --- NEWS | 3 +++ sapi/phpdbg/phpdbg.c | 6 ------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 11fb787662c4a..a665633283d0c 100644 --- a/NEWS +++ b/NEWS @@ -39,6 +39,9 @@ PHP NEWS . Add missing filter cleanups on phar failure. (nielsdos) . Fixed bug GH-18642 (Signed integer overflow in ext/phar fseek). (nielsdos) +- PHPDBG: + . Fix 'phpdbg --help' segfault on shutdown with USE_ZEND_ALLOC=0. (nielsdos) + - PDO ODBC: . Fix memory leak if WideCharToMultiByte() fails. (nielsdos) diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index 5a4dd6acdbe2f..f27d87de84187 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -179,12 +179,6 @@ static PHP_MSHUTDOWN_FUNCTION(phpdbg) /* {{{ */ phpdbg_notice("Script ended normally"); } - /* hack to restore mm_heap->use_custom_heap in order to receive memory leak info */ - if (use_mm_wrappers) { - /* ASSUMING that mm_heap->use_custom_heap is the first element of the struct ... */ - *(int *) zend_mm_get_heap() = 0; - } - if (PHPDBG_G(buffer)) { free(PHPDBG_G(buffer)); PHPDBG_G(buffer) = NULL; From 0a95b2f30caa26cf540d48b47c623995acd1183c Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:54:29 +0200 Subject: [PATCH 37/44] Fix GH-18820: Windows compilation issue: php-src\Zend\zend_exceptions.h(75): error C2122: 'message': prototype parameter in name list illegal INTERNAL_FUNCTION_PARAMETERS is defined in zend.h, but not included in zend_exceptions.h (and it shouldn't). Expand the macro to fix the compile issue. --- Zend/zend_exceptions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zend/zend_exceptions.h b/Zend/zend_exceptions.h index 86dc379cce871..35f6699559421 100644 --- a/Zend/zend_exceptions.h +++ b/Zend/zend_exceptions.h @@ -69,7 +69,7 @@ ZEND_API zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, extern ZEND_API void (*zend_throw_exception_hook)(zend_object *ex); -ZEND_API zend_result zend_update_exception_properties(INTERNAL_FUNCTION_PARAMETERS, zend_string *message, zend_long code, zval *previous); +ZEND_API zend_result zend_update_exception_properties(zend_execute_data *execute_data, zval *return_value, zend_string *message, zend_long code, zval *previous); /* show an exception using zend_error(severity,...), severity should be E_ERROR */ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity); From 559858c82249155db5c362cf6a7efcc2665414a8 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:33:33 +0200 Subject: [PATCH 38/44] Improve performance of unpack() with nameless repetitions (#18803) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can avoid creating temporary strings, and then reparsing them into numbers with zend_symtable_update() by using zend_hash_index_update() directly. For the following benchmark on an i7-4790: ```php $file = str_repeat('A', 100000); for ($i=0;$i<100;$i++) unpack('C*',$file); ``` I get: ``` Benchmark 1: ./sapi/cli/php y.php Time (mean ± σ): 85.8 ms ± 1.8 ms [User: 74.5 ms, System: 10.4 ms] Range (min … max): 83.8 ms … 92.4 ms 33 runs Benchmark 2: ./sapi/cli/php_old y.php Time (mean ± σ): 318.3 ms ± 2.7 ms [User: 306.7 ms, System: 9.9 ms] Range (min … max): 314.9 ms … 321.6 ms 10 runs Summary ./sapi/cli/php y.php ran 3.71 ± 0.08 times faster than ./sapi/cli/php_old y.php ``` On an i7-1185G7 I get: ``` Benchmark 1: ./sapi/cli/php test.php Time (mean ± σ): 60.1 ms ± 0.7 ms [User: 47.8 ms, System: 12.0 ms] Range (min … max): 59.2 ms … 63.8 ms 48 runs Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options. Benchmark 2: ./sapi/cli/php_old test.php Time (mean ± σ): 220.8 ms ± 2.2 ms [User: 209.6 ms, System: 10.7 ms] Range (min … max): 218.5 ms … 224.5 ms 13 runs Summary ./sapi/cli/php test.php ran 3.67 ± 0.06 times faster than ./sapi/cli/php_old test.php ``` --- UPGRADING | 2 ++ ext/standard/pack.c | 40 +++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/UPGRADING b/UPGRADING index 7025c9778e7ac..26c87d7aeb795 100644 --- a/UPGRADING +++ b/UPGRADING @@ -582,6 +582,8 @@ PHP 8.5 UPGRADE NOTES . Improved performance of array functions with callbacks (array_find, array_filter, array_map, usort, ...). . Improved performance of urlencode() and rawurlencode(). + . Improved unpack() performance with nameless repetitions by avoiding + creating temporary strings and reparsing them. - XMLReader: . Improved property access performance. diff --git a/ext/standard/pack.c b/ext/standard/pack.c index ec30be436741d..1dc04dab86c1a 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -885,12 +885,15 @@ PHP_FUNCTION(unpack) if ((inputpos + size) <= inputlen) { zend_string* real_name; + zend_long long_key = 0; zval val; - if (repetitions == 1 && namelen > 0) { + if (namelen == 0) { + real_name = NULL; + long_key = i + 1; + } else if (repetitions == 1) { /* Use a part of the formatarg argument directly as the name. */ real_name = zend_string_init_fast(name, namelen); - } else { /* Need to add the 1-based element number to the name */ char buf[MAX_LENGTH_OF_LONG + 1]; @@ -912,7 +915,6 @@ PHP_FUNCTION(unpack) size = len; ZVAL_STRINGL(&val, &input[inputpos], len); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } case 'A': { @@ -939,7 +941,6 @@ PHP_FUNCTION(unpack) } ZVAL_STRINGL(&val, &input[inputpos], len + 1); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } /* New option added for Z to remain in-line with the Perl implementation */ @@ -964,7 +965,6 @@ PHP_FUNCTION(unpack) len = s; ZVAL_STRINGL(&val, &input[inputpos], len); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -979,7 +979,9 @@ PHP_FUNCTION(unpack) if (size > INT_MAX / 2) { - zend_string_release(real_name); + if (real_name) { + zend_string_release_ex(real_name, false); + } zend_argument_value_error(1, "repeater must be less than or equal to %d", INT_MAX / 2); RETURN_THROWS(); } @@ -1016,7 +1018,6 @@ PHP_FUNCTION(unpack) ZSTR_VAL(buf)[len] = '\0'; ZVAL_STR(&val, buf); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -1026,7 +1027,6 @@ PHP_FUNCTION(unpack) zend_long v = (type == 'c') ? (int8_t) x : x; ZVAL_LONG(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -1046,7 +1046,6 @@ PHP_FUNCTION(unpack) } ZVAL_LONG(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -1062,7 +1061,6 @@ PHP_FUNCTION(unpack) } ZVAL_LONG(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -1082,8 +1080,6 @@ PHP_FUNCTION(unpack) } ZVAL_LONG(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); - break; } @@ -1104,7 +1100,6 @@ PHP_FUNCTION(unpack) } ZVAL_LONG(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } #endif @@ -1124,7 +1119,6 @@ PHP_FUNCTION(unpack) } ZVAL_DOUBLE(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } @@ -1143,13 +1137,12 @@ PHP_FUNCTION(unpack) } ZVAL_DOUBLE(&val, v); - zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); break; } case 'x': /* Do nothing with input, just skip it */ - break; + goto no_output; case 'X': if (inputpos < size) { @@ -1160,7 +1153,7 @@ PHP_FUNCTION(unpack) php_error_docref(NULL, E_WARNING, "Type %c: outside of string", type); } } - break; + goto no_output; case '@': if (repetitions <= inputlen) { @@ -1170,10 +1163,19 @@ PHP_FUNCTION(unpack) } i = repetitions - 1; /* Done, break out of for loop */ - break; + goto no_output; } - zend_string_release(real_name); + if (real_name) { + zend_symtable_update(Z_ARRVAL_P(return_value), real_name, &val); + } else { + zend_hash_index_update(Z_ARRVAL_P(return_value), long_key, &val); + } + +no_output: + if (real_name) { + zend_string_release_ex(real_name, false); + } inputpos += size; if (inputpos < 0) { From dbabbe180b157eeaac5002276667f1f56f0b4def Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:35:56 +0200 Subject: [PATCH 39/44] Remove dead code from openssl_spki_new() implementation (#18752) If s is not NULL, the length can't be <= 0 because we at least append `spkac` in the string, which is non-empty. I noticed this condition because if it were actually possible to execute, then it would leak memory. --- ext/openssl/openssl.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c index 6518a719314fd..4d1567f56d8c2 100644 --- a/ext/openssl/openssl.c +++ b/ext/openssl/openssl.c @@ -652,10 +652,6 @@ PHP_FUNCTION(openssl_spki_new) if (spki != NULL) { NETSCAPE_SPKI_free(spki); } - - if (s && ZSTR_LEN(s) <= 0) { - RETVAL_FALSE; - } } /* }}} */ From 2a77e282f86f3f2ecfcd68a83e7dcddf92165995 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:09:49 +0100 Subject: [PATCH 40/44] ext/standard/pack: Inline constant single use variables They serve no purpose and are just confusing --- ext/standard/pack.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 1dc04dab86c1a..b0ed378b49ec0 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -919,7 +919,6 @@ PHP_FUNCTION(unpack) } case 'A': { /* A will strip any trailing whitespace */ - char padn = '\0'; char pads = ' '; char padt = '\t'; char padc = '\r'; char padl = '\n'; zend_long len = inputlen - inputpos; /* Remaining string */ /* If size was given take minimum of len and size */ @@ -931,11 +930,11 @@ PHP_FUNCTION(unpack) /* Remove trailing white space and nulls chars from unpacked data */ while (--len >= 0) { - if (input[inputpos + len] != padn - && input[inputpos + len] != pads - && input[inputpos + len] != padt - && input[inputpos + len] != padc - && input[inputpos + len] != padl + if (input[inputpos + len] != '\0' + && input[inputpos + len] != ' ' + && input[inputpos + len] != '\t' + && input[inputpos + len] != '\r' + && input[inputpos + len] != '\n' ) break; } @@ -946,7 +945,6 @@ PHP_FUNCTION(unpack) /* New option added for Z to remain in-line with the Perl implementation */ case 'Z': { /* Z will strip everything after the first null character */ - char pad = '\0'; zend_long s, len = inputlen - inputpos; /* Remaining string */ @@ -959,7 +957,7 @@ PHP_FUNCTION(unpack) /* Remove everything after the first null */ for (s=0 ; s < len ; s++) { - if (input[inputpos + s] == pad) + if (input[inputpos + s] == '\0') break; } len = s; From e96a7f0dfb50aaab94a04276d2bf06e37ecfc62d Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:14:22 +0100 Subject: [PATCH 41/44] ext/standard/pack: Remove useless casts And use char instead of widening to int for no reason --- ext/standard/pack.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index b0ed378b49ec0..e6ca60d456d42 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -274,7 +274,7 @@ PHP_FUNCTION(pack) } /* Handle special arg '*' for all codes and check argv overflows */ - switch ((int) code) { + switch (code) { /* Never uses any args */ case 'x': case 'X': @@ -380,10 +380,10 @@ PHP_FUNCTION(pack) /* Calculate output length and upper bound while processing*/ for (i = 0; i < formatcount; i++) { - int code = (int) formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; - switch ((int) code) { + switch (code) { case 'h': case 'H': INC_OUTPUTPOS((arg + (arg % 2)) / 2,1) /* 4 bit per arg */ @@ -463,10 +463,10 @@ PHP_FUNCTION(pack) /* Do actual packing */ for (i = 0; i < formatcount; i++) { - int code = (int) formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; - switch ((int) code) { + switch (code) { case 'a': case 'A': case 'Z': { @@ -632,7 +632,7 @@ PHP_FUNCTION(pack) case 'd': { while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); memcpy(&ZSTR_VAL(output)[outputpos], &v, sizeof(v)); outputpos += sizeof(v); } @@ -642,7 +642,7 @@ PHP_FUNCTION(pack) case 'e': { /* pack little endian double */ while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); php_pack_copy_double(1, &ZSTR_VAL(output)[outputpos], v); outputpos += sizeof(v); } @@ -652,7 +652,7 @@ PHP_FUNCTION(pack) case 'E': { /* pack big endian double */ while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); php_pack_copy_double(0, &ZSTR_VAL(output)[outputpos], v); outputpos += sizeof(v); } @@ -784,7 +784,7 @@ PHP_FUNCTION(unpack) if (namelen > 200) namelen = 200; - switch ((int) type) { + switch (type) { /* Never use any input */ case 'X': size = -1; @@ -902,7 +902,7 @@ PHP_FUNCTION(unpack) real_name = zend_string_concat2(name, namelen, res, digits); } - switch ((int) type) { + switch (type) { case 'a': { /* a will not strip any trailing whitespace or null padding */ zend_long len = inputlen - inputpos; /* Remaining string */ From 34e22c54bc75b7bbb1c3a2daa01e0a3d300cb6cb Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:20:04 +0100 Subject: [PATCH 42/44] ext/standard/pack: Reduce scope of variable --- ext/standard/pack.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index e6ca60d456d42..7db7724cdb2ed 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -737,7 +737,6 @@ PHP_FUNCTION(unpack) while (formatlen-- > 0) { char type = *(format++); - char c; int repetitions = 1, argb; char *name; int namelen; @@ -745,7 +744,7 @@ PHP_FUNCTION(unpack) /* Handle format arguments if any */ if (formatlen > 0) { - c = *format; + char c = *format; if (c >= '0' && c <= '9') { errno = 0; From a297a44d2fbd9a32148c2373111131dce685f9bb Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:24:51 +0100 Subject: [PATCH 43/44] ext/standard/pack: Remove unused header includes --- ext/standard/pack.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 7db7724cdb2ed..a7568544be021 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -16,30 +16,10 @@ #include "php.h" -#include #include #include #include -#include -#include -#ifdef PHP_WIN32 -#define O_RDONLY _O_RDONLY -#include "win32/param.h" -#else -#include -#endif #include "pack.h" -#ifdef HAVE_PWD_H -#ifdef PHP_WIN32 -#include "win32/pwd.h" -#else -#include -#endif -#endif -#include "fsock.h" -#ifdef HAVE_NETINET_IN_H -#include -#endif #define INC_OUTPUTPOS(a,b) \ if ((a) < 0 || ((INT_MAX - outputpos)/((int)b)) < (a)) { \ From def3a95b14618774e63f42829edcc2ffe026bf63 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 15:14:07 +0100 Subject: [PATCH 44/44] [skip ci] ext/standard/pack: Fix indentation to use tabs --- ext/standard/pack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index a7568544be021..d4c5cc1f04cfa 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -360,7 +360,7 @@ PHP_FUNCTION(pack) /* Calculate output length and upper bound while processing*/ for (i = 0; i < formatcount; i++) { - char code = formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; switch (code) { @@ -443,7 +443,7 @@ PHP_FUNCTION(pack) /* Do actual packing */ for (i = 0; i < formatcount; i++) { - char code = formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; switch (code) {