|
8 | 8 | #include "connection/connection_pool.h" |
9 | 9 |
|
10 | 10 | #include <cstdint> |
| 11 | +#include <cstring> // For std::memcpy |
11 | 12 | #include <iomanip> // std::setw, std::setfill |
12 | 13 | #include <iostream> |
13 | 14 | #include <utility> // std::forward |
@@ -2463,10 +2464,11 @@ static py::object FetchLobColumnData(SQLHSTMT hStmt, |
2463 | 2464 | } else { |
2464 | 2465 | // Wide characters |
2465 | 2466 | size_t wcharSize = sizeof(SQLWCHAR); |
2466 | | - if (bytesRead >= wcharSize) { |
2467 | | - auto sqlwBuf = reinterpret_cast<const SQLWCHAR*>(chunk.data()); |
| 2467 | + if (bytesRead >= wcharSize && (bytesRead % wcharSize == 0)) { |
2468 | 2468 | size_t wcharCount = bytesRead / wcharSize; |
2469 | | - while (wcharCount > 0 && sqlwBuf[wcharCount - 1] == 0) { |
| 2469 | + std::vector<SQLWCHAR> alignedBuf(wcharCount); |
| 2470 | + std::memcpy(alignedBuf.data(), chunk.data(), bytesRead); |
| 2471 | + while (wcharCount > 0 && alignedBuf[wcharCount - 1] == 0) { |
2470 | 2472 | --wcharCount; |
2471 | 2473 | bytesRead -= wcharSize; |
2472 | 2474 | } |
@@ -2495,14 +2497,18 @@ static py::object FetchLobColumnData(SQLHSTMT hStmt, |
2495 | 2497 | } |
2496 | 2498 | if (isWideChar) { |
2497 | 2499 | #if defined(_WIN32) |
2498 | | - std::wstring wstr(reinterpret_cast<const wchar_t*>(buffer.data()), buffer.size() / sizeof(wchar_t)); |
| 2500 | + size_t wcharCount = buffer.size() / sizeof(wchar_t); |
| 2501 | + std::vector<wchar_t> alignedBuf(wcharCount); |
| 2502 | + std::memcpy(alignedBuf.data(), buffer.data(), buffer.size()); |
| 2503 | + std::wstring wstr(alignedBuf.data(), wcharCount); |
2499 | 2504 | std::string utf8str = WideToUTF8(wstr); |
2500 | 2505 | return py::str(utf8str); |
2501 | 2506 | #else |
2502 | 2507 | // Linux/macOS handling |
2503 | 2508 | size_t wcharCount = buffer.size() / sizeof(SQLWCHAR); |
2504 | | - const SQLWCHAR* sqlwBuf = reinterpret_cast<const SQLWCHAR*>(buffer.data()); |
2505 | | - std::wstring wstr = SQLWCHARToWString(sqlwBuf, wcharCount); |
| 2509 | + std::vector<SQLWCHAR> alignedBuf(wcharCount); |
| 2510 | + std::memcpy(alignedBuf.data(), buffer.data(), buffer.size()); |
| 2511 | + std::wstring wstr = SQLWCHARToWString(alignedBuf.data(), wcharCount); |
2506 | 2512 | std::string utf8str = WideToUTF8(wstr); |
2507 | 2513 | return py::str(utf8str); |
2508 | 2514 | #endif |
@@ -2623,8 +2629,7 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p |
2623 | 2629 | uint64_t numCharsInData = dataLen / sizeof(SQLWCHAR); |
2624 | 2630 | if (numCharsInData < dataBuffer.size()) { |
2625 | 2631 | #if defined(__APPLE__) || defined(__linux__) |
2626 | | - const SQLWCHAR* sqlwBuf = reinterpret_cast<const SQLWCHAR*>(dataBuffer.data()); |
2627 | | - std::wstring wstr = SQLWCHARToWString(sqlwBuf, numCharsInData); |
| 2632 | + std::wstring wstr = SQLWCHARToWString(dataBuffer.data(), numCharsInData); |
2628 | 2633 | std::string utf8str = WideToUTF8(wstr); |
2629 | 2634 | row.append(py::str(utf8str)); |
2630 | 2635 | #else |
|
0 commit comments