Skip to content

Fix #80575: Casting mysqli to array produces null on every field #6587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: PHP-7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions ext/mysqli/mysqli.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,20 @@ static int mysqli_object_has_property(zval *object, zval *member, int has_set_ex
return ret;
} /* }}} */

HashTable *mysqli_object_get_debug_info(zval *object, int *is_temp)
static HashTable *mysqli_object_get_properties_for(zval *object, zend_prop_purpose purpose) /* {{{ */
{
mysqli_object *obj = Z_MYSQLI_P(object);
HashTable *retval, *props = obj->prop_handler;
mysqli_prop_handler *entry;

switch (purpose) {
case ZEND_PROP_PURPOSE_DEBUG:
case ZEND_PROP_PURPOSE_ARRAY_CAST:
break;
default:
return zend_std_get_properties_for(object, purpose);
}
Copy link
Member

@nikic nikic Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, what I had in mind is implementing only __debugInfo and dropping the internal get_debug_info/get_properties_for handler


retval = zend_new_array(zend_hash_num_elements(props) + 1);

ZEND_HASH_FOREACH_PTR(props, entry) {
Expand All @@ -447,7 +455,6 @@ HashTable *mysqli_object_get_debug_info(zval *object, int *is_temp)
}
} ZEND_HASH_FOREACH_END();

*is_temp = 1;
return retval;
}

Expand Down Expand Up @@ -585,7 +592,8 @@ PHP_MINIT_FUNCTION(mysqli)
mysqli_object_handlers.read_property = mysqli_read_property;
mysqli_object_handlers.write_property = mysqli_write_property;
mysqli_object_handlers.has_property = mysqli_object_has_property;
mysqli_object_handlers.get_debug_info = mysqli_object_get_debug_info;
mysqli_object_handlers.get_properties_for = mysqli_object_get_properties_for;

memcpy(&mysqli_object_driver_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
mysqli_object_driver_handlers.free_obj = mysqli_driver_free_storage;
memcpy(&mysqli_object_link_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
Expand Down
1 change: 1 addition & 0 deletions ext/mysqli/mysqli_fe.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ const zend_function_entry mysqli_link_methods[] = {
PHP_FALIAS(thread_safe, mysqli_thread_safe, arginfo_mysqli_no_params)
PHP_FALIAS(use_result, mysqli_use_result, arginfo_mysqli_no_params)
PHP_FALIAS(refresh,mysqli_refresh, arginfo_class_mysqli_refresh)
PHP_ME(mysqli, __debugInfo, arginfo_mysqli_no_params, 0)
PHP_FE_END
};
/* }}} */
Expand Down
1 change: 1 addition & 0 deletions ext/mysqli/mysqli_fe.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ PHP_FUNCTION(mysqli_thread_id);
PHP_FUNCTION(mysqli_thread_safe);
PHP_FUNCTION(mysqli_use_result);
PHP_FUNCTION(mysqli_warning_count);
PHP_METHOD(mysqli, __debugInfo);

PHP_FUNCTION(mysqli_stmt_construct);
PHP_FUNCTION(mysqli_result_construct);
Expand Down
12 changes: 12 additions & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,4 +1280,16 @@ PHP_FUNCTION(mysqli_get_links_stats)
add_assoc_long_ex(return_value, "active_plinks", sizeof("active_plinks") - 1, MyG(num_active_persistent));
add_assoc_long_ex(return_value, "cached_plinks", sizeof("cached_plinks") - 1, MyG(num_inactive_persistent));
}

PHP_METHOD(mysqli, __debugInfo)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}

zval *self = ZEND_THIS;
zend_object *obj = Z_OBJ_P(self);
HashTable *ht = obj->handlers->get_properties_for(self, ZEND_PROP_PURPOSE_ARRAY_CAST);
RETURN_ARR(ht);
}
/* }}} */
18 changes: 18 additions & 0 deletions ext/mysqli/tests/bug80575.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Bug #80575 (Casting mysqli to array produces null on every field)
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifemb.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
require_once('connect.inc');
$link = new mysqli($host, $user, $passwd, $db, $port, $socket);
var_dump(((array) $link)["affected_rows"]);
var_dump($link->__debugInfo()["affected_rows"]);
?>
--EXPECT--
int(0)
int(0)