pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Null Characters in Strings, Version 9.3.1
15+ messages / 4 participants
[nested] [flat]

* Null Characters in Strings, Version 9.3.1
@ 2014-02-13 17:53 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  0 siblings, 1 reply; 15+ messages in thread

From: Nils Gösche @ 2014-02-13 17:53 UTC (permalink / raw)
  To: pgsql-odbc

Hi!

I have noticed a change of behavior in version 9.3.1 of the ODBC driver. I am using the 32 bit version for testing, haven’t tried 64 bit yet.

I am retrieving a string value from a table that has a column of type ‘text’. This worked just fine with version 9.2.1 of the driver, but since I upgraded the ODBC driver to 9.3.1, something strange is happening:

In psql, I do:

select length(body) from macros where name = 'check_pruefziffer';

and it returns 459. The text contains only ASCII characters.

If I retrieve the value in a C# program using the ODBC driver, I get a string that has a null character at position 459, but a total length of 487!  The string up to position 458 is correct, but has now been extended with a null character and a few junk characters.

This shouldn’t be happening, right? Am I doing something wrong here? Here is some C# code that I wrote for testing:

    class Program
    {
        const string ConnString = @"Driver={PostgreSQL Unicode};server=localhost;port=5432;database=...;uid...;pwd=...";

        static void Main(string[] args)
        {
            try
            {
                using (var db = new OdbcConnection(ConnString))
                {
                    db.Open();
                    using (var trans = db.BeginTransaction())
                    {
                        try
                        {
                            const string cmdText = @"SELECT body FROM macros WHERE name = 'check_pruefziffer'";
                            using (var cmd = new OdbcCommand(cmdText, db, trans))
                            {
                                var ret = cmd.ExecuteScalar();
                                if (ret == null)
                                {
                                    Console.WriteLine("ret == null");
                                }
                                else
                                {
                                    Console.WriteLine("ret is {0}: {1}", ret.GetType(), ret);
                                    Console.WriteLine("\nChecking for null byte...");
                                    string s = (string) ret;
                                    int pos = s.IndexOf('\0');
                                    if (pos >= 0)
                                    {
                                        Console.WriteLine("Found null byte at {0}!  Total = {1}", pos, s.Length);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Not found.");
                                    }
                                }
                            }
                            trans.Commit();
                        }
                        catch
                        {
                            trans.Rollback();
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message);
            }
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }
    }

Here is the tail of the output:

[SNIP]

  $computedZiffer = 10 - ($sum % 10)

  if ($computedZiffer -eq $pruefZiffer) { $ret = $true }
}
$ret ) { $ret = $true }
}
$ret

Checking for null byte...
Found null byte at 459!  Total = 487
Press any key to exit...

If you select the same value in psql, the tail looks like this:

[SNIP]
                                                                +
   $computedZiffer = 10 - ($sum % 10)                           +
                                                                +
   if ($computedZiffer -eq $pruefZiffer) { $ret = $true }       +
 }                                                              +
 $ret
(1 row)

The string that the C# program is getting contains a null byte plus some junk after the $ret.

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."




-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
@ 2014-02-18 17:06 ` Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Nils Gösche @ 2014-02-18 17:06 UTC (permalink / raw)
  To: pgsql-odbc

I wrote:

> If I retrieve the value in a C# program using the ODBC driver, I get a
> string that has a null character at position 459, but a total length of
> 487!  The string up to position 458 is correct, but has now been
> extended with a null character and a few junk characters.

Am I the only one thinking this is a serious bug? I already had to
downgrade the ODBC driver to 9.2.1 on a customer's machine because of this...

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."




-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
@ 2014-02-18 17:19   ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  0 siblings, 1 reply; 15+ messages in thread

From: Heikki Linnakangas @ 2014-02-18 17:19 UTC (permalink / raw)
  To: Nils Gösche <cartan@cartan.de>; +Cc: pgsql-odbc

On 02/18/2014 07:06 PM, Nils Gösche wrote:
> I wrote:
>
>> If I retrieve the value in a C# program using the ODBC driver, I get a
>> string that has a null character at position 459, but a total length of
>> 487!  The string up to position 458 is correct, but has now been
>> extended with a null character and a few junk characters.
>
> Am I the only one thinking this is a serious bug?

Maybe.. I don't have a C# environment to test this with; if you can 
write a small stand-alone C program to reproduce this and post it, I'll 
take a look.

- Heikki


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2014-02-18 19:39     ` Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Nils Gösche @ 2014-02-18 19:39 UTC (permalink / raw)
  To: 'Heikki Linnakangas' <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

Heikki wrote:

> On 02/18/2014 07:06 PM, Nils Gösche wrote:
> > I wrote:
> >
> >> If I retrieve the value in a C# program using the ODBC driver, I get
> >> a string that has a null character at position 459, but a total
> >> length of 487!  The string up to position 458 is correct, but has
> now
> >> been extended with a null character and a few junk characters.
> >
> > Am I the only one thinking this is a serious bug?
> 
> Maybe.. I don't have a C# environment to test this with; if you can
> write a small stand-alone C program to reproduce this and post it, I'll
> take a look.

Thanks! I have never written an ODBC program in C before. Wow, what a PITA... ;-).  I took a little example program from Microsoft's site and adapted it.  The code is attached.  The essential point is at line 353:

    if (wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t) != pThisBinding->indPtr)
    {
        wprintf(L"2 * wcslen = %d, indPtr = %d\n", wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t),
                pThisBinding->indPtr);
    }
    else
    {
        wprintf(pThisBinding->fChar ? DISPLAY_FORMAT_C:DISPLAY_FORMAT,
                PIPE,
                pThisBinding->cDisplaySize,
                pThisBinding->cDisplaySize,
                pThisBinding->wszBuffer);
    }

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:

Connected!
Enter SQL commands, type (control)Z to exit
SQL COMMAND>| body
          |
2 * wcslen = 918, indPtr = 974
 |

SQL COMMAND>
Disconnected.

(indPtr is what we got in the last parameter of SQLBindCol)

The numeric values here are consistent with the result I am getting in the C# program: There is a null character at character index 459, but the returned value is actually larger than that (974 bytes), resulting in a C# string with 487 characters!

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

CODBCHello.cpp (text/plain, 19.2K) download /******************************************************************************* /* ODBCSQL: a sample program that implements an ODBC command line interpreter. /* /* USAGE: ODBCSQL DSN=<dsn name> or /* ODBCSQL FILEDSN=<file dsn> or /* ODBCSQL DRIVER={driver name} /* /* /* Copyright(c) Microsoft Corporation. This is a WDAC sample program and /* is not suitable for use in production environments. /* /******************************************************************************/ /* Modules: /* Main Main driver loop, executes queries. /* DisplayResults Display the results of the query if any /* AllocateBindings Bind column data /* DisplayTitles Print column titles /* SetConsole Set console display mode /* HandleError Show ODBC error messages /******************************************************************************/ #include <windows.h> #include <sql.h> #include <sqlext.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #include <stdlib.h> #include <sal.h> /*******************************************/ /* Macro to call ODBC functions and */ /* report an error on failure. */ /* Takes handle, handle type, and stmt */ /*******************************************/ #define TRYODBC(h, ht, x) { RETCODE rc = x;\ if (rc != SQL_SUCCESS) \ { \ HandleDiagnosticRecord (h, ht, rc); \ } \ if (rc == SQL_ERROR) \ { \ fwprintf(stderr, L"Error in " L#x L"\n"); \ goto Exit; \ } \ } /******************************************/ /* Structure to store information about */ /* a column. /******************************************/ typedef struct STR_BINDING { SQLSMALLINT cDisplaySize; /* size to display */ WCHAR *wszBuffer; /* display buffer */ SQLLEN indPtr; /* size or null */ BOOL fChar; /* character col? */ struct STR_BINDING *sNext; /* linked list */ } BINDING; /******************************************/ /* Forward references */ /******************************************/ void HandleDiagnosticRecord (SQLHANDLE hHandle, SQLSMALLINT hType, RETCODE RetCode); void DisplayResults(HSTMT hStmt, SQLSMALLINT cCols); void AllocateBindings(HSTMT hStmt, SQLSMALLINT cCols, BINDING** ppBinding, SQLSMALLINT* pDisplay); void DisplayTitles(HSTMT hStmt, DWORD cDisplaySize, BINDING* pBinding); void SetConsole(DWORD cDisplaySize, BOOL fInvert); /*****************************************/ /* Some constants */ /*****************************************/ #if 0 # define DISPLAY_MAX 50 // Arbitrary limit on column width to display #else # define DISPLAY_MAX 8192 // Arbitrary limit on column width to display #endif #define DISPLAY_FORMAT_EXTRA 3 // Per column extra display bytes (| <data> ) #define DISPLAY_FORMAT L"%c %*.*s " #define DISPLAY_FORMAT_C L"%c %-*.*s " #define NULL_SIZE 6 // <NULL> #define SQL_QUERY_SIZE 1000 // Max. Num characters for SQL Query passed in. #define PIPE L'|' SHORT gHeight = 80; // Users screen height int __cdecl wmain(int argc, WCHAR **argv) { SQLHENV hEnv = NULL; SQLHDBC hDbc = NULL; SQLHSTMT hStmt = NULL; WCHAR* pwszConnStr; WCHAR wszInput[SQL_QUERY_SIZE]; // Allocate an environment if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR) { fwprintf(stderr, L"Unable to allocate an environment handle\n"); exit(-1); } // Register this as an application that expects 3.x behavior, // you must register something if you use AllocHandle TRYODBC(hEnv, SQL_HANDLE_ENV, SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)); // Allocate a connection TRYODBC(hEnv, SQL_HANDLE_ENV, SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc)); if (argc > 1) { pwszConnStr = *++argv; } else { pwszConnStr = L""; } // Connect to the driver. Use the connection string if supplied // on the input, otherwise let the driver manager prompt for input. TRYODBC(hDbc, SQL_HANDLE_DBC, SQLDriverConnect(hDbc, GetDesktopWindow(), pwszConnStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE)); fwprintf(stderr, L"Connected!\n"); TRYODBC(hDbc, SQL_HANDLE_DBC, SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt)); wprintf(L"Enter SQL commands, type (control)Z to exit\nSQL COMMAND>"); // Loop to get input and execute queries #if 0 while(_fgetts(wszInput, SQL_QUERY_SIZE-1, stdin)) #else wcscpy(wszInput, L"SELECT body FROM macros WHERE name = 'check_pruefziffer'"); #endif { RETCODE RetCode; SQLSMALLINT sNumResults; // Execute the query if (!(*wszInput)) { wprintf(L"SQL COMMAND>"); #if 0 continue; #else goto Exit; #endif } RetCode = SQLExecDirect(hStmt,wszInput, SQL_NTS); switch(RetCode) { case SQL_SUCCESS_WITH_INFO: { HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode); // fall through } case SQL_SUCCESS: { // If this is a row-returning query, display // results TRYODBC(hStmt, SQL_HANDLE_STMT, SQLNumResultCols(hStmt,&sNumResults)); if (sNumResults > 0) { DisplayResults(hStmt,sNumResults); } else { SQLLEN cRowCount; TRYODBC(hStmt, SQL_HANDLE_STMT, SQLRowCount(hStmt,&cRowCount)); if (cRowCount >= 0) { wprintf(L"%Id %s affected\n", cRowCount, cRowCount == 1 ? L"row" : L"rows"); } } break; } case SQL_ERROR: { HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode); break; } default: fwprintf(stderr, L"Unexpected return code %hd!\n", RetCode); } TRYODBC(hStmt, SQL_HANDLE_STMT, SQLFreeStmt(hStmt, SQL_CLOSE)); wprintf(L"SQL COMMAND>"); } Exit: // Free ODBC handles and exit if (hStmt) { SQLFreeHandle(SQL_HANDLE_STMT, hStmt); } if (hDbc) { SQLDisconnect(hDbc); SQLFreeHandle(SQL_HANDLE_DBC, hDbc); } if (hEnv) { SQLFreeHandle(SQL_HANDLE_ENV, hEnv); } wprintf(L"\nDisconnected."); wprintf(L"\nPress any key to exit..."); _getch(); return 0; } /************************************************************************ /* DisplayResults: display results of a select query /* /* Parameters: /* hStmt ODBC statement handle /* cCols Count of columns /************************************************************************/ void DisplayResults(HSTMT hStmt, SQLSMALLINT cCols) { BINDING *pFirstBinding, *pThisBinding; SQLSMALLINT cDisplaySize; RETCODE RetCode = SQL_SUCCESS; int iCount = 0; // Allocate memory for each column AllocateBindings(hStmt, cCols, &pFirstBinding, &cDisplaySize); // Set the display mode and write the titles DisplayTitles(hStmt, cDisplaySize+1, pFirstBinding); // Fetch and display the data bool fNoData = false; do { // Fetch a row if (iCount++ >= gHeight - 2) { int nInputChar; bool fEnterReceived = false; while(!fEnterReceived) { wprintf(L" "); SetConsole(cDisplaySize+2, TRUE); wprintf(L" Press ENTER to continue, Q to quit (height:%hd)", gHeight); SetConsole(cDisplaySize+2, FALSE); nInputChar = _getch(); wprintf(L"\n"); if ((nInputChar == 'Q') || (nInputChar == 'q')) { goto Exit; } else if ('\r' == nInputChar) { fEnterReceived = true; } // else loop back to display prompt again } iCount = 1; DisplayTitles(hStmt, cDisplaySize+1, pFirstBinding); } TRYODBC(hStmt, SQL_HANDLE_STMT, RetCode = SQLFetch(hStmt)); if (RetCode == SQL_NO_DATA_FOUND) { fNoData = true; } else { // Display the data. Ignore truncations for (pThisBinding = pFirstBinding; pThisBinding; pThisBinding = pThisBinding->sNext) { if (pThisBinding->indPtr != SQL_NULL_DATA) { if (wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t) != pThisBinding->indPtr) { wprintf(L"2 * wcslen = %d, indPtr = %d\n", wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t), pThisBinding->indPtr); } else { wprintf(pThisBinding->fChar ? DISPLAY_FORMAT_C:DISPLAY_FORMAT, PIPE, pThisBinding->cDisplaySize, pThisBinding->cDisplaySize, pThisBinding->wszBuffer); } } else { wprintf(DISPLAY_FORMAT_C, PIPE, pThisBinding->cDisplaySize, pThisBinding->cDisplaySize, L"<NULL>"); } } wprintf(L" %c\n",PIPE); } } while (!fNoData); SetConsole(cDisplaySize+2, TRUE); wprintf(L"%*.*s", cDisplaySize+2, cDisplaySize+2, L" "); SetConsole(cDisplaySize+2, FALSE); wprintf(L"\n"); Exit: // Clean up the allocated buffers while (pFirstBinding) { pThisBinding = pFirstBinding->sNext; free(pFirstBinding->wszBuffer); free(pFirstBinding); pFirstBinding = pThisBinding; } } /************************************************************************ /* AllocateBindings: Get column information and allocate bindings /* for each column. /* /* Parameters: /* hStmt Statement handle /* cCols Number of columns in the result set /* *lppBinding Binding pointer (returned) /* lpDisplay Display size of one line /************************************************************************/ void AllocateBindings(HSTMT hStmt, SQLSMALLINT cCols, BINDING **ppBinding, SQLSMALLINT *pDisplay) { SQLSMALLINT iCol; BINDING *pThisBinding, *pLastBinding = NULL; SQLLEN cchDisplay, ssType; SQLSMALLINT cchColumnNameLength; *pDisplay = 0; for (iCol = 1; iCol <= cCols; iCol++) { pThisBinding = (BINDING *)(malloc(sizeof(BINDING))); if (!(pThisBinding)) { fwprintf(stderr, L"Out of memory!\n"); exit(-100); } if (iCol == 1) { *ppBinding = pThisBinding; } else { pLastBinding->sNext = pThisBinding; } pLastBinding = pThisBinding; // Figure out the display length of the column (we will // bind to char since we are only displaying data, in general // you should bind to the appropriate C type if you are going // to manipulate data since it is much faster...) TRYODBC(hStmt, SQL_HANDLE_STMT, SQLColAttribute(hStmt, iCol, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &cchDisplay)); // Figure out if this is a character or numeric column; this is // used to determine if we want to display the data left- or right- // aligned. // SQL_DESC_CONCISE_TYPE maps to the 1.x SQL_COLUMN_TYPE. // This is what you must use if you want to work // against a 2.x driver. TRYODBC(hStmt, SQL_HANDLE_STMT, SQLColAttribute(hStmt, iCol, SQL_DESC_CONCISE_TYPE, NULL, 0, NULL, &ssType)); pThisBinding->fChar = (ssType == SQL_CHAR || ssType == SQL_VARCHAR || ssType == SQL_LONGVARCHAR); pThisBinding->sNext = NULL; // Arbitrary limit on display size if (cchDisplay > DISPLAY_MAX) cchDisplay = DISPLAY_MAX; // Allocate a buffer big enough to hold the text representation // of the data. Add one character for the null terminator pThisBinding->wszBuffer = (WCHAR *)malloc((cchDisplay+1) * sizeof(WCHAR)); if (!(pThisBinding->wszBuffer)) { fwprintf(stderr, L"Out of memory!\n"); exit(-100); } // Map this buffer to the driver's buffer. At Fetch time, // the driver will fill in this data. Note that the size is // count of bytes (for Unicode). All ODBC functions that take // SQLPOINTER use count of bytes; all functions that take only // strings use count of characters. TRYODBC(hStmt, SQL_HANDLE_STMT, SQLBindCol(hStmt, iCol, SQL_C_TCHAR, (SQLPOINTER) pThisBinding->wszBuffer, (cchDisplay + 1) * sizeof(WCHAR), &pThisBinding->indPtr)); // Now set the display size that we will use to display // the data. Figure out the length of the column name TRYODBC(hStmt, SQL_HANDLE_STMT, SQLColAttribute(hStmt, iCol, SQL_DESC_NAME, NULL, 0, &cchColumnNameLength, NULL)); pThisBinding->cDisplaySize = max((SQLSMALLINT)cchDisplay, cchColumnNameLength); if (pThisBinding->cDisplaySize < NULL_SIZE) pThisBinding->cDisplaySize = NULL_SIZE; *pDisplay += pThisBinding->cDisplaySize + DISPLAY_FORMAT_EXTRA; } return; Exit: exit(-1); return; } /************************************************************************ /* DisplayTitles: print the titles of all the columns and set the /* shell window's width /* /* Parameters: /* hStmt Statement handle /* cDisplaySize Total display size /* pBinding list of binding information /************************************************************************/ void DisplayTitles(HSTMT hStmt, DWORD cDisplaySize, BINDING *pBinding) { WCHAR wszTitle[DISPLAY_MAX]; SQLSMALLINT iCol = 1; SetConsole(cDisplaySize+2, TRUE); for (; pBinding; pBinding = pBinding->sNext) { TRYODBC(hStmt, SQL_HANDLE_STMT, SQLColAttribute(hStmt, iCol++, SQL_DESC_NAME, wszTitle, sizeof(wszTitle), // Note count of bytes! NULL, NULL)); wprintf(DISPLAY_FORMAT_C, PIPE, pBinding->cDisplaySize, pBinding->cDisplaySize, wszTitle); } Exit: wprintf(L" %c", PIPE); SetConsole(cDisplaySize+2, FALSE); wprintf(L"\n"); } /************************************************************************ /* SetConsole: sets console display size and video mode /* /* Parameters /* siDisplaySize Console display size /* fInvert Invert video? /************************************************************************/ void SetConsole(DWORD dwDisplaySize, BOOL fInvert) { HANDLE hConsole; CONSOLE_SCREEN_BUFFER_INFO csbInfo; // Reset the console screen buffer size if necessary hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if (hConsole != INVALID_HANDLE_VALUE) { if (GetConsoleScreenBufferInfo(hConsole, &csbInfo)) { if (csbInfo.dwSize.X < (SHORT) dwDisplaySize) { csbInfo.dwSize.X = (SHORT) dwDisplaySize; SetConsoleScreenBufferSize(hConsole, csbInfo.dwSize); } gHeight = csbInfo.dwSize.Y; } if (fInvert) { SetConsoleTextAttribute(hConsole, (WORD)(csbInfo.wAttributes | BACKGROUND_BLUE)); } else { SetConsoleTextAttribute(hConsole, (WORD)(csbInfo.wAttributes & ~(BACKGROUND_BLUE))); } } } /************************************************************************ /* HandleDiagnosticRecord : display error/warning information /* /* Parameters: /* hHandle ODBC handle /* hType Type of handle (HANDLE_STMT, HANDLE_ENV, HANDLE_DBC) /* RetCode Return code of failing command /************************************************************************/ void HandleDiagnosticRecord (SQLHANDLE hHandle, SQLSMALLINT hType, RETCODE RetCode) { SQLSMALLINT iRec = 0; SQLINTEGER iError; WCHAR wszMessage[1000]; WCHAR wszState[SQL_SQLSTATE_SIZE+1]; if (RetCode == SQL_INVALID_HANDLE) { fwprintf(stderr, L"Invalid handle!\n"); return; } while (SQLGetDiagRec(hType, hHandle, ++iRec, wszState, &iError, wszMessage, (SQLSMALLINT)(sizeof(wszMessage) / sizeof(WCHAR)), (SQLSMALLINT *)NULL) == SQL_SUCCESS) { // Hide data truncated.. if (wcsncmp(wszState, L"01004", 5)) { fwprintf(stderr, L"[%5.5s] %s (%d)\n", wszState, wszMessage, iError); } } }
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
@ 2014-02-18 20:30       ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  0 siblings, 1 reply; 15+ messages in thread

From: Heikki Linnakangas @ 2014-02-18 20:30 UTC (permalink / raw)
  To: Nils Gösche <cartan@cartan.de>; +Cc: pgsql-odbc

On 02/18/2014 09:39 PM, Nils Gösche wrote:
> Heikki wrote:
>
>> On 02/18/2014 07:06 PM, Nils Gösche wrote:
>>> I wrote:
>>>
>>>> If I retrieve the value in a C# program using the ODBC driver, I get
>>>> a string that has a null character at position 459, but a total
>>>> length of 487!  The string up to position 458 is correct, but has
>> now
>>>> been extended with a null character and a few junk characters.
>>>
>>> Am I the only one thinking this is a serious bug?
>>
>> Maybe.. I don't have a C# environment to test this with; if you can
>> write a small stand-alone C program to reproduce this and post it, I'll
>> take a look.
>
> Thanks! I have never written an ODBC program in C before. Wow, what a PITA... ;-).

Yep, it's quite verbose :-).

>  I took a little example program from Microsoft's site and adapted it.  The code is attached.

I'm afraid I can't easily compile and execute that either, with all the 
Windows-ism's in it. Could you pick one of the regression test cases 
(e.g 
http://git.postgresql.org/gitweb/?p=psqlodbc.git;a=blob_plain;f=test/src/select-test.c;hb=HEAD), 
and modify it to reproduce the problem? And please also include the SQL 
statements to create the test table and data.

>  The essential point is at line 353:
>
>      if (wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t) != pThisBinding->indPtr)
>      {
>          wprintf(L"2 * wcslen = %d, indPtr = %d\n", wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t),
>                  pThisBinding->indPtr);
>      }
>      else
>      {
>          wprintf(pThisBinding->fChar ? DISPLAY_FORMAT_C:DISPLAY_FORMAT,
>                  PIPE,
>                  pThisBinding->cDisplaySize,
>                  pThisBinding->cDisplaySize,
>                  pThisBinding->wszBuffer);
>      }
>
> 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.

- Heikki


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2014-02-19 00:09         ` Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 11:21           ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 2 replies; 15+ messages in thread

From: Hiroshi Inoue @ 2014-02-19 00:09 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; Nils Gösche <cartan@cartan.de>; +Cc: pgsql-odbc

(2014/02/19 5:30), Heikki Linnakangas wrote:
> On 02/18/2014 09:39 PM, Nils Gösche wrote:
>> Heikki wrote:
>>
>>> On 02/18/2014 07:06 PM, Nils Gösche wrote:
>>>> I wrote:
>>>>
>>>>> If I retrieve the value in a C# program using the ODBC driver, I get
>>>>> a string that has a null character at position 459, but a total
>>>>> length of 487!  The string up to position 458 is correct, but has
>>> now
>>>>> been extended with a null character and a few junk characters.
>>>>
>>>> Am I the only one thinking this is a serious bug?
>>>
>>> Maybe.. I don't have a C# environment to test this with; if you can
>>> write a small stand-alone C program to reproduce this and post it, I'll
>>> take a look.
>>
>> Thanks! I have never written an ODBC program in C before. Wow, what a
>> PITA... ;-).
>
> Yep, it's quite verbose :-).
>
>>  I took a little example program from Microsoft's site and adapted
>> it.  The code is attached.
>
> I'm afraid I can't easily compile and execute that either, with all the
> Windows-ism's in it. Could you pick one of the regression test cases
> (e.g
> http://git.postgresql.org/gitweb/?p=psqlodbc.git;a=blob_plain;f=test/src/select-test.c;hb=HEAD),
> and modify it to reproduce the problem? And please also include the SQL
> statements to create the test table and data.
>
>>  The essential point is at line 353:
>>
>>      if (wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t) !=
>> pThisBinding->indPtr)
>>      {
>>          wprintf(L"2 * wcslen = %d, indPtr = %d\n",
>> wcslen(pThisBinding->wszBuffer) * sizeof(wchar_t),
>>                  pThisBinding->indPtr);
>>      }
>>      else
>>      {
>>          wprintf(pThisBinding->fChar ? DISPLAY_FORMAT_C:DISPLAY_FORMAT,
>>                  PIPE,
>>                  pThisBinding->cDisplaySize,
>>                  pThisBinding->cDisplaySize,
>>                  pThisBinding->wszBuffer);
>>      }
>>
>> 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 <inoue@tpf.co.p>
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.

regards,
Hiroshi Inoue



-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
@ 2014-02-19 11:21           ` Nils Gösche <cartan@cartan.de>
  2014-02-20 00:51             ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  1 sibling, 1 reply; 15+ messages in thread

From: Nils Gösche @ 2014-02-19 11:21 UTC (permalink / raw)
  To: 'Hiroshi Inoue' <inoue@tpf.co.jp>; 'Heikki Linnakangas' <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

Inoue-san wrote:

> Maybe it's caused by the commit
> 3666c87c1440862bde2e6b8f43ee585deed70d31
> Author: Hiroshi Inoue <inoue@tpf.co.p>
> 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.

Ah, ok: If I set LFConversion=0, I get correct values in C#, without any null characters. But also without Ctrl-Ms!  Too bad that our software has come to rely on the Ctrl-M conversion taking place already.  If you think about the possibility of having both Windows and Unix clients working on the same database, I suppose having this conversion being done in the driver is not a bad thing.

So, if you could make LFConversion work reliably again, I would be a happy camper :-)

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."




-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 11:21           ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
@ 2014-02-20 00:51             ` Hiroshi Inoue <inoue@tpf.co.jp>
  0 siblings, 0 replies; 15+ messages in thread

From: Hiroshi Inoue @ 2014-02-20 00:51 UTC (permalink / raw)
  To: Nils Gösche <cartan@cartan.de>; Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

(2014/02/19 20:21), Nils Gösche wrote:
> Inoue-san wrote:
>
>> Maybe it's caused by the commit
>> 3666c87c1440862bde2e6b8f43ee585deed70d31
>> Author: Hiroshi Inoue <inoue@tpf.co.p>
>> 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.
>
> Ah, ok: If I set LFConversion=0, I get correct values in C#, without any null characters. But also without Ctrl-Ms!  Too bad that our software has come to rely on the Ctrl-M conversion taking place already.  If you think about the possibility of having both Windows and Unix clients working on the same database, I suppose having this conversion being done in the driver is not a bad thing.
>
> So, if you could make LFConversion work reliably again, I would be a happy camper :-)

OK I already put back the commit.

regards,
Hiroshi Inoue


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
@ 2014-02-19 13:12           ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-20 01:43             ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-24 11:59             ` Re: Null Characters in Strings, Version 9.3.1 Christoph Berg <christoph.berg@credativ.de>
  1 sibling, 2 replies; 15+ messages in thread

From: Heikki Linnakangas @ 2014-02-19 13:12 UTC (permalink / raw)
  To: Hiroshi Inoue <inoue@tpf.co.jp>; +Cc: Nils Gösche <cartan@cartan.de>; pgsql-odbc

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 <inoue@tpf.co.p>
> 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

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

lfconversion-fix+test.patch (text/x-diff, 13.9K) download 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 <stdio.h> +#include <stdlib.h> +#include <errno.h> + +#if 0 +#include <iconv.h> +#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) {
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2014-02-20 01:43             ` Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-24 10:37               ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  1 sibling, 1 reply; 15+ messages in thread

From: Hiroshi Inoue @ 2014-02-20 01:43 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: Nils Gösche <cartan@cartan.de>; pgsql-odbc

(2014/02/19 22:12), Heikki Linnakangas wrote:
> 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 <inoue@tpf.co.p>
>> 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.

The commit was for e.g. columns bound by SQLBindCols. In such cases
truncated data can't be seen any more. I forgot about SQLGetData()
when applying the commit.
I already put back the commit.

> 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 didn't want to open error-checking on the face because the use cases
  are considerably depended on luck.

> 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.

I don't object to the patch.

regards,
Hiroshi Inoue


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-20 01:43             ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
@ 2014-02-24 10:37               ` Nils Gösche <cartan@cartan.de>
  0 siblings, 0 replies; 15+ messages in thread

From: Nils Gösche @ 2014-02-24 10:37 UTC (permalink / raw)
  To: pgsql-odbc

Hi!

> I don't object to the patch.
> 
> regards,
> Hiroshi Inoue

Version 9.3.2 works fine again, here. Thanks!

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."
 



-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2014-02-24 11:59             ` Christoph Berg <christoph.berg@credativ.de>
  2014-02-24 13:37               ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  1 sibling, 1 reply; 15+ messages in thread

From: Christoph Berg @ 2014-02-24 11:59 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

Re: Heikki Linnakangas 2014-02-19 <5304ADB1.6090306@vmware.com>
> 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

Hi Heikki,

I'm afraid you forgot to update Makefile.am to include that file so it
didn't make it into the 09.03.0200 tarball, rendering the testsuite
unusable.

Christoph
-- 
cb@df7cb.de | http://www.df7cb.de/

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-24 11:59             ` Re: Null Characters in Strings, Version 9.3.1 Christoph Berg <christoph.berg@credativ.de>
@ 2014-02-24 13:37               ` Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-24 13:41                 ` Re: Null Characters in Strings, Version 9.3.1 Christoph Berg <christoph.berg@credativ.de>
  0 siblings, 1 reply; 15+ messages in thread

From: Heikki Linnakangas @ 2014-02-24 13:37 UTC (permalink / raw)
  To: Christoph Berg <christoph.berg@credativ.de>; pgsql-odbc

On 02/24/2014 01:59 PM, Christoph Berg wrote:
> Re: Heikki Linnakangas 2014-02-19 <5304ADB1.6090306@vmware.com>
>> 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
>
> Hi Heikki,
>
> I'm afraid you forgot to update Makefile.am to include that file so it
> didn't make it into the 09.03.0200 tarball, rendering the testsuite
> unusable.

Oh, I wasn't even aware that EXTRA_DIST needs to be updated manually! 
Fixed now. Looking at the commit log, Hiroshi and Hiroshi have been 
fixing it after me every time I've added a new test case. Thanks for 
doing that, I'll try to remember it myself the next time.

BTW, do we care about building with non-GNU make? If not, we could use 
wildcards in EXTRA_DIST instead of listing the files individually.

- Heikki


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
  2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:06 ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 17:19   ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-18 19:39     ` Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
  2014-02-18 20:30       ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-19 00:09         ` Re: Null Characters in Strings, Version 9.3.1 Hiroshi Inoue <inoue@tpf.co.jp>
  2014-02-19 13:12           ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
  2014-02-24 11:59             ` Re: Null Characters in Strings, Version 9.3.1 Christoph Berg <christoph.berg@credativ.de>
  2014-02-24 13:37               ` Re: Null Characters in Strings, Version 9.3.1 Heikki Linnakangas <hlinnakangas@vmware.com>
@ 2014-02-24 13:41                 ` Christoph Berg <christoph.berg@credativ.de>
  0 siblings, 0 replies; 15+ messages in thread

From: Christoph Berg @ 2014-02-24 13:41 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

Re: Heikki Linnakangas 2014-02-24 <530B4B06.5040007@vmware.com>
> BTW, do we care about building with non-GNU make? If not, we could
> use wildcards in EXTRA_DIST instead of listing the files
> individually.

Given that psqlodbc needs libpq, libpq is built from PostgreSQL, and
that insists on GNU make, that's not even a new requirement.

Christoph
-- 
cb@df7cb.de | http://www.df7cb.de/

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: Null Characters in Strings, Version 9.3.1
@ 2014-02-18 23:28 Nils Gösche <cartan@cartan.de>
  0 siblings, 0 replies; 15+ messages in thread

From: Nils Gösche @ 2014-02-18 23:28 UTC (permalink / raw)
  To: 'Heikki Linnakangas' <hlinnakangas@vmware.com>; +Cc: pgsql-odbc

Heikki wrote:

> I'm afraid I can't easily compile and execute that either, with all the
Windows-ism's in it. Could you pick one of the regression test cases (e.g
http://git.postgresql.org/gitweb/?p=psqlodbc.git;a=blob_plain;f=test/src/sel
ect-test.c;hb=HEAD),
> and modify it to reproduce the problem? And please also include the SQL
statements to create the test table and data.

Ok, I included a little create script, and modified one of the regression
test files to fetch a single text value as WCHAR from a single row of data.
The resulting program is reasonably simple.

The end of the output with version 9.2.1 of the driver looks like this:

  $computedZiffer = 10 - ($sum % 10)

  if ($computedZiffer -eq $pruefZiffer) { $ret = $true }
}
$ret
strlen = 487
sqlLen = 974
disconnecting
Press ENTER to exit...

And here it is with 9.3.1:

  $computedZiffer = 10 - ($sum % 10)

  if ($computedZiffer -eq $pruefZiffer) { $ret = $true }
}
$ret
strlen = 459
sqlLen = 974
disconnecting
Press ENTER to exit...

strlen is different in both cases, but sqlLen is the same. I modified the
program to output the received SQL_C_WCHAR value in binary (preceded by a
UTF16 BOM). The two result files are attached (value921.txt and
value931.txt). If you look at them in a hex editor, you will notice that the
9.2.1 version contains some Ctrl-M characters! Apparently, these have been
stripped in the 9.3.1 version, but the sqlLen value is still the same. I
suppose that .NET uses the latter value to determine how many characters to
include in its query result string (null characters are perfectly normal
characters in C# strings).

Regards,
--
Nils Gösche 
Don’t ask for whom the <Ctrl-G> tolls. 

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

  Create.sql (application/octet-stream, 596B) download

CODBCSimple.c (text/plain, 1.5K) download #include <stdio.h> #include <stdlib.h> #include "common.h" void write_output(const unsigned char *buf, int count) { FILE *fs = fopen("value.txt", "wb"); if (fs == NULL) { fprintf(stderr, "fopen failed: %s\n", strerror(errno)); } else { unsigned char bom[2] = { 0xFF, 0xFE }; fwrite(bom, 1, 2, fs); fwrite(buf, 1, count, fs); fclose(fs); } } int main(int argc, char **argv) { SQLRETURN rc; SQLLEN sqlLen; HSTMT hstmt = SQL_NULL_HSTMT; char *sql; char buf[8192]; wchar_t wbuf[8192]; test_connect(); rc = SQLAllocStmt(conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } /* Run a query with a result set with all kinds of values */ sql = "SELECT body FROM macros WHERE name = 'check_pruefziffer'"; rc = SQLExecDirect(hstmt, (SQLCHAR *)sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); /* Fetch result */ rc = SQLFetch(hstmt); CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); rc = SQLGetData(hstmt, 1, SQL_C_WCHAR, wbuf, sizeof(wbuf), &sqlLen); CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt); wprintf(L"varcharcol: %s\n", wbuf); printf("strlen = %u\n", wcslen(wbuf)); printf("sqlLen = %d\n", sqlLen); write_output((unsigned char *) wbuf, sqlLen); rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); /* Clean up */ test_disconnect(); puts("Press ENTER to exit..."); fgets(buf, sizeof buf, stdin); return 0; }
value921.txt (text/plain, 976B) download ÿþParam ( [parameter(Mandatory=$true, HelpMessage="The barcode value")] [string]$bc ) $ret = $false if ($bc.Length -gt 1) { $pruefZiffer = $bc[$bc.Length - 1] - 48 $sum = 0 $len = $bc.Length - 1 for ($i = 0; $i -lt $len; $i++) { $dig = $bc[$i] - 48 if ($i % 2 -eq 0) { $sum += 3 * $dig } else { $sum += $dig } } $computedZiffer = 10 - ($sum % 10) if ($computedZiffer -eq $pruefZiffer) { $ret = $true } } $ret
value931.txt (text/plain, 976B) download ÿþParam ( [parameter(Mandatory=$true, HelpMessage="The barcode value")] [string]$bc ) $ret = $false if ($bc.Length -gt 1) { $pruefZiffer = $bc[$bc.Length - 1] - 48 $sum = 0 $len = $bc.Length - 1 for ($i = 0; $i -lt $len; $i++) { $dig = $bc[$i] - 48 if ($i % 2 -eq 0) { $sum += 3 * $dig } else { $sum += $dig } } $computedZiffer = 10 - ($sum % 10) if ($computedZiffer -eq $pruefZiffer) { $ret = $true } } $ret) { $ret = $true } } $ret
^ permalink raw reply [nested|flat] 15+ messages in thread


end of thread, other threads:[~2014-02-24 13:41 UTC | newest]

Thread overview: 15+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-02-13 17:53 Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>
2014-02-18 17:06 ` Nils Gösche <cartan@cartan.de>
2014-02-18 17:19   ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-02-18 19:39     ` Nils Gösche <cartan@cartan.de>
2014-02-18 20:30       ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-02-19 00:09         ` Hiroshi Inoue <inoue@tpf.co.jp>
2014-02-19 11:21           ` Nils Gösche <cartan@cartan.de>
2014-02-20 00:51             ` Hiroshi Inoue <inoue@tpf.co.jp>
2014-02-19 13:12           ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-02-20 01:43             ` Hiroshi Inoue <inoue@tpf.co.jp>
2014-02-24 10:37               ` Nils Gösche <cartan@cartan.de>
2014-02-24 11:59             ` Christoph Berg <christoph.berg@credativ.de>
2014-02-24 13:37               ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-02-24 13:41                 ` Christoph Berg <christoph.berg@credativ.de>
2014-02-18 23:28 Re: Null Characters in Strings, Version 9.3.1 Nils Gösche <cartan@cartan.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox