Uploaded image for project: 'MariaDB Connector/C'
  1. MariaDB Connector/C
  2. CONC-342

mysql_options() with MYSQL_SET_CHARSET_NAME is broken

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 3.0.5
    • Fix Version/s: N/A
    • Labels:
      None

      Description

      MariaDB C library thinks that connection character was set to utf8mb4, but in reality not. As connection charset affects functions like mysql_real_escape_string() it is a problem.

      It happen only when connection charset is configured via mysql_options() call with MYSQL_SET_CHARSET_NAME prior to calling mysql_real_connect().

      When charset is configured via function mysql_set_character_set() after mysql_real_connect() everything is OK.

      In attachment is example which demonstrate this problem. MariaDB C library thinks that charset is utf8mb4 (and based on this fact is doing escaping and other operations). But in reality only 3byte utf8 charset is used (retrieved via SELECT query), which e.g. cannot transfer new 8byte emoji.

      $ gcc -o test-utf8 -W -Wall -O2 test-utf8.c `mysql_config --cflags --libs`
      $ ./test-utf8 127.0.0.1 3306 pali pali
      SQL @@character_set_client: utf8
      SQL @@character_set_results: utf8
      SQL @@character_set_connection: utf8
      SQL @@character_set_server: utf8
      SQL @@character_set_database: utf8
      SQL @@collation_connection: utf8_unicode_ci
      SQL @@collation_database: utf8_unicode_ci
      character set information:
      character set+collation number: 45
      character set name: utf8mb4_general_ci
      collation name: utf8mb4
      comment: (null)
      directory: (null)
      multi byte character min. length: 1
      multi byte character max. length: 4
      

      If you add mysql_set_character_set(mysql, "utf8mb4"); after mysql_real_connect() then output is correct:

      SQL @@character_set_client: utf8mb4
      SQL @@character_set_results: utf8mb4
      SQL @@character_set_connection: utf8mb4
      SQL @@character_set_server: utf8
      SQL @@character_set_database: utf8
      SQL @@collation_connection: utf8mb4_general_ci
      SQL @@collation_database: utf8_unicode_ci
      character set information:
      character set+collation number: 45
      character set name: utf8mb4_general_ci
      collation name: utf8mb4
      comment: (null)
      directory: (null)
      multi byte character min. length: 1
      multi byte character max. length: 4
      

      Just use last MariaDB Connnector/C 3.0.5 and try to connect to MariaDB server 5.5.52 (version which is available on the CentOS 7.3).

      Important part is to configure server with:

      collation_server=utf8_unicode_ci
      character-set-server=utf8
      skip-character-set-client-handshake
      

      (Or latin1 or whatever different from utf8mb4)

      It looks like that server option skip-character-set-client-handshake cause this problem and makes client defective. But client library should not be affected by any such server configuration.

      This problem was discovered while developing Perl DBI driver DBD::MariaDB: https://github.com/gooddata/DBD-MariaDB. As a workaround DBD::MariaDB always change charset after mysql_real_connect() was called.

        Attachments

          Activity

            People

            Assignee:
            georg Georg Richter
            Reporter:
            pali Pali
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: