mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
Fix truncating wide strings when using NVARCHAR in ODBC backend
ODBC SQLDescribeCol() returns column size in characters, but SQLBindCol() expects the buffer size in bytes. The existing code failed to multiply by sizeof(SQLWCHAR), which could result in buffer truncation, corrupted reads, or subtle bugs when binding to wide character types such as NVARCHAR. Fix this by using the size in bytes. Also update an existing unit test to show the problem: by simply reducing the buffer size, it could be made to fail without the changes in this commit. Closes #1290. Closes #1292.
This commit is contained in:
@@ -56,7 +56,8 @@ void odbc_standard_into_type_backend::define_by_pos(
|
||||
// Do exactly the same thing here as for x_stdstring above.
|
||||
size = static_cast<SQLUINTEGER>(statement_.column_size(position_));
|
||||
size = (size >= ODBC_MAX_COL_SIZE || size == 0) ? odbc_max_buffer_length : size;
|
||||
size += sizeof(SQLWCHAR);
|
||||
size++;
|
||||
size = size * sizeof(SQLWCHAR); // size in bytes
|
||||
buf_ = new char[size];
|
||||
data = buf_;
|
||||
break;
|
||||
|
||||
@@ -140,12 +140,13 @@ void odbc_vector_into_type_backend::define_by_pos(
|
||||
statement_.fetchVectorByRows_ = true;
|
||||
}
|
||||
|
||||
colSize_ += sizeof(SQLWCHAR);
|
||||
colSize_++;
|
||||
colSize_ = colSize_ * sizeof(SQLWCHAR); // size in bytes
|
||||
|
||||
const std::size_t elementsCount
|
||||
= statement_.fetchVectorByRows_ ? 1 : vectorSize;
|
||||
|
||||
buf_ = new char[colSize_ * elementsCount * sizeof(SQLWCHAR)];
|
||||
buf_ = new char[colSize_ * elementsCount];
|
||||
}
|
||||
break;
|
||||
case x_stdtm:
|
||||
|
||||
@@ -83,7 +83,7 @@ struct wide_text_table_creator : public table_creator_base
|
||||
: table_creator_base(sql)
|
||||
{
|
||||
sql << "create table soci_test ("
|
||||
"wide_text nvarchar(40) null"
|
||||
"wide_text nvarchar(17) null"
|
||||
")";
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user