Fixed handshake response charset. (#13470)

The character set ID included in the handshake data at the time of connection
actually only includes the lower 8 bits of the ID, so if  try to use this to specify
a character set, the corresponding character set may not exist.

In case of an invalid character set, the default character set is now used
without an error.

Fixes #13452
Closes #13470
This commit is contained in:
Saki Takamachi 2024-03-04 21:51:02 +09:00
parent bfacc4146f
commit e9c5f0504c
No known key found for this signature in database
GPG key ID: E4A36F6D37931A8B
2 changed files with 10 additions and 7 deletions

View file

@ -19,6 +19,9 @@
#ifndef MYSQLND_CHARSET_H
#define MYSQLND_CHARSET_H
#define MYSQLND_UTF8_MB3_DEFAULT_ID 33
#define MYSQLND_UTF8_MB4_DEFAULT_ID 45
PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const charset, char * newstr,
const char * escapestr, const size_t escapestr_len);

View file

@ -22,6 +22,7 @@
#include "mysqlnd_auth.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_charset.h"
/* {{{ mysqlnd_command::set_option */
@ -613,13 +614,12 @@ MYSQLND_METHOD(mysqlnd_command, handshake)(MYSQLND_CONN_DATA * const conn, const
conn->protocol_version = greet_packet.protocol_version;
conn->server_version = mnd_pestrdup(greet_packet.server_version, conn->persistent);
conn->greet_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
if (!conn->greet_charset) {
char * msg;
mnd_sprintf(&msg, 0, "Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet.charset_no);
SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, msg);
mnd_sprintf_free(msg);
goto err;
const MYSQLND_CHARSET *read_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
if (!read_charset) {
greet_packet.charset_no = conn->m->get_server_version(conn) >= 50500 ? MYSQLND_UTF8_MB4_DEFAULT_ID : MYSQLND_UTF8_MB3_DEFAULT_ID;
conn->greet_charset = mysqlnd_find_charset_nr(greet_packet.charset_no);
} else {
conn->greet_charset = read_charset;
}
conn->server_capabilities = greet_packet.server_capabilities;