From 49026872615a907cb2f168fe24c8ac039322cb31 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 17 Sep 2024 00:05:32 +0200 Subject: [PATCH 1/2] Fix GH-15901: phpdbg: Assertion failure on `i funcs` New hash tables are not automatically packed, so we must not treat them as such. Therefore we guard the foreach appropriately. --- sapi/phpdbg/phpdbg_info.c | 60 ++++++++++++++++++---------------- sapi/phpdbg/tests/gh15901.phpt | 10 ++++++ 2 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 sapi/phpdbg/tests/gh15901.phpt diff --git a/sapi/phpdbg/phpdbg_info.c b/sapi/phpdbg/phpdbg_info.c index b6c48d548f1f0..9969bb318632b 100644 --- a/sapi/phpdbg/phpdbg_info.c +++ b/sapi/phpdbg/phpdbg_info.c @@ -399,27 +399,29 @@ PHPDBG_INFO(classes) /* {{{ */ phpdbg_notice("User Classes (%d)", zend_hash_num_elements(&classes)); /* once added, assume that classes are stable... until shutdown. */ - ZEND_HASH_PACKED_FOREACH_PTR(&classes, ce) { - phpdbg_print_class_name(ce); - - if (ce->parent) { - if (ce->ce_flags & ZEND_ACC_LINKED) { - zend_class_entry *pce = ce->parent; - do { - phpdbg_out("|-------- "); - phpdbg_print_class_name(pce); - } while ((pce = pce->parent)); - } else { - phpdbg_writeln("|-------- User Class %s (not yet linked because declaration for parent was not encountered when declaring the class)", ZSTR_VAL(ce->parent_name)); + if (HT_IS_PACKED(&classes)) { + ZEND_HASH_PACKED_FOREACH_PTR(&classes, ce) { + phpdbg_print_class_name(ce); + + if (ce->parent) { + if (ce->ce_flags & ZEND_ACC_LINKED) { + zend_class_entry *pce = ce->parent; + do { + phpdbg_out("|-------- "); + phpdbg_print_class_name(pce); + } while ((pce = pce->parent)); + } else { + phpdbg_writeln("|-------- User Class %s (not yet linked because declaration for parent was not encountered when declaring the class)", ZSTR_VAL(ce->parent_name)); + } } - } - if (ce->info.user.filename) { - phpdbg_writeln("|---- in %s on line %u", ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start); - } else { - phpdbg_writeln("|---- no source code"); - } - } ZEND_HASH_FOREACH_END(); + if (ce->info.user.filename) { + phpdbg_writeln("|---- in %s on line %u", ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start); + } else { + phpdbg_writeln("|---- no source code"); + } + } ZEND_HASH_FOREACH_END(); + } zend_hash_destroy(&classes); @@ -445,17 +447,19 @@ PHPDBG_INFO(funcs) /* {{{ */ phpdbg_notice("User Functions (%d)", zend_hash_num_elements(&functions)); - ZEND_HASH_PACKED_FOREACH_PTR(&functions, zf) { - zend_op_array *op_array = &zf->op_array; + if (HT_IS_PACKED(&functions)) { + ZEND_HASH_PACKED_FOREACH_PTR(&functions, zf) { + zend_op_array *op_array = &zf->op_array; - phpdbg_write("|-------- %s", op_array->function_name ? ZSTR_VAL(op_array->function_name) : "{main}"); + phpdbg_write("|-------- %s", op_array->function_name ? ZSTR_VAL(op_array->function_name) : "{main}"); - if (op_array->filename) { - phpdbg_writeln(" in %s on line %d", ZSTR_VAL(op_array->filename), op_array->line_start); - } else { - phpdbg_writeln(" (no source code)"); - } - } ZEND_HASH_FOREACH_END(); + if (op_array->filename) { + phpdbg_writeln(" in %s on line %d", ZSTR_VAL(op_array->filename), op_array->line_start); + } else { + phpdbg_writeln(" (no source code)"); + } + } ZEND_HASH_FOREACH_END(); + } zend_hash_destroy(&functions); diff --git a/sapi/phpdbg/tests/gh15901.phpt b/sapi/phpdbg/tests/gh15901.phpt new file mode 100644 index 0000000000000..00783b71968a2 --- /dev/null +++ b/sapi/phpdbg/tests/gh15901.phpt @@ -0,0 +1,10 @@ +--TEST-- +GH-15901 (phpdbg: Assertion failure on `i funcs`) +--PHPDBG-- +i funcs +i classes +--EXPECT-- +prompt> [User Functions (0)] +prompt> [User Classes (0)] +prompt> [User Classes (0)] +prompt> From fe95e68e1cf94c48940c7a04ff5c550ece637c29 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Wed, 18 Sep 2024 18:51:35 +0200 Subject: [PATCH 2/2] Check for HT_IS_INITIALIZED() instead of HT_IS_PACKED() --- sapi/phpdbg/phpdbg_info.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sapi/phpdbg/phpdbg_info.c b/sapi/phpdbg/phpdbg_info.c index 9969bb318632b..d64703755cbe0 100644 --- a/sapi/phpdbg/phpdbg_info.c +++ b/sapi/phpdbg/phpdbg_info.c @@ -399,7 +399,7 @@ PHPDBG_INFO(classes) /* {{{ */ phpdbg_notice("User Classes (%d)", zend_hash_num_elements(&classes)); /* once added, assume that classes are stable... until shutdown. */ - if (HT_IS_PACKED(&classes)) { + if (HT_IS_INITIALIZED(&classes)) { ZEND_HASH_PACKED_FOREACH_PTR(&classes, ce) { phpdbg_print_class_name(ce); @@ -447,7 +447,7 @@ PHPDBG_INFO(funcs) /* {{{ */ phpdbg_notice("User Functions (%d)", zend_hash_num_elements(&functions)); - if (HT_IS_PACKED(&functions)) { + if (HT_IS_INITIALIZED(&functions)) { ZEND_HASH_PACKED_FOREACH_PTR(&functions, zf) { zend_op_array *op_array = &zf->op_array;