From a3e8b92d971aef58174aefaa60741d16af8271ae Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Thu, 6 Sep 2018 12:39:31 +0200 Subject: ODBC: Remove the trailing \0 in the case of a non unicode string Some ODBC drivers, such as old Informix ODBC drivers will incorrectly include a trailing \0 in a string when this should not exist. For unicode strings this was already accounted for, but for non-unicode ones this was not covered. The change also fixes up the comments a bit to make this clearer and also added one for the unicode case. Task-number: QTBUG-62406 Change-Id: Id932a58d9e5fdff2f4d1aacf8cc9fdaeb34f95f4 Reviewed-by: Edward Welbourne --- src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp index 547eb2043d..daf9686b5e 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp +++ b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp @@ -72,6 +72,7 @@ inline static QString fromSQLTCHAR(const QVarLengthArray& input, int s { QString result; + // Remove any trailing \0 as some drivers misguidedly append one int realsize = qMin(size, input.size()); if(realsize > 0 && input[realsize-1] == 0) realsize--; @@ -458,7 +459,6 @@ static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool uni // more data can be fetched, the length indicator does NOT // contain the number of bytes returned - it contains the // total number of bytes that CAN be fetched - // colSize-1: remove 0 termination when there is more data to fetch int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : int(lengthIndicator / sizeof(SQLTCHAR)); fieldVal += fromSQLTCHAR(buf, rSize); if (lengthIndicator < SQLLEN(colSize*sizeof(SQLTCHAR))) { @@ -499,9 +499,12 @@ static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool uni // more data can be fetched, the length indicator does NOT // contain the number of bytes returned - it contains the // total number of bytes that CAN be fetched - // colSize-1: remove 0 termination when there is more data to fetch int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator; - fieldVal += QString::fromUtf8((const char *)buf.constData(), rSize); + // Remove any trailing \0 as some drivers misguidedly append one + int realsize = qMin(rSize, buf.size()); + if (realsize > 0 && buf[realsize - 1] == 0) + realsize--; + fieldVal += QString::fromUtf8(reinterpret_cast(buf.constData()), realsize); if (lengthIndicator < SQLLEN(colSize)) { // workaround for Drivermanagers that don't return SQL_NO_DATA break; -- cgit v1.2.3