Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpectedly changed the output code page when calling the ssh, ssh-keygen, scp, sftp command. #2027

Closed
3 tasks done
kemaruya opened this issue Feb 1, 2023 · 3 comments
Closed
3 tasks done

Comments

@kemaruya
Copy link

kemaruya commented Feb 1, 2023

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • Search the existing issues.

Steps to reproduce

Open command prompt and just run scp, sftp, ssh or ssh-keygen command.

Expected behavior

Keep previews output codepage between before execute scp, sftp, ssh or ssh-keygen command.

----
Microsoft Windows [Version 10.0.20348.169]
(c) Microsoft Corporation. All rights reserved.
 
C:\Users\Administrator>chcp
現在のコード ページ: 932 << Output Language is Japanese (CP932)
 
C:\Users\Administrator>ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
           [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
           [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
           [-i identity_file] [-J [user@]host[:port]] [-L address]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] destination [command]
 
C:\Users\Administrator>chcp
現在のコード ページ: 932 << Output Language is still Japanese (CP932)
 
C:\Users\Administrator>

Actual behavior

The output codepage has been changed to UTF8 (65001) after execute scp, sftp, ssh or ssh-keygen command.

----
Microsoft Windows [Version 10.0.20348.169]
(c) Microsoft Corporation. All rights reserved.
 
C:\Users\Administrator>chcp
現在のコード ページ: 932 << Output Language is Japanese (CP932)
 
C:\Users\Administrator>ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
           [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
           [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
           [-i identity_file] [-J [user@]host[:port]] [-L address]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] destination [command]
 
C:\Users\Administrator>chcp
Active code page: 932 << Output Language has been changed to UTF8 (CP65001)
 
C:\Users\Administrator>

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.20348.1
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.20348.1
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Version

9.1.0.0

Visuals

No response

@kemaruya
Copy link
Author

kemaruya commented Feb 1, 2023

@mgkuhn
Copy link

mgkuhn commented Feb 17, 2023

@NoMoreFood I first wondered if your calling SetConsoleOutputCP() (without saving the previous state first with GetConsoleOutputCP) in contrib/win32/win32compat/win32-utf8.c:msetlocale() isn't redundant, given that there also has been code to do all that in contrib/win32/win32compat/console.c:ConEnterRawMode() since PowerShell/openssh-portable@84e87be which does save and restore the prior state?

But then I realized may be not, as msetlocale() also gets called not only in ssh, but also in scp, sftp, ssh-keygen, which probably don't open a raw-mode console. I suspect msetlocale(), which gets called earlier, should query with GetConsoleOutputCP the prior state. It can't return that state, such that the caller could restore the prior state, e.g. in a function registered with atexit(), because the original utf8.c:msetlocale() is a wrapper around setlocale() that doesn't save any state, because on Unix setlocale doesn't change any state outside the current process, unlike SetConsoleOutputCP, which does affect the console state beyond the lifetime of the current process. So perhaps registering an atexit() function to restore the CP state from within contrib/win32/win32compat/win32-utf8.c:msetlocale() is a better thing to do here?

@kemaruya
Copy link
Author

I wrote a workaround to address the #2027 issue.
The compilation was successful and confirmed that the event could be avoided.
PowerShell/openssh-portable#666

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

No branches or pull requests

5 participants