Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WG6ww-0004WA-6F for pgsql-odbc@arkaria.postgresql.org; Wed, 19 Feb 2014 13:12:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WG6wv-00021a-Kf for pgsql-odbc@arkaria.postgresql.org; Wed, 19 Feb 2014 13:12:29 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WG6wu-00021U-GO for pgsql-odbc@postgresql.org; Wed, 19 Feb 2014 13:12:28 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WG6wo-0008My-Lo for pgsql-odbc@postgresql.org; Wed, 19 Feb 2014 13:12:26 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 191762B1BA; Wed, 19 Feb 2014 05:12:21 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 1584AB0163; Wed, 19 Feb 2014 05:12:21 -0800 (PST) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 78381120034; Wed, 19 Feb 2014 05:12:20 -0800 (PST) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA; Wed, 19 Feb 2014 05:12:18 -0800 (PST) Message-ID: <5304ADB1.6090306@vmware.com> Date: Wed, 19 Feb 2014 15:12:17 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: Hiroshi Inoue CC: =?UTF-8?B?TmlscyBHw7ZzY2hl?= , pgsql-odbc@postgresql.org Subject: Re: Null Characters in Strings, Version 9.3.1 References: <006a01cf28e4$7c425450$74c6fcf0$@de> <003201cf2ccb$c5d6ddd0$51849970$@de> <5303960F.3050706@vmware.com> <003b01cf2ce1$2e33bc80$8a9b3580$@de> <5303C2EA.7060109@vmware.com> <5303F655.8000200@tpf.co.jp> In-Reply-To: <5303F655.8000200@tpf.co.jp> Content-Type: multipart/mixed; boundary="------------080703000805080501030808" X-Pg-Spam-Score: -7.5 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------080703000805080501030808 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 02/19/2014 02:09 AM, Hiroshi Inoue wrote: > (2014/02/19 5:30), Heikki Linnakangas wrote: >> On 02/18/2014 09:39 PM, Nils Gösche wrote: >>> If I run this with version 9.2.1 of the driver (I am using 32 Bit >>> here), it will go to the else clause and print the value of the text >>> column. However, if I run this with the latest version, I get the >>> following output: >> >> Hmm. If you could pinpoint it to the exact commit that changed the >> behavior, that would help too. > > Maybe it's caused by the commit 3666c87c1440862bde2e6b8f43ee585deed70d31 > Author: Hiroshi Inoue > Date: Thu Aug 22 20:40:01 2013 +0900 > > When LF->CR+LF conversion causes an buffer truncation, supress the > conversion (in case of unicode). > > Nils, please try to turn off *LF <-> CR/LF conversion* option. > Anyway I can't think of the way to cure the problem other than putting > back the commit. Hmm, what was the point of that commit in the first place? I don't think it was a good idea. If your application needs CR+LF, it's not going to be happy if you sometimes don't do the conversion. Better to truncate. While looking into this, I found the conversion functions difficult to debug, because it's defined by the def_utf2ucs macro. It would be much more natural to have one regular function with an argument to indicate if it should do error-checking. It used to be a regular function, but commit ca1daf08f0eef73222abcd8aafe96b8e29afff91 turned it into a macro. Not sure why; premature optimization perhaps? I propose the attached patch. It turns the utf8_to_ucs2_lf function back into a regular function, and reverts the troublesome commit 3666c87c1440862bde2e6b8f43ee585deed70d31. It also adds a regression test case similar to Nils'. Barring objections, I'll commit this. - Heikki --------------080703000805080501030808 Content-Type: text/x-diff; name="lfconversion-fix+test.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="lfconversion-fix+test.patch" diff --git a/convert.c b/convert.c index 84cc1d8..2168994 100644 --- a/convert.c +++ b/convert.c @@ -1270,7 +1270,7 @@ inolog("2stime fr=%d\n", std_time.fr); #ifdef UNICODE_SUPPORT if (fCType == SQL_C_WCHAR) { - len = utf8_to_ucs2_lf(neut_str, SQL_NTS, lf_conv, NULL, 0); + len = utf8_to_ucs2_lf(neut_str, SQL_NTS, lf_conv, NULL, 0, FALSE); len *= WCLEN; changed = TRUE; } @@ -1330,7 +1330,7 @@ inolog("2stime fr=%d\n", std_time.fr); len = pg_bin2whex(pgdc->ttlbuf, (SQLWCHAR *) pgdc->ttlbuf, len); } else - utf8_to_ucs2_lf(neut_str, SQL_NTS, lf_conv, (SQLWCHAR *) pgdc->ttlbuf, len / WCLEN); + utf8_to_ucs2_lf(neut_str, SQL_NTS, lf_conv, (SQLWCHAR *) pgdc->ttlbuf, len / WCLEN, FALSE); } else #endif /* UNICODE_SUPPORT */ diff --git a/psqlodbc.h b/psqlodbc.h index f63f53c..f452f43 100644 --- a/psqlodbc.h +++ b/psqlodbc.h @@ -590,11 +590,9 @@ const pthread_mutexattr_t *getMutexAttr(void); #define WCLEN sizeof(SQLWCHAR) SQLULEN ucs2strlen(const SQLWCHAR *ucs2str); char *ucs2_to_utf8(const SQLWCHAR *ucs2str, SQLLEN ilen, SQLLEN *olen, BOOL tolower); -SQLULEN utf8_to_ucs2_lf0(const char * utf8str, SQLLEN ilen, BOOL lfconv, SQLWCHAR *ucs2str, SQLULEN buflen); -SQLULEN utf8_to_ucs2_lf1(const char * utf8str, SQLLEN ilen, BOOL lfconv, SQLWCHAR *ucs2str, SQLULEN buflen); +SQLULEN utf8_to_ucs2_lf(const char * utf8str, SQLLEN ilen, BOOL lfconv, SQLWCHAR *ucs2str, SQLULEN buflen, BOOL errcheck); int msgtowstr(const char *, const char *, int, LPWSTR, int); int wstrtomsg(const char *, const LPWSTR, int, char *, int); -#define utf8_to_ucs2_lf(utf8str, ilen, lfconv, ucs2str, buflen) utf8_to_ucs2_lf0(utf8str, ilen, lfconv, ucs2str, buflen) #define utf8_to_ucs2(utf8str, ilen, ucs2str, buflen) utf8_to_ucs2_lf0(utf8str, ilen, FALSE, ucs2str, buflen) #endif /* UNICODE_SUPPORT */ diff --git a/test/Makefile b/test/Makefile index cbeb134..a819095 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,7 @@ TESTS = connect stmthandles select commands multistmt getresult prepare \ params notice arraybinding insertreturning dataatexecution \ boolsaschar cvtnulldate alter quotes cursors positioned-update \ - catalogfunctions bindcol + catalogfunctions bindcol lfconversion TESTBINS = $(patsubst %,src/%-test, $(TESTS)) TESTSQLS = $(patsubst %,sql/%.sql, $(TESTS)) diff --git a/test/expected/lfconversion.out b/test/expected/lfconversion.out new file mode 100644 index 0000000..ca4776c --- /dev/null +++ b/test/expected/lfconversion.out @@ -0,0 +1,18 @@ +\! ./src/lfconversion-test +connected +reading to char buffer... +strlen 22, SQLGetData claims 22 + +reading to char buffer, with truncation... +strlen 9, SQLGetData claims 22 + +reading to SQLWCHAR buffer... +len 22 chars, SQLGetData claims 44 bytes + +reading to SQLWCHAR buffer, with truncation... +len 4 chars, SQLGetData claims 44 bytes + +reading to SQLWCHAR buffer, with LF->CR+LF conversion causing truncation... +len 20 chars, SQLGetData claims 44 bytes + +disconnecting diff --git a/test/src/lfconversion-test.c b/test/src/lfconversion-test.c new file mode 100644 index 0000000..20f4b7f --- /dev/null +++ b/test/src/lfconversion-test.c @@ -0,0 +1,111 @@ +#include +#include +#include + +#if 0 +#include +#endif + +#include "common.h" + +int main(int argc, char **argv) +{ + SQLRETURN rc; + SQLLEN sqlLen; + SQLLEN ccharlen; + SQLLEN wcharlen; + HSTMT hstmt = SQL_NULL_HSTMT; + char *sql; + int i; + char buf[1000]; + SQLWCHAR wbuf[1000]; + + /* Enable LF -> CR+LF conversion */ + test_connect_ext("CX=1"); + + rc = SQLAllocStmt(conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + /* + * Return several columns that all contain the same string, with newlines. + * We want to try getting the column contents with several different + * options, and the driver doesn't let you fetch the same column more than + * once. + */ + sql = "SELECT E'string\nwith\nnewlines', E'string\nwith\nnewlines', " + "E'string\nwith\nnewlines', E'string\nwith\nnewlines', " + "E'string\nwith\nnewlines'"; + rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + + rc = SQLFetch(hstmt); + CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); + + printf("reading to char buffer...\n"); + rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ccharlen); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + printf("strlen %d, SQLGetData claims %d\n\n", strlen(buf), ccharlen); + + printf("reading to char buffer, with truncation...\n"); + rc = SQLGetData(hstmt, 2, SQL_C_CHAR, buf, 10, &ccharlen); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + printf("strlen %d, SQLGetData claims %d\n\n", strlen(buf), ccharlen); + + printf("reading to SQLWCHAR buffer...\n"); + rc = SQLGetData(hstmt, 3, SQL_C_WCHAR, wbuf, sizeof(wbuf), &wcharlen); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + + /* On some platforms, SQLWCHAR != wchar_t, so we cannot use wcslen here */ + for (i = 0; i < sizeof(wbuf) && wbuf[i] != 0; i++); + printf("len %d chars, SQLGetData claims %d bytes\n\n", i, wcharlen); + + printf("reading to SQLWCHAR buffer, with truncation...\n"); + rc = SQLGetData(hstmt, 4, SQL_C_WCHAR, wbuf, 10, &wcharlen); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + + for (i = 0; i < sizeof(wbuf) && wbuf[i] != 0; i++); + printf("len %d chars, SQLGetData claims %d bytes\n\n", i, wcharlen); + + /* + * Read into a buffer that's slightly too small, so that it would fit + * if it wasn't for the LF->CR+LF conversion. + */ + printf("reading to SQLWCHAR buffer, with LF->CR+LF conversion causing truncation...\n"); + rc = SQLGetData(hstmt, 5, SQL_C_WCHAR, wbuf, 42, &wcharlen); + CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); + + for (i = 0; i < sizeof(wbuf) && wbuf[i] != 0; i++); + printf("len %d chars, SQLGetData claims %d bytes\n\n", i, wcharlen); + + /* + * Print out the string, but on Unix we have to convert it to UTF-8 first. + * On Windows we could just use wprintf. + */ +#if 0 + { + iconv_t cd = iconv_open("UTF-8", "UCS-2"); + char utf8buf[1000]; + size_t l; + size_t inbytes = wcharlen; + size_t outbytes = sizeof(utf8buf); + char *obuf = utf8buf; + char *ibuf = (char *) wbuf; + + l = iconv(cd, &ibuf, &inbytes, &obuf, &outbytes); + *obuf = 0; + printf("inremains %d outremains %d l: %d s: %s\n", inbytes, outbytes, l, utf8buf); + } +#endif + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + + /* Clean up */ + test_disconnect(); + + return 0; +} diff --git a/win_unicode.c b/win_unicode.c index 145a332..7b13e9e 100644 --- a/win_unicode.c +++ b/win_unicode.c @@ -164,154 +164,159 @@ char *ucs2_to_utf8(const SQLWCHAR *ucs2str, SQLLEN ilen, SQLLEN *olen, BOOL lowe #define byte4_m32 0x0f #define byte4_m4 0x3f -#define def_utf2ucs(errcheck) \ -SQLULEN utf8_to_ucs2_lf##errcheck(const char *utf8str, SQLLEN ilen, BOOL lfconv, SQLWCHAR *ucs2str, SQLULEN bufcount) \ -{ \ - int i, lfcount = 0; \ - SQLULEN rtn, ocount, wcode; \ - const UCHAR *str; \ -\ -/*mylog("utf8_to_ucs2 ilen=%d bufcount=%d", ilen, bufcount);*/ \ - if (!utf8str) \ - return 0; \ -/*mylog(" string=%s\n", utf8str);*/ \ - if (little_endian < 0) \ - { \ - int crt = 1; \ - little_endian = (0 != ((char *) &crt)[0]); \ - } \ - if (!bufcount) \ - ucs2str = NULL; \ - else if (!ucs2str) \ - bufcount = 0; \ - if (ilen < 0) \ - ilen = strlen(utf8str); \ - for (i = 0, ocount = 0, str = utf8str; i < ilen && *str;) \ - { \ - /* if (iswascii(*str)) */ \ - if (isascii(*str)) \ - { \ - if (lfconv && PG_LINEFEED == *str && \ - (i == 0 || PG_CARRIAGE_RETURN != str[-1])) \ - { \ - if (ocount < bufcount) \ - ucs2str[ocount] = PG_CARRIAGE_RETURN; \ - ocount++; \ - lfcount++; \ - } \ - if (ocount < bufcount) \ - ucs2str[ocount] = *str; \ - ocount++; \ - i++; \ - str++; \ - } \ - else if (0xf8 == (*str & 0xf8)) /* more than 5 byte code */ \ - { \ - ocount = (SQLULEN) -1; \ - goto cleanup; \ - } \ - else if (0xf0 == (*str & 0xf8)) /* 4 byte code */ \ - { \ - if (01 == 0##errcheck) \ - { \ - if (i + 4 > ilen || \ - 0 == (str[1] & 0x80) || \ - 0 == (str[2] & 0x80) || \ - 0 == (str[3] & 0x80)) \ - { \ - ocount = (SQLULEN) -1; \ - goto cleanup; \ - } \ - } \ - if (ocount < bufcount) \ - { \ - wcode = (surrog1_bits | \ - ((((UInt4) *str) & byte4_m1) << 8) | \ - ((((UInt4) str[1]) & byte4_m2) << 2) | \ - ((((UInt4) str[2]) & byte4_m31) >> 4)) \ - - surrogate_adjust; \ - ucs2str[ocount] = (SQLWCHAR) wcode; \ - } \ - ocount++; \ - if (ocount < bufcount) \ - { \ - wcode = surrog2_bits | \ - ((((UInt4) str[2]) & byte4_m32) << 6) | \ - (((UInt4) str[3]) & byte4_m4); \ - ucs2str[ocount] = (SQLWCHAR) wcode; \ - } \ - ocount++; \ - i += 4; \ - str += 4; \ - } \ - else if (0xe0 == (*str & 0xf0)) /* 3 byte code */ \ - { \ - if (01 == 0##errcheck) \ - { \ - if (i + 3 > ilen || \ - 0 == (str[1] & 0x80) || \ - 0 == (str[2] & 0x80)) \ - { \ - ocount = (SQLULEN) -1; \ - goto cleanup; \ - } \ - } \ - if (ocount < bufcount) \ - { \ - wcode = ((((UInt4) *str) & byte3_m1) << 12) | \ - ((((UInt4) str[1]) & byte3_m2) << 6) | \ - (((UInt4) str[2]) & byte3_m3); \ - ucs2str[ocount] = (SQLWCHAR) wcode; \ - } \ - ocount++; \ - i += 3; \ - str += 3; \ - } \ - else if (0xc0 == (*str & 0xe0)) /* 2 byte code */ \ - { \ - if (01 == 0##errcheck) \ - { \ - if (i + 2 > ilen || \ - 0 == (str[1] & 0x80)) \ - { \ - ocount = (SQLULEN) -1; \ - goto cleanup; \ - } \ - } \ - if (ocount < bufcount) \ - { \ - wcode = ((((UInt4) *str) & byte2_m1) << 6) | \ - (((UInt4) str[1]) & byte2_m2); \ - ucs2str[ocount] = (SQLWCHAR) wcode; \ - } \ - ocount++; \ - i += 2; \ - str += 2; \ - } \ - else \ - { \ - ocount = (SQLULEN) -1; \ - goto cleanup; \ - } \ - } \ -cleanup: \ - rtn = ocount; \ - if (ocount == (SQLULEN) -1) \ - { \ - if (00 == 0##errcheck) \ - rtn = 0; \ - ocount = 0; \ - } \ - if (ocount >= bufcount && ocount < bufcount + lfcount) \ - return utf8_to_ucs2_lf##errcheck(utf8str, ilen, FALSE, ucs2str, bufcount); \ - if (ocount < bufcount && ucs2str) \ - ucs2str[ocount] = 0; \ -/*mylog(" ocount=%d\n", ocount);*/ \ - return rtn; \ -} +/* + * Convert a string from UTF-8 encoding to UCS-2. + * + * utf8str - input string in UTF-8 + * ilen - length of input string in bytes (or SQL_NTS) + * lfconv - TRUE if line feeds (LF) should be converted to CR + LF + * ucs2str - output buffer + * bufcount - size of output buffer + * errcheck - if TRUE, check for invalidly encoded input characters + * + * Returns the number of SQLWCHARs copied to output buffer. If the output + * buffer is too small, the output is truncated. The output string is + * NULL-terminated, except when the output is truncated. + */ +SQLULEN +utf8_to_ucs2_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv, + SQLWCHAR *ucs2str, SQLULEN bufcount, BOOL errcheck) +{ + int i; + SQLULEN rtn, ocount, wcode; + const UCHAR *str; -def_utf2ucs(0) -def_utf2ucs(1) +/*mylog("utf8_to_ucs2 ilen=%d bufcount=%d", ilen, bufcount);*/ + if (!utf8str) + return 0; +/*mylog(" string=%s\n", utf8str);*/ + + if (!bufcount) + ucs2str = NULL; + else if (!ucs2str) + bufcount = 0; + if (ilen < 0) + ilen = strlen(utf8str); + for (i = 0, ocount = 0, str = utf8str; i < ilen && *str;) + { + /* if (iswascii(*str)) */ + if (isascii(*str)) + { + if (lfconv && PG_LINEFEED == *str && + (i == 0 || PG_CARRIAGE_RETURN != str[-1])) + { + if (ocount < bufcount) + ucs2str[ocount] = PG_CARRIAGE_RETURN; + ocount++; + } + if (ocount < bufcount) + ucs2str[ocount] = *str; + ocount++; + i++; + str++; + } + else if (0xf8 == (*str & 0xf8)) /* more than 5 byte code */ + { + ocount = (SQLULEN) -1; + goto cleanup; + } + else if (0xf0 == (*str & 0xf8)) /* 4 byte code */ + { + if (errcheck) + { + if (i + 4 > ilen || + 0 == (str[1] & 0x80) || + 0 == (str[2] & 0x80) || + 0 == (str[3] & 0x80)) + { + ocount = (SQLULEN) -1; + goto cleanup; + } + } + if (ocount < bufcount) + { + wcode = (surrog1_bits | + ((((UInt4) *str) & byte4_m1) << 8) | + ((((UInt4) str[1]) & byte4_m2) << 2) | + ((((UInt4) str[2]) & byte4_m31) >> 4)) + - surrogate_adjust; + ucs2str[ocount] = (SQLWCHAR) wcode; + } + ocount++; + if (ocount < bufcount) + { + wcode = surrog2_bits | + ((((UInt4) str[2]) & byte4_m32) << 6) | + (((UInt4) str[3]) & byte4_m4); + ucs2str[ocount] = (SQLWCHAR) wcode; + } + ocount++; + i += 4; + str += 4; + } + else if (0xe0 == (*str & 0xf0)) /* 3 byte code */ + { + if (errcheck) + { + if (i + 3 > ilen || + 0 == (str[1] & 0x80) || + 0 == (str[2] & 0x80)) + { + ocount = (SQLULEN) -1; + goto cleanup; + } + } + if (ocount < bufcount) + { + wcode = ((((UInt4) *str) & byte3_m1) << 12) | + ((((UInt4) str[1]) & byte3_m2) << 6) | + (((UInt4) str[2]) & byte3_m3); + ucs2str[ocount] = (SQLWCHAR) wcode; + } + ocount++; + i += 3; + str += 3; + } + else if (0xc0 == (*str & 0xe0)) /* 2 byte code */ + { + if (errcheck) + { + if (i + 2 > ilen || + 0 == (str[1] & 0x80)) + { + ocount = (SQLULEN) -1; + goto cleanup; + } + } + if (ocount < bufcount) + { + wcode = ((((UInt4) *str) & byte2_m1) << 6) | + (((UInt4) str[1]) & byte2_m2); + ucs2str[ocount] = (SQLWCHAR) wcode; + } + ocount++; + i += 2; + str += 2; + } + else + { + ocount = (SQLULEN) -1; + goto cleanup; + } + } +cleanup: + rtn = ocount; + if (ocount == (SQLULEN) -1) + { + if (!errcheck) + rtn = 0; + ocount = 0; + } + if (ocount < bufcount && ucs2str) + ucs2str[ocount] = 0; +/*mylog(" ocount=%d\n", ocount);*/ + return rtn; +} int msgtowstr(const char *enc, const char *inmsg, int inlen, LPWSTR outmsg, int buflen) { --------------080703000805080501030808 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------080703000805080501030808--