onnxruntime
e227e8a6 - Fix narrow-to-wide string conversion bugs in dll_load_error.cc (#27777)

Commit
87 days ago
Fix narrow-to-wide string conversion bugs in dll_load_error.cc (#27777) ### Description Fixes the DLL import-name conversion logic in `DetermineLoadLibraryError()` in `onnxruntime/core/platform/windows/dll_load_error.cc`. The changes: - Replace `LoadLibrary(dll_name)` with `LoadLibraryW(...)` so the code no longer passes a `const char*` import-table name to the wide-character Windows API under `UNICODE` builds. - Replace the naive `char*` to `std::wstring` construction with `MultiByteToWideChar(CP_ACP, ...)`, which is appropriate for ANSI PE import names. - Handle conversion failures by skipping the import entry instead of continuing with an invalid or empty converted name. - Fix the follow-up buffer sizing issue in the conversion path by allocating space for the terminating null, checking the conversion result, and then resizing the `std::wstring` back down to exclude the terminator. These updates keep the change focused on the Windows DLL dependency diagnostic path while ensuring the new conversion logic is memory-safe. ### Motivation and Context The original code mixed narrow and wide string handling in a Windows-specific path that reports missing dependent DLLs. Under standard `UNICODE` builds, that could cause the dependency lookup to fail because a narrow import name was effectively being passed to a wide-character API. The follow-up review also identified an off-by-one write risk in the first version of the conversion fix: `MultiByteToWideChar(..., -1, ...)` returns a length including the null terminator, so the destination buffer must reserve space for that terminator before conversion. This update corrects that issue and preserves the intended diagnostic behavior. ### Test ``` "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" git checkout tlwu/fix-dll-name-string-conversion-test cd 测试目录 test_dll_load.cmd ``` Output is like ``` ====== Caught expected error ====== [ONNXRuntimeError] : 1 : FAIL : Error loading "E:\git\onnxruntime\测试目录\test_main.dll" which depends on "missing_dep.dll" which is missing. (Error 126: "The specified module could not be found.") =================================== ✅ SUCCESS: The fix works! 'missing_dep.dll' is correctly identified and reported in the error message. ``` --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Author
Parents
Loading