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:
mkkang
2025-08-05 09:17:42 +09:00
committed by Vadim Zeitlin
parent 143f302bb2
commit e61bb364d9
3 changed files with 6 additions and 4 deletions

View File

@@ -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;

View File

@@ -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:

View File

@@ -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"
")";
}
};