Skip to content

fix: use _wfopen for non-ASCII log paths on Windows#16

Open
Windsland52 wants to merge 2 commits into
masterfrom
fix/wide-path-logging
Open

fix: use _wfopen for non-ASCII log paths on Windows#16
Windsland52 wants to merge 2 commits into
masterfrom
fix/wide-path-logging

Conversation

@Windsland52
Copy link
Copy Markdown
Member

@Windsland52 Windsland52 commented May 10, 2026

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:

  • Prevent logger initialization from failing on Windows when the log directory path contains non-ASCII characters by opening log files with _wfopen instead of fopen.

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.
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续的审查。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread source/Logger/Logger.cpp
@MistEO
Copy link
Copy Markdown
Member

MistEO commented May 11, 2026

测过没

@Windsland52
Copy link
Copy Markdown
Member Author

@Windsland52
Copy link
Copy Markdown
Member Author

测了下没问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants