]> granicus.if.org Git - php/commitdiff
mysqli_set_charset now throws an mysqli_sql_exception when incorrect charset is provided
authorDharman <tekiela246@gmail.com>
Wed, 16 Sep 2020 13:20:03 +0000 (14:20 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 18 Sep 2020 12:57:59 +0000 (14:57 +0200)
Closes GH-6142.

ext/mysqli/mysqli_nonapi.c
ext/mysqli/tests/mysqli_set_charset.phpt
ext/mysqlnd/mysqlnd_connection.c

index 03e975462e6ffb7d07e5564a1ff560d2a4a63518..b3aae572e93d4c8c9d7fa4d80d1f3785a710e1f1 100644 (file)
@@ -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;
index 30bbb1410f6ca865d2aabadcf060a8a5ca8843d0..e9373a44dbdfefc184d605eec5d642b27d39905b 100644 (file)
@@ -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
 <?php
     require_once("clean_table.inc");
 ?>
---EXPECT--
+--EXPECTF--
+Exception: %s
+Exception: Variable 'character_set_client' can't be set to the value of 'ucs2'
 mysqli object is already closed
 done!
index 252c05f676dd27eeab49b903ff8f4012130e7462..9f34c0407004b70e56bf1f8a91004da4cefecb19 100644 (file)
@@ -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;