From: Andrey Hristov Date: Fri, 18 Sep 2009 10:46:51 +0000 (+0000) Subject: Fix for bug#48754 mysql_close() crash php when no handle specified X-Git-Tag: php-5.4.0alpha1~191^2~2643 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b30dfcd47538aecae94adeb08be6a7d8a11d66c4;p=php Fix for bug#48754 mysql_close() crash php when no handle specified --- diff --git a/ext/mysql/php_mysql.c b/ext/mysql/php_mysql.c index 577433d363..f8780017db 100644 --- a/ext/mysql/php_mysql.c +++ b/ext/mysql/php_mysql.c @@ -1001,6 +1001,7 @@ PHP_FUNCTION(mysql_pconnect) Close a MySQL connection */ PHP_FUNCTION(mysql_close) { + int resource_id; zval *mysql_link=NULL; php_mysql_conn *mysql; @@ -1014,16 +1015,25 @@ PHP_FUNCTION(mysql_close) ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, NULL, MySG(default_link), "MySQL-Link", le_link, le_plink); } - if (mysql_link) { /* explicit resource number */ - PHPMY_UNBUFFERED_QUERY_CHECK(); - zend_list_delete(Z_RESVAL_P(mysql_link)); + resource_id = mysql_link ? Z_RESVAL_P(mysql_link) : MySG(default_link); + PHPMY_UNBUFFERED_QUERY_CHECK(); +#ifdef MYSQL_USE_MYSQLND + { + int tmp; + if ((mysql = zend_list_find(resource_id, &tmp)) && tmp == le_plink) { + mysqlnd_end_psession(mysql->conn); + } } +#endif + zend_list_delete(resource_id); if (!mysql_link || (mysql_link && Z_RESVAL_P(mysql_link)==MySG(default_link))) { - PHPMY_UNBUFFERED_QUERY_CHECK(); - zend_list_delete(MySG(default_link)); MySG(default_link) = -1; + if (mysql_link) { + /* on an explicit close of the default connection it had a refcount of 2 so we need one more call */ + zend_list_delete(resource_id); + } } RETURN_TRUE; diff --git a/ext/mysql/tests/bug48754.phpt b/ext/mysql/tests/bug48754.phpt new file mode 100644 index 0000000000..fb322f4615 --- /dev/null +++ b/ext/mysql/tests/bug48754.phpt @@ -0,0 +1,92 @@ +--TEST-- +Bug #48754 (mysql_close() crash php when no handle specified) +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Explicit connection on close +Expect same thread id for $link and default conn: bool(true) +resource(%d) of type (mysql link) +resource(%d) of type (Unknown) + +Warning: mysql_close(): no MySQL-Link resource supplied in %s on line %d + +Closing default link +Expect same thread id for $link and default conn but not the previous: bool(true) +resource(%d) of type (mysql link) +resource(%d) of type (mysql link) +resource(%d) of type (Unknown) + +Explicit resource and pconnect +resource(%d) of type (mysql link persistent) +resource(%d) of type (Unknown) + +Warning: mysql_close(): no MySQL-Link resource supplied in %s on line %d + +Default link and pconnect +resource(%d) of type (mysql link persistent) +resource(%d) of type (mysql link persistent) +resource(%d) of type (Unknown)