Fix SQLWCHAR conversion paths on Linux (draft)#291
Fix SQLWCHAR conversion paths on Linux (draft)#291fdcastel wants to merge 3 commits intoFirebirdSQL:masterfrom
Conversation
…ectW / SQLPrepareW) Exercises both ConvertingString constructor paths in MainUnicode.cpp: output buffer (SQLGetDiagRecW / SQLErrorW) and input buffer (SQLExecDirectW / SQLPrepareW). Linux tests skip with a pointer to the follow-up Unicode rewrite PR; they are meant to drive that PR's implementation. Windows runs them for real. Per the plan in FirebirdSQL#289 comment 4279111183. Tracked as issue FirebirdSQL#287 Tier 1b.
Replace wchar_t-assuming mbstowcs/wcstombs/wcslen calls in the non- connection Linux branches with SQLWCHAR-aware byte-level widening/ narrowing loops (matches unixODBC's ansi_to_unicode_copy / unicode_to_ansi_copy). Makes the widechar-tests PR green on Linux. Remove the Linux-only SKIP guard added by the tests PR — the tests now run on every platform. Issue FirebirdSQL#287 Tier 1b is not fully closed by this: the connection MbsToWcs/WcsToMbs branch and non-ASCII handling remain open.
Please read again this explanation #289 (comment) or contains no bug. |
LPWSTR is WCHAR* = unsigned short* = SQLWCHAR* on both Windows and unixODBC/Linux, so `*(LPWSTR)ptr = L'\0'` writes 2 bytes via narrowing store, not 4 — no overrun. Thanks to @irodushka for the correction (FirebirdSQL#289 discussion_r3109004999). Keeping the replacement `unicodeString[len] = 0;` which he endorsed as more compact and understandable. The sibling comment in convUnicodeToString stays — that one is a real 4-byte write through an explicit `(wchar_t*)` cast.
Per FirebirdSQL#289 discussion with @irodushka: rather than keep ASAN running under continue-on-error, disable it outright until the Unicode rewrite lands (draft PR FirebirdSQL#291, tracked as FirebirdSQL#287 Tier 1b). Re-enable the matrix entry once FirebirdSQL#291 merges. Valgrind remains strict.
|
ok! |
|
@irodushka Agreed on the destructor case — One distinction worth separating on the sibling case in |
|
PR description updated to reflect the distinction. |
| #endif | ||
| else if ( wcString[length] != 0 ) | ||
| { | ||
| ptEndWC = (wchar_t*)&wcString[length]; |
There was a problem hiding this comment.
| ptEndWC = (wchar_t*)&wcString[length]; | |
| ptEndWC = wcString + length; |
| { | ||
| size_t bytesNeeded; | ||
| wchar_t *ptEndWC = NULL; | ||
| wchar_t saveWC; |
There was a problem hiding this comment.
| wchar_t saveWC; | |
| SQLWCHAR saveWC; |
| SQLCHAR * convUnicodeToString( SQLWCHAR *wcString, int length ) | ||
| { | ||
| size_t bytesNeeded; | ||
| wchar_t *ptEndWC = NULL; |
There was a problem hiding this comment.
| SQLWCHAR *ptEndWC = NULL; |
|
Yep. This place really looks like overrun. Frankly, two overruns - 1) and in the second place, at the end of the function: this's the third real write overrun)) unfixed) I think it's easier to rewrite this than to try to fix all those issues... |

Summary
Draft reference implementation against the widechar test suite in #290. Fixes the
MainUnicode.cppLinux branches that assumedsizeof(SQLWCHAR) == sizeof(wchar_t).What this PR does
ConvertingStringdestructor (output path, used bySQLGetDiagRecW/SQLErrorW): Linux branch now does a byte-level widening loop (SQLCHAR→SQLWCHAR) instead ofmbstowcs((wchar_t*)...)— the latter wrote UTF-32 into a UTF-16 buffer on Linux, truncatingHY000toHand smashing the stack on tight output buffers.convUnicodeToString(input path, used bySQLExecDirectW/SQLPrepareW): Linux branch does the inverse byte-narrowing loop instead ofwcstombs, and uses a localsqlwcharLenhelper instead ofwcslen((wchar_t*)...).lengthString = length / sizeof(SQLWCHAR): correct character count in the length-specified ctor.convUnicodeToString,*ptEndWC = L'\0'through awchar_t*was a genuine 4-byte write into a 2-byteSQLWCHARslot on Linux and is the real fix. The sibling*(LPWSTR)...in the destructor is a 2-byte narrowing store — not an overrun, per @irodushka in Add ASAN and Valgrind integration for CI test suite #289 (discussion_r3109004999) — so the replacement there is a style simplification, not a fix.Mirrors unixODBC's own
ansi_to_unicode_copy/unicode_to_ansi_copyapproach.Scope / Out of scope
connection->MbsToWcs/WcsToMbsbranch and non-ASCII (iconv) handling remain open.GTEST_SKIPguards added in Add widechar ODBC call tests (driving Unicode fix) #290, so the test suite runs on every platform.Context
Per @irodushka's plan in #289 comment 4279111183. Opened as draft so either:
(a) @irodushka approves and it lands,
(b) @irodushka pushes his own implementation on top, or
(c) @irodushka closes it and opens his own, using #290's tests as the acceptance criteria.
Test plan