fix: use _wfopen for non-ASCII log paths on Windows#16
Open
Windsland52 wants to merge 2 commits into
Open
Conversation
When the log directory path contains non-ASCII characters (e.g. CJK), path.string() converts the internal wstring to narrow using the system active code page (ACP), not UTF-8. On Chinese Windows (ACP=936/GBK), this produces garbled bytes that cause fopen to throw an exception. Using _wfopen with the wide string path directly preserves the characters regardless of ACP setting.
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并且留下了一些整体性的反馈:
- 建议在调用
_fileno/SetHandleInformation之前先对file_ptr进行空指针检查,以避免在_wfopen失败时出现未定义行为。 - 如果目标编译器是 MSVC,你可能希望使用
_wfopen_s替代_wfopen,以避免 CRT 的“不安全”警告,并让错误处理更加明确。
给 AI 代理的提示
Please address the comments from this code review:
## Overall Comments
- Consider adding a null check for `file_ptr` before calling `_fileno`/`SetHandleInformation` to avoid undefined behavior if `_wfopen` fails.
- If you are targeting MSVC, you may want to use `_wfopen_s` instead of `_wfopen` to avoid the CRT "insecure" warnings and make error handling more explicit.
## Individual Comments
### Comment 1
<location path="source/Logger/Logger.cpp" line_range="200-202" />
<code_context>
// https://stackoverflow.com/questions/55513974/controlling-inheritability-of-file-handles-created-by-c-stdfstream-in-window
- std::string str_log_path = log_path_.string();
- FILE* file_ptr = fopen(str_log_path.c_str(), append ? "a" : "w");
+ FILE* file_ptr = _wfopen(log_path_.c_str(), append ? L"a" : L"w");
SetHandleInformation((HANDLE)_get_osfhandle(_fileno(file_ptr)), HANDLE_FLAG_INHERIT, 0);
ofs_ = std::ofstream(file_ptr);
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle _wfopen failure before using the FILE* to avoid undefined behavior.
If `_wfopen` returns `nullptr`, passing `file_ptr` to `SetHandleInformation` and constructing `std::ofstream` from it is undefined behavior. Add a null check after `_wfopen` and return/handle the error before calling `SetHandleInformation` or initializing `ofs_`.
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续的审查。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- Consider adding a null check for
file_ptrbefore calling_fileno/SetHandleInformationto avoid undefined behavior if_wfopenfails. - If you are targeting MSVC, you may want to use
_wfopen_sinstead of_wfopento avoid the CRT "insecure" warnings and make error handling more explicit.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider adding a null check for `file_ptr` before calling `_fileno`/`SetHandleInformation` to avoid undefined behavior if `_wfopen` fails.
- If you are targeting MSVC, you may want to use `_wfopen_s` instead of `_wfopen` to avoid the CRT "insecure" warnings and make error handling more explicit.
## Individual Comments
### Comment 1
<location path="source/Logger/Logger.cpp" line_range="200-202" />
<code_context>
// https://stackoverflow.com/questions/55513974/controlling-inheritability-of-file-handles-created-by-c-stdfstream-in-window
- std::string str_log_path = log_path_.string();
- FILE* file_ptr = fopen(str_log_path.c_str(), append ? "a" : "w");
+ FILE* file_ptr = _wfopen(log_path_.c_str(), append ? L"a" : L"w");
SetHandleInformation((HANDLE)_get_osfhandle(_fileno(file_ptr)), HANDLE_FLAG_INHERIT, 0);
ofs_ = std::ofstream(file_ptr);
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle _wfopen failure before using the FILE* to avoid undefined behavior.
If `_wfopen` returns `nullptr`, passing `file_ptr` to `SetHandleInformation` and constructing `std::ofstream` from it is undefined behavior. Add a null check after `_wfopen` and return/handle the error before calling `SetHandleInformation` or initializing `ofs_`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Member
|
测过没 |
Member
Author
|
没 |
Member
Author
|
测了下没问题 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the log directory path contains non-ASCII characters (e.g. CJK),
path.string() converts the internal wstring to narrow using the system
active code page (ACP), not UTF-8. On Chinese Windows (ACP=936/GBK),
this produces garbled bytes that cause fopen to throw an exception.
Using _wfopen with the wide string path directly preserves the characters
regardless of ACP setting.
Related: MaaFramework#1309
Summary by Sourcery
错误修复:
_wfopen而不是fopen打开日志文件,防止在 Windows 上当日志目录路径包含非 ASCII 字符时记录器初始化失败。Original summary in English
Summary by Sourcery
Bug Fixes: