Skip to content

Commit

Permalink
Fix storing UTF-8 configuration values on Windows
Browse files Browse the repository at this point in the history
std::string values were accidentally converted to wxString using current
charset instead of UTF-8, eventually resulting in double-encoded UTF-8.

Fix by explicitly treating narrow strings as UTF-8 in both directions.

The actual fix is using str::to_wx(value) instead of wxString(value)
in Config::Write(key, std::string); the rest of the changes is just
for consistency.
  • Loading branch information
vslavik committed Apr 4, 2024
1 parent 3d3ac4d commit a07d4cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "configuration.h"

#include "str_helpers.h"

#include <wx/config.h>
#include <wx/thread.h>

Expand Down Expand Up @@ -220,14 +222,14 @@ bool Config::Read(const std::string& key, std::string *out)
wxString s;
if (!wxConfig::Get()->Read(key, &s))
return false;
*out = s.utf8_string();
*out = str::to_utf8(s);
return true;
}

void Config::Write(const std::string& key, const std::string& value)
{
CfgLock lock;
wxConfig::Get()->Write(key, wxString(value));
wxConfig::Get()->Write(key, str::to_wx(value));
}

bool Config::Read(const std::string& key, std::wstring *out)
Expand All @@ -237,14 +239,14 @@ bool Config::Read(const std::string& key, std::wstring *out)
wxString s;
if (!wxConfig::Get()->Read(key, &s))
return false;
*out = s.ToStdWstring();
*out = str::to_wstring(s);
return true;
}

void Config::Write(const std::string& key, const std::wstring& value)
{
CfgLock lock;
wxConfig::Get()->Write(key, wxString(value));
wxConfig::Get()->Write(key, str::to_wx(value));
}

bool Config::Read(const std::string& key, bool *out)
Expand Down
5 changes: 5 additions & 0 deletions src/str_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ inline wxString to_wx(const std::string& utf8)
return wxString::FromUTF8(utf8.c_str());
}

inline wxString to_wx(const std::wstring& str)
{
return wxString(str);
}

#if defined(__cplusplus) && defined(__OBJC__)

inline NSString *to_NS(const wxString& str)
Expand Down

0 comments on commit a07d4cf

Please sign in to comment.