Skip to content

Commit

Permalink
utils: Convert Windows args to utf-8 string
Browse files Browse the repository at this point in the history
  • Loading branch information
ken2812221 authored and furszy committed Aug 1, 2021
1 parent e8cfa6e commit be89860
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/pivx-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "utilstrencodings.h"

#include <stdio.h>
#include <tuple>

#include <event2/event.h>
#include <event2/http.h>
Expand Down Expand Up @@ -324,6 +325,10 @@ int CommandLineRPC(int argc, char* argv[])

int main(int argc, char* argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();
if (!SetupNetworking()) {
fprintf(stderr, "Error: Initializing networking failed\n");
Expand Down
4 changes: 4 additions & 0 deletions src/pivxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ bool AppInit(int argc, char* argv[])

int main(int argc, char* argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();

// Connect pivxd signal handlers
Expand Down
4 changes: 4 additions & 0 deletions src/qt/pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ WId BitcoinApplication::getMainWinId() const
#ifndef BITCOIN_QT_TEST
int main(int argc, char* argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();

/// 1. Parse command-line options. These take precedence over anything else.
Expand Down
33 changes: 33 additions & 0 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include <codecvt>

#include <io.h> /* for _commit */
#include <shellapi.h>
#include <shlobj.h>
#endif

Expand Down Expand Up @@ -1066,6 +1067,10 @@ void SetupEnvironment()
} catch (const std::runtime_error&) {
setenv("LC_ALL", "C", 1);
}
#elif defined(WIN32)
// Set the default input/output charset is utf-8
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif
// The path locale is lazy initialized and to avoid deinitialization errors
// in multithreading environments, it is set explicitly by the main thread.
Expand Down Expand Up @@ -1123,3 +1128,31 @@ int ScheduleBatchPriority(void)
return 1;
#endif
}

namespace util {
#ifdef WIN32
WinCmdLineArgs::WinCmdLineArgs()
{
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt;
argv = new char*[argc];
args.resize(argc);
for (int i = 0; i < argc; i++) {
args[i] = utf8_cvt.to_bytes(wargv[i]);
argv[i] = &*args[i].begin();
}
LocalFree(wargv);
}

WinCmdLineArgs::~WinCmdLineArgs()
{
delete[] argv;
}

std::pair<int, char**> WinCmdLineArgs::get()
{
return std::make_pair(argc, argv);
}
#endif
} // namespace util

20 changes: 20 additions & 0 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include <boost/signals2/signal.hpp>
Expand Down Expand Up @@ -301,4 +302,23 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
*/
int ScheduleBatchPriority(void);

namespace util {

#ifdef WIN32
class WinCmdLineArgs
{
public:
WinCmdLineArgs();
~WinCmdLineArgs();
std::pair<int, char**> get();

private:
int argc;
char** argv;
std::vector<std::string> args;
};
#endif

} // namespace util

#endif // BITCOIN_UTIL_SYSTEM_H
2 changes: 1 addition & 1 deletion test/functional/feature_uacomment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run_test(self):
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected, match=ErrorMatch.FULL_REGEX)

self.log.info("test -uacomment unsafe characters")
for unsafe_char in ['/', ':', '(', ')']:
for unsafe_char in ['/', ':', '(', ')', '₿', '🏃']:
expected = r"Error: User Agent comment \(" + re.escape(unsafe_char) + r"\) contains unsafe characters."
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected, match=ErrorMatch.FULL_REGEX)

Expand Down

0 comments on commit be89860

Please sign in to comment.