Bug #111217 mysql/mysqldump/... --default-character-set=<user-defined charset> is impossible
Submitted: 31 May 2023 11:07 Modified: 1 Jun 2023 1:32
Reporter: Rungong An Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S3 (Non-critical)
Version: OS:Any
Assigned to: CPU Architecture:Any

[31 May 2023 11:07] Rungong An
Description:
The document in https://dev.mysql.com/doc/refman/5.7/en/adding-character-set.html says that:

  You must assign a unique ID number to each collation. The range of IDs from 1024 to 2047 is reserved for user-defined collations. 

However, when just using mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset) before connection,the client side code in sql-common/client.c does not allow me to define a collation id > 255:

static char *mysql_fill_packet_header(MYSQL *mysql, char *buff,
                                      size_t buff_size MY_ATTRIBUTE((unused))) {
  NET *net = &mysql->net;
  char *end;
  uchar *buff_p = (uchar *)buff;
  (void)buff_size; /* avoid warnings */

  if (mysql->client_flag & CLIENT_PROTOCOL_41) {
    /* 4.1 server and 4.1 client has a 32 byte option flag */
    assert(buff_size >= 32);

    int4store(buff_p, mysql->client_flag);
    int4store(buff_p + 4, net->max_packet_size);
    buff[8] = (char)mysql->charset->number;
    memset(buff + 9, 0, 32 - 9);
    end = buff + 32;
  } else {
    assert(buff_size >= 5);
    assert(mysql->client_flag <= UINT_MAX16);

    int2store(buff_p, (uint16)mysql->client_flag);
    int3store(buff_p + 2, net->max_packet_size);
    end = buff + 5;
  }
  return end;
}

According to the code  buff[8] = (char)mysql->charset->number, the collation number must be <= 255 if I want to use the client option --default-character-set=<user-defined charset>.

So I consider adding mysql_set_character_set(&mysql, default_charset) after the connection is established a suitable fix.

How to repeat:
None

Suggested fix:
After the connection is established successfully(mysql_real_connect returns not NULL), add these code:

  MY_CHARSET_INFO cs;
  mysql_get_character_set_info(&mysql, &cs);
  if (cs.number >= 1024 && cs.number <= 2047) {
      mysql_set_character_set(&mysql, default_charset);
  }
[31 May 2023 13:13] MySQL Verification Team
Please do not submit the same bug more than once. An existing bug report already describes this very problem. Even if you feel that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments to the original bug instead.

Thank you for your interest in MySQL.

The original bug is:

https://bugs.mysql.com/bug.php?id=111181
[1 Jun 2023 1:32] Rungong An
OK.