From 74d16999fc7cd5cf778547d796f5ac8e761b8518 Mon Sep 17 00:00:00 2001 From: Dharman Date: Wed, 16 Sep 2020 14:20:03 +0100 Subject: [PATCH] mysqli_set_charset now throws an mysqli_sql_exception when incorrect charset is provided Closes GH-6142. --- ext/mysqli/mysqli_nonapi.c | 1 + ext/mysqli/tests/mysqli_set_charset.phpt | 18 +++++++++++++++++- ext/mysqlnd/mysqlnd_connection.c | 7 ++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ext/mysqli/mysqli_nonapi.c b/ext/mysqli/mysqli_nonapi.c index 03e975462e..b3aae572e9 100644 --- a/ext/mysqli/mysqli_nonapi.c +++ b/ext/mysqli/mysqli_nonapi.c @@ -1040,6 +1040,7 @@ PHP_FUNCTION(mysqli_set_charset) MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID); if (mysql_set_character_set(mysql->mysql, cs_name)) { + MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql); RETURN_FALSE; } RETURN_TRUE; diff --git a/ext/mysqli/tests/mysqli_set_charset.phpt b/ext/mysqli/tests/mysqli_set_charset.phpt index 30bbb1410f..e9373a44db 100644 --- a/ext/mysqli/tests/mysqli_set_charset.phpt +++ b/ext/mysqli/tests/mysqli_set_charset.phpt @@ -101,6 +101,20 @@ if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STOR } mysqli_free_result($res); + // Make sure that set_charset throws an exception in exception mode + mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); + try { + $link->set_charset('invalid'); + } catch (\mysqli_sql_exception $exception) { + echo "Exception: " . $exception->getMessage() . "\n"; + } + + try { + $link->set_charset('ucs2'); + } catch (\mysqli_sql_exception $exception) { + echo "Exception: " . $exception->getMessage() . "\n"; + } + mysqli_close($link); try { @@ -115,6 +129,8 @@ if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STOR ---EXPECT-- +--EXPECTF-- +Exception: %s +Exception: Variable 'character_set_client' can't be set to the value of 'ucs2' mysqli object is already closed done! diff --git a/ext/mysqlnd/mysqlnd_connection.c b/ext/mysqlnd/mysqlnd_connection.c index 252c05f676..9f34c04070 100644 --- a/ext/mysqlnd/mysqlnd_connection.c +++ b/ext/mysqlnd/mysqlnd_connection.c @@ -1152,8 +1152,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, c DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname); if (!charset) { - SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, - "Invalid characterset or character set not supported"); + SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Invalid character set was provided"); DBG_RETURN(ret); } @@ -1161,9 +1160,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, c char * query; size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname); - if (FAIL == (ret = conn->m->query(conn, query, query_len))) { - php_error_docref(NULL, E_WARNING, "Error executing query"); - } else if (conn->error_info->error_no) { + if (FAIL == (ret = conn->m->query(conn, query, query_len)) || conn->error_info->error_no) { ret = FAIL; } else { conn->charset = charset; -- 2.40.0