Skip to content

Commit 0522ae6

Browse files
committed
ext/mysqli: mysql_thread_id/mysqli_kill internal changes proposal.
- Both mysql_thread_id and mysql_kill despite dealing possibly with 64 bits values operates in reality with 32 bits internally, leading to truncation. They are both deprecated and passing commands instead is advised.
1 parent 7d551a8 commit 0522ae6

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

ext/mysqli/mysqli_api.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,9 @@ PHP_FUNCTION(mysqli_insert_id)
10401040
/* {{{ Kill a mysql process on the server */
10411041
PHP_FUNCTION(mysqli_kill)
10421042
{
1043-
MY_MYSQL *mysql;
1044-
zval *mysql_link;
1045-
zend_long processid;
1043+
MY_MYSQL *mysql;
1044+
zval *mysql_link;
1045+
zend_long processid;
10461046

10471047
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &processid) == FAILURE) {
10481048
RETURN_THROWS();
@@ -1055,7 +1055,11 @@ PHP_FUNCTION(mysqli_kill)
10551055

10561056
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
10571057

1058-
if (mysql_kill(mysql->mysql, processid)) {
1058+
char query[64];
1059+
snprintf(query, sizeof(query), "KILL CONNECTION " ZEND_LONG_FMT, processid);
1060+
1061+
// 1317 is ER_QUERY_INTERRUPTED from server's side
1062+
if (mysql_real_query(mysql->mysql, query, strlen(query)) && mysql_errno(mysql->mysql) != 1317) {
10591063
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
10601064
RETURN_FALSE;
10611065
}
@@ -1981,14 +1985,36 @@ PHP_FUNCTION(mysqli_store_result)
19811985
PHP_FUNCTION(mysqli_thread_id)
19821986
{
19831987
MY_MYSQL *mysql;
1988+
MYSQL_RES *result;
1989+
MYSQL_ROW row;
19841990
zval *mysql_link;
1991+
zend_long processid;
19851992

19861993
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
19871994
RETURN_THROWS();
19881995
}
19891996
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
19901997

1991-
RETURN_LONG((zend_long) mysql_thread_id(mysql->mysql));
1998+
static const char *query = "SELECT CONNECTION_ID()";
1999+
2000+
if (mysql_real_query(mysql->mysql, query, strlen(query))) {
2001+
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
2002+
RETURN_LONG(-1);
2003+
}
2004+
2005+
result = mysql_store_result(mysql->mysql);
2006+
if (!result) {
2007+
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
2008+
RETURN_LONG(-1);
2009+
}
2010+
2011+
row = mysql_fetch_row(result);
2012+
processid = (zend_long)strtoll(row[0], NULL, 10);
2013+
2014+
efree(row);
2015+
mysql_free_result(result);
2016+
2017+
RETURN_LONG(processid);
19922018
}
19932019
/* }}} */
19942020

0 commit comments

Comments
 (0)