MFH:Fix for bug#48754 mysql_close() crash php when no handle specified

This commit is contained in:
Andrey Hristov 2009-09-18 10:49:31 +00:00
parent c2e66884e1
commit 8aae3baa45
3 changed files with 103 additions and 7 deletions

2
NEWS
View File

@ -139,6 +139,8 @@ PHP NEWS
gmail dot com, Pierre)
- Fixed bug #48762 (IPv6 address filter still rejects valid address). (Felipe)
- Fixed bug #48757 (ReflectionFunction::invoke() parameter issues). (Kalle)
- Fixed bug #48754 (mysql_close() crash php when no handle specified).
(Johannes, Andrey)
- Fixed bug #48746 (Unable to browse directories within Junction Points).
(Pierre, Kanwaljeet Singla)
- Fixed bug #48745 (mysqlnd: mysql_num_fields returns wrong column count for

View File

@ -971,6 +971,7 @@ PHP_FUNCTION(mysql_pconnect)
Close a MySQL connection */
PHP_FUNCTION(mysql_close)
{
int resource_id;
zval *mysql_link=NULL;
php_mysql_conn *mysql;
@ -984,24 +985,25 @@ PHP_FUNCTION(mysql_close)
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, NULL, MySG(default_link), "MySQL-Link", le_link, le_plink);
}
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(Z_RESVAL_P(mysql_link), &tmp)) && tmp == le_plink) {
if ((mysql = zend_list_find(resource_id, &tmp)) && tmp == le_plink) {
mysqlnd_end_psession(mysql->conn);
}
}
#endif
if (mysql_link) { /* explicit resource number */
PHPMY_UNBUFFERED_QUERY_CHECK();
zend_list_delete(Z_RESVAL_P(mysql_link));
}
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;

View File

@ -0,0 +1,92 @@
--TEST--
Bug #48754 (mysql_close() crash php when no handle specified)
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
require_once('connect.inc');
function my_mysql_pconnect($host, $user, $passwd, $db, $port, $socket) {
if ($socket)
$host = sprintf("%s:%s", $host, $socket);
else if ($port)
$host = sprintf("%s:%s", $host, $port);
if (!$link = mysql_pconnect($host, $user, $passwd, true)) {
printf("[000-a] Cannot connect using host '%s', user '%s', password '****', [%d] %s\n",
$host, $user, $passwd,
mysql_errno(), mysql_error());
return false;
}
return $link;
}
echo "Explicit connection on close\n";
$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket);
$link1_thread_id = mysql_thread_id($link);
$default1_thread_id = mysql_thread_id();
echo 'Expect same thread id for $link and default conn: ';
var_dump($link1_thread_id == $default1_thread_id);
var_dump($link);
mysql_close($link);
var_dump($link);
// we sohuld have no default link anymore
mysql_close();
echo "\nClosing default link\n";
$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket);
$link2_thread_id = mysql_thread_id($link);
$default2_thread_id = mysql_thread_id();
echo 'Expect same thread id for $link and default conn but not the previous: ';
var_dump($link1_thread_id == $default1_thread_id && $link1_thread_id != $link2_thread_id);
var_dump($link);
mysql_close();
var_dump($link);
mysql_close($link);
var_dump($link);
echo "\nExplicit resource and pconnect\n";
$link = my_mysql_pconnect($host, $user, $passwd, $db, $port, $socket);
var_dump($link);
mysql_close($link);
var_dump($link);
// we sohuld have no default link
mysql_close();
echo "\nDefault link and pconnect\n";
$link = my_mysql_pconnect($host, $user, $passwd, $db, $port, $socket);
var_dump($link);
mysql_close();
var_dump($link);
mysql_close($link);
var_dump($link);
?>
--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)