diff options
author | Andy Shaw <andy.shaw@qt.io> | 2018-09-06 12:39:31 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2018-09-11 16:39:03 +0000 |
commit | a3e8b92d971aef58174aefaa60741d16af8271ae (patch) | |
tree | 6a999a0b8133431b7f741fafac033fb3f9515aa6 | |
parent | d8c210d7f576fc54b41ad06c061ece7cadeed648 (diff) |
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 <edward.welbourne@qt.io>
-rw-r--r-- | src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 9 |
1 files 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<SQLTCHAR>& 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<const char *>(buf.constData()), realsize); if (lengthIndicator < SQLLEN(colSize)) { // workaround for Drivermanagers that don't return SQL_NO_DATA break; |