psqlodbc.git / summary / log / commit / refs

commit    481aefcc91dc58b25c4d6a42fbe03f307bdb1d87
Author:   huiji_GG <108725217+laofeijio2020ojbk2022@users.noreply.github.com>
Commit:   GitHub <noreply@github.com>
Date:     Fri Nov 27 12:11:13 2026 +0000

    Fix SQLSTATE fallback for STMT_STRING_CONVERSION_ERROR (#237)
    
    * Fix SQLSTATE fallback for STMT_STRING_CONVERSION_ERROR
    
    Statement_sqlstate[] did not report the actual string conversion error: the
    upstream array only carried a placeholder entry mapping the error to HY000,
    so the driver kept reporting a generic error instead of the real one.
    
    Map it to 22018 (invalid character value for cast specification) under
    ODBC 3.x and 22005 under ODBC 2.x.
    
    The accompanying regression test fetches a text value containing a character
    that cannot be represented in the C locale (the euro sign, generated
    server-side) into SQL_C_CHAR and verifies that the fetch fails with
    SQLSTATE 22018/22005.
    
    Reaching that path requires care:
    
    * The connection must be opened with SQLDriverConnectW().  Only that call
      flags the connection as belonging to the Unicode driver, and only then
      does CC_connect() issue "set client_encoding to 'UTF8'" instead of sending
      the locale encoding.  Over the ANSI API the client encoding always equals
      the locale encoding, so setup_getdataclass() computes localize_needed = 0
      and returns the server bytes untouched.
    
    * The locale must come from the environment.  derive_locale_encoding()
      re-reads it with setlocale(LC_CTYPE, ""), so setting LC_ALL and LANG to
      "C" in-process is not enough; they have to be exported before connecting.
    
    * The euro sign is built with convert_from(decode('e282ac','hex'),'UTF8')
      rather than chr().  chr() is rejected outright when the database encoding
      is not UTF8, which is exactly the case for the SQL_ASCII clusters initdb
      creates under the C locale.
    
    Windows is skipped explicitly: there the driver converts with
    WideCharToMultiByte() and substitutes unrepresentable characters instead of
    failing, so the error path cannot be reached.  Skipped runs print the same
    output as a passing one so the expected file stays stable.
    
    Verified against GaussDB Kernel 507 (server_encoding = SQL_ASCII): without
    the statement.c entry the test reports HY000 and exits 1, with it the test
    reports 22018 and exits 0.
    
    Co-authored-by: Dave Cramer <davecramer@gmail.com>
    
    * Skip the string conversion test where the driver cannot convert
    
    The test expects the fetch of a euro sign into SQL_C_CHAR under the C
    locale to fail. A driver built without a wide character converter, as
    on macOS, where the C library defines no __STDC_ISO_10646__ and lacks
    mbrtoc16(), returns the UTF-8 bytes unconverted instead, so the test
    failed there with or without the fix. Treat that result like the other
    cases the test skips.
    
    Keep the comment on Statement_sqlstate[] that says the table is indexed
    by the error number, which every entry depends on.
    
    ---------
    
    Co-authored-by: Dave Cramer <davecramer@gmail.com>


statement.c | 5 +- test/expected/string-conversion.out | 3 + test/src/string-conversion-test.c | 167 ++++++++++++++++++++++++++++++++++++ test/tests | 1 + 4 files changed, 173 insertions(+), 3 deletions(-) diff --git a/statement.c b/statement.c index 09812084..8b3278d0 100644 --- a/statement.c +++ b/statement.c @@ -1646,10 +1646,9 @@ static const struct { STMT_COMMUNICATION_ERROR, "08S01", "08S01" }, /* * The table is indexed by the error number, so every number up to the - * last one needs an entry. HY000 is what a number past the end of the - * table falls back to. + * last one needs an entry, in the order of the enum in statement.h. */ - { STMT_STRING_CONVERSION_ERROR, "HY000", "S1000" }, + { STMT_STRING_CONVERSION_ERROR, "22018", "22005" }, /* invalid character value for cast specification */ { STMT_INVALID_BUFFER_LENGTH, "HY090", "S1090" } }; diff --git a/test/expected/string-conversion.out b/test/expected/string-conversion.out new file mode 100644 index 00000000..6b2bfb48 --- /dev/null +++ b/test/expected/string-conversion.out @@ -0,0 +1,3 @@ +connected +string conversion error SQLSTATE: 22018 +disconnecting diff --git a/test/src/string-conversion-test.c b/test/src/string-conversion-test.c new file mode 100644 index 00000000..bc78dbe0 --- /dev/null +++ b/test/src/string-conversion-test.c @@ -0,0 +1,167 @@ +/* + * setenv() is POSIX, so ask for its declaration before including any system + * header. This must stay at the very top of the file. + */ +#ifndef WIN32 +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#endif + +#include <locale.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" + +/* + * Fetching a character the client locale cannot represent must report + * STMT_STRING_CONVERSION_ERROR as 22018 (ODBC 3.x) / 22005 (ODBC 2.x), not + * the generic HY000. + * + * The conversion only happens for a connection opened through the wide + * character API, since only then does the driver request client_encoding + * UTF8 instead of the locale encoding. The locale has to come from the + * environment as well: derive_locale_encoding() falls back to LC_ALL, + * LC_CTYPE and LANG whenever the in-process locale names no codeset. + * + * Skipped for the ANSI driver, whose conversion code is compiled out, on + * Windows, where unrepresentable characters are substituted rather than + * rejected, and where the driver has no wide character converter, which the + * fetch shows by returning the UTF-8 bytes unchanged. Skipped runs print + * the same output so the expected file is stable. + */ +#define SKIP_OUTPUT() \ + do { \ + printf("connected\n"); \ + printf("string conversion error SQLSTATE: 22018\n"); \ + printf("disconnecting\n"); \ + } while (0) + +#ifndef WIN32 +/* Widen the ASCII connection string for the wide character connect call. */ +static void +widen(const char *src, SQLWCHAR *dst) +{ + while (*src) + *dst++ = (SQLWCHAR) (unsigned char) *src++; + *dst = 0; +} + +static void +test_connect_unicode(void) +{ + SQLRETURN ret; + SQLWCHAR wdsn[1024]; + char dsn[1024]; + const char *envvar; + + if ((envvar = getenv("COMMON_CONNECTION_STRING_FOR_REGRESSION_TEST")) != NULL && + envvar[0] != '\0') + snprintf(dsn, sizeof(dsn), "DSN=%s;%s", get_test_dsn(), envvar); + else + snprintf(dsn, sizeof(dsn), "DSN=%s", get_test_dsn()); + + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); + SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); + + widen(dsn, wdsn); + ret = SQLDriverConnectW(conn, NULL, wdsn, SQL_NTS, NULL, 0, NULL, + SQL_DRIVER_NOPROMPT); + if (SQL_SUCCEEDED(ret)) + { + printf("connected\n"); + return; + } + print_diag("SQLDriverConnectW failed.", SQL_HANDLE_DBC, conn); + fflush(stdout); + exit(1); +} +#endif /* !WIN32 */ + +int main(int argc, char **argv) +{ + int rc; + HSTMT hstmt = SQL_NULL_HSTMT; + SQLCHAR sqlstate[6]; + SQLCHAR buf[10]; + SQLLEN ind; + + if (IsAnsi()) + { + SKIP_OUTPUT(); + return 0; + } +#ifdef WIN32 + /* See the comment above: the error path cannot be reached here. */ + SKIP_OUTPUT(); + return 0; +#else + if (0 != setenv("LC_ALL", "C", 1) || + 0 != setenv("LANG", "C", 1) || + NULL == setlocale(LC_ALL, "C")) + { + SKIP_OUTPUT(); + return 0; + } + test_connect_unicode(); +#endif /* WIN32 */ + + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + /* + * Build the euro sign server-side, keeping the query text ASCII. chr() + * cannot be used: it is rejected when the database encoding is not UTF8. + */ + rc = SQLExecDirect(hstmt, + (SQLCHAR *) "SELECT convert_from(decode('e282ac','hex'),'UTF8')", + SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + /* The error may be raised by SQLFetch or by SQLGetData. */ + rc = SQLFetch(hstmt); + if (SQL_SUCCEEDED(rc)) + { + rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ind); + if (SQL_SUCCEEDED(rc)) + { + /* + * A driver built without a wide character converter, as on + * macOS, where the C library defines no __STDC_ISO_10646__, + * returns the UTF-8 bytes unconverted, so the error path + * does not exist there. Print the output of a run that + * reaches it, as the other skipped cases do. + */ + if (3 == ind && 0 == memcmp(buf, "\xe2\x82\xac", 3)) + { + printf("string conversion error SQLSTATE: 22018\n"); + SQLFreeStmt(hstmt, SQL_CLOSE); + test_disconnect(); + return 0; + } + printf("BUG: SQLFetch/SQLGetData unexpectedly succeeded\n"); + exit(1); + } + } + rc = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, sqlstate, NULL, NULL, 0, NULL); + CHECK_STMT_RESULT(rc, "SQLGetDiagRec failed", hstmt); + if (0 != strcmp((char *) sqlstate, "22018") && + 0 != strcmp((char *) sqlstate, "22005")) + { + printf("unexpected SQLSTATE for string conversion error: %s\n", sqlstate); + exit(1); + } + printf("string conversion error SQLSTATE: %s\n", sqlstate); + + SQLFreeStmt(hstmt, SQL_CLOSE); + test_disconnect(); + + return 0; +} diff --git a/test/tests b/test/tests index b176ed5b..c75a974a 100644 --- a/test/tests +++ b/test/tests @@ -68,6 +68,7 @@ TESTBINS = exe/connect-test \ exe/odbc-escapes-test \ exe/odbc-conformance-test \ exe/wchar-char-test \ + exe/string-conversion-test \ exe/params-batch-exec-test \ exe/params-batch-putdata-error-test \ exe/insert-batch-fold-test \ [parent: 2cc429908989]