Skip to content

Commit b1180b3

Browse files
committed
CLI: Restore the original codepage on windows
* Fixes #11465
1 parent 9a63e80 commit b1180b3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/cli/Utils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ namespace Utils
4343
QTextStream STDIN;
4444
QTextStream DEVNULL;
4545

46+
#ifdef Q_OS_WIN
47+
UINT origCodePage;
48+
UINT origOutputCodePage;
49+
#endif
50+
4651
void setDefaultTextStreams()
4752
{
4853
auto fd = new QFile();
@@ -66,13 +71,24 @@ namespace Utils
6671
DEVNULL.setDevice(fd);
6772

6873
#ifdef Q_OS_WIN
74+
origCodePage = GetConsoleCP();
75+
origOutputCodePage = GetConsoleOutputCP();
76+
6977
// On Windows, we ask via keepassxc-cli.exe.manifest to use UTF-8,
7078
// but the console code-page isn't automatically changed to match.
7179
SetConsoleCP(GetACP());
7280
SetConsoleOutputCP(GetACP());
7381
#endif
7482
}
7583

84+
void resetTextStreams()
85+
{
86+
#ifdef Q_OS_WIN
87+
SetConsoleCP(origCodePage);
88+
SetConsoleOutputCP(origOutputCodePage);
89+
#endif
90+
}
91+
7692
void setStdinEcho(bool enable = true)
7793
{
7894
#ifdef Q_OS_WIN

src/cli/Utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace Utils
3939
static const QStringList EntryFieldNames(QStringList() << UuidFieldName << TagsFieldName);
4040

4141
void setDefaultTextStreams();
42+
void resetTextStreams();
4243

4344
void setStdinEcho(bool enable);
4445
bool loadFileKey(const QString& path, QSharedPointer<FileKey>& fileKey);

src/cli/keepassxc-cli.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ int main(int argc, char** argv)
181181

182182
QCoreApplication app(argc, argv);
183183
QCoreApplication::setApplicationVersion(KEEPASSXC_VERSION);
184+
// Cleanup code pages after cli exits
185+
QObject::connect(&app, &QCoreApplication::destroyed, &app, [] { Utils::resetTextStreams(); });
184186

185187
Bootstrap::bootstrap(config()->get(Config::GUI_Language).toString());
186188
Utils::setDefaultTextStreams();
@@ -218,7 +220,9 @@ int main(int argc, char** argv)
218220
// Switch to parser.showVersion() when available (QT 5.4).
219221
out << KEEPASSXC_VERSION << Qt::endl;
220222
return EXIT_SUCCESS;
221-
} else if (parser.isSet(debugInfoOption)) {
223+
}
224+
225+
if (parser.isSet(debugInfoOption)) {
222226
QString debugInfo = Tools::debugInfo().append("\n").append(Crypto::debugInfo());
223227
out << debugInfo << Qt::endl;
224228
return EXIT_SUCCESS;

src/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ int main(int argc, char** argv)
174174
return EXIT_FAILURE;
175175
}
176176

177+
Utils::setDefaultTextStreams();
178+
177179
// Apply the configured theme before creating any GUI elements
178180
app.applyTheme();
179181

@@ -192,9 +194,6 @@ int main(int argc, char** argv)
192194
mainWindow.setAllowScreenCapture(parser.isSet(allowScreenCaptureOption));
193195

194196
const bool pwstdin = parser.isSet(pwstdinOption);
195-
if (!fileNames.isEmpty() && pwstdin) {
196-
Utils::setDefaultTextStreams();
197-
}
198197
for (const QString& filename : fileNames) {
199198
QString password;
200199
if (pwstdin) {
@@ -228,5 +227,7 @@ int main(int argc, char** argv)
228227
__lsan_disable();
229228
#endif
230229

230+
Utils::resetTextStreams();
231+
231232
return exitCode;
232233
}

0 commit comments

Comments
 (0)