Restore the console output code page on exit#77
Closed
enesilhaydin wants to merge 1 commit into
Closed
Conversation
set_console_cp_utf8() relied on a Drop guard to restore the original code page, but main always terminates via process::exit, which skips destructors. Register the restore as an atexit handler instead, since the CRT runs those during exit().
Contributor
Author
|
@microsoft-github-policy-service agree |
Member
That's only true on Linux and macOS and some other *nix. On Windows the stdlib calls |
Member
|
I have given you credit in #65, because it adopts the atexit() approach for ntsort. |
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.
The console output code page is switched to UTF-8 at startup but never restored.
set_console_cp_utf8()returns aRestoreConsoleCpwhoseDropimpl is supposed to put the original code page back. That destructor never runs:mainends every path withprocess::exit(...), andstd::process::exitterminates the process without unwinding the stack, so the guard is never dropped.The practical effect is that running any util once leaves the console on code page 65001 for the rest of the session. Programs run afterwards that emit OEM/ANSI-encoded output then render as mojibake until the user runs
chcpmanually.This swaps the
Dropguard for anatexithandler.std::process::exitcalls the CRTexit(), which does runatexithandlers, so the original code page is restored on every exit path. The handler uses an atomic swap so it only restores once, andset_console_cp_utf8()is a no-op when the page is already UTF-8 or can't be queried.