mirror of
https://github.com/php/php-src.git
synced 2025-08-20 01:14:28 +02:00
Fix for bug#48754 mysql_close() crash php when no handle specified
This commit is contained in:
parent
4ac04d8936
commit
b30dfcd475
2 changed files with 107 additions and 5 deletions
|
@ -1001,6 +1001,7 @@ PHP_FUNCTION(mysql_pconnect)
|
||||||
Close a MySQL connection */
|
Close a MySQL connection */
|
||||||
PHP_FUNCTION(mysql_close)
|
PHP_FUNCTION(mysql_close)
|
||||||
{
|
{
|
||||||
|
int resource_id;
|
||||||
zval *mysql_link=NULL;
|
zval *mysql_link=NULL;
|
||||||
php_mysql_conn *mysql;
|
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);
|
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, NULL, MySG(default_link), "MySQL-Link", le_link, le_plink);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mysql_link) { /* explicit resource number */
|
resource_id = mysql_link ? Z_RESVAL_P(mysql_link) : MySG(default_link);
|
||||||
PHPMY_UNBUFFERED_QUERY_CHECK();
|
PHPMY_UNBUFFERED_QUERY_CHECK();
|
||||||
zend_list_delete(Z_RESVAL_P(mysql_link));
|
#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
|
if (!mysql_link
|
||||||
|| (mysql_link && Z_RESVAL_P(mysql_link)==MySG(default_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;
|
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;
|
RETURN_TRUE;
|
||||||
|
|
92
ext/mysql/tests/bug48754.phpt
Normal file
92
ext/mysql/tests/bug48754.phpt
Normal 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)
|
Loading…
Add table
Add a link
Reference in a new issue