Skip to content

mysqli_set_charset now throws an mysqli_sql_exception when incorrect charset is provided #6142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/mysqli/mysqli_nonapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,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;
Expand Down
16 changes: 16 additions & 0 deletions ext/mysqli/tests/mysqli_set_charset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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->getMessage() . "\n";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it but it is difficult to keep the indentation consistent when half of the file is using tabs and the other half is using 4 spaces. My IDE ignores this differences and the file doesn't follow any formatting rules. If I execute CS-fixer it actually fixes formatting but I believe I should leave the formatting as it is. If you want me to execute CS-fixer when updating test cases I can do that, but then I would probably require the CS config file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to the tabs in the SKIPIF section? I've opened #6161 to fix that.


try {
$link->set_charset('ucs2');
} catch (\mysqli_sql_exception $exception) {
echo $exception->getMessage() . "\n";
}

mysqli_close($link);

try {
Expand All @@ -117,5 +131,7 @@ if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STOR
require_once("clean_table.inc");
?>
--EXPECT--
Invalid character set was provided
Variable 'character_set_client' can't be set to the value of 'ucs2'
mysqli object is already closed
done!
7 changes: 2 additions & 5 deletions ext/mysqlnd/mysqlnd_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,18 +1152,15 @@ 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);
}

if (PASS == conn->m->local_tx_start(conn, this_func)) {
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;
Expand Down