-
Notifications
You must be signed in to change notification settings - Fork 48
Fix CodeQL SM02986: char* to wchar_t* cast warning in PythonParam #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -345,8 +345,12 @@ void PythonStringParam<CharType>::RetrieveValueAndStrLenInd(bp::object mainNames | |
| char *utf16str = PyBytes_AsString(PyUnicode_AsUTF16String(tempObj.ptr())); | ||
|
|
||
| // Reinterpret the bytes as wchar_t *, which we will return. | ||
| // The buffer contains UTF-16 code units in native byte order. The string always starts with a BOM mark. | ||
| // (https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsUTF16String) | ||
| // We treat it as an array of 16-bit code units (CharType expected to be the size of wchar_t). | ||
| // | ||
| CharType *wData = reinterpret_cast<CharType *>(utf16str); | ||
| static_assert(sizeof(CharType) == sizeof(wchar_t), "CharType must match wchar_t size for UTF-16 reinterpretation."); | ||
| CharType *wData = reinterpret_cast<CharType *>(utf16str); // CodeQL [SM02986]: The buffer is properly aligned (divisible by 2), already contains real UTF-16 data (SQL NVARCHAR), and we know its exact length (not relying on null termination); so treating it as wchar_t* is safe. | ||
|
Comment on lines
+350
to
+353
|
||
|
|
||
| // Ignore 2 byte BOM at front of wData that was added by PyUnicode_AsUTF16String | ||
| // | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyUnicode_AsUTF16Stringreturns a new reference; calling it inline insidePyBytes_AsString(...)leaks thatPyObject*and also prevents any error handling. IfPyUnicode_AsUTF16Stringfails (e.g.,MemoryError) it returnsnullptr, andPyBytes_AsString(nullptr)is undefined/crash. Store the returned bytes object in a temporary, validate it’s non-null (and thatPyBytes_AsStringsucceeds), copy out the needed data, thenPy_DECREFthe temporary.