Skip to content

Commit

Permalink
Make sure that all strings in the IDO database are UTF8-encoded
Browse files Browse the repository at this point in the history
fixes #10554
  • Loading branch information
gunnarbeutner committed Dec 10, 2015
1 parent a36dd6e commit fba0b49
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
36 changes: 36 additions & 0 deletions lib/base/utility.cpp
Expand Up @@ -1654,3 +1654,39 @@ String Utility::GetPlatformArchitecture(void)
return UnameHelper('m');
#endif /* _WIN32 */
}

String Utility::ValidateUTF8(const String& input)
{
String output;
size_t length = input.GetLength();

for (size_t i = 0; i < length; i++) {
if ((input[i] & 0x80) == 0) {
output += input[i];
continue;
}

if ((input[i] & 0xE0) == 0xC0 && length > i + 1 &&
(input[i + 1] & 0xC0) == 0x80) {
output += input[i];
output += input[i + 1];
i++;
continue;
}

if ((input[i] & 0xF0) == 0xE0 && length > i + 2 &&
(input[i + 1] & 0xC0) == 0x80 && (input[i + 2] & 0xC0) == 0x80) {
output += input[i];
output += input[i + 1];
output += input[i + 2];
i += 2;
continue;
}

output += '\xEF';
output += '\xBF';
output += '\xBD';
}

return output;
}
2 changes: 2 additions & 0 deletions lib/base/utility.hpp
Expand Up @@ -139,6 +139,8 @@ class I2_BASE_API Utility
static String GetPlatformVersion(void);
static String GetPlatformArchitecture(void);

static String ValidateUTF8(const String& input);

private:
Utility(void);
static void CollectPaths(const String& path, std::vector<String>& paths);
Expand Down
8 changes: 5 additions & 3 deletions lib/db_ido_mysql/idomysqlconnection.cpp
Expand Up @@ -574,10 +574,12 @@ String IdoMysqlConnection::Escape(const String& s)
{
AssertOnWorkQueue();

size_t length = s.GetLength();
char *to = new char[s.GetLength() * 2 + 1];
String utf8s = Utility::ValidateUTF8(s);

mysql_real_escape_string(&m_Connection, to, s.CStr(), length);
size_t length = utf8s.GetLength();
char *to = new char[utf8s.GetLength() * 2 + 1];

mysql_real_escape_string(&m_Connection, to, utf8s.CStr(), length);

String result = String(to);

Expand Down
8 changes: 5 additions & 3 deletions lib/db_ido_pgsql/idopgsqlconnection.cpp
Expand Up @@ -464,10 +464,12 @@ String IdoPgsqlConnection::Escape(const String& s)
{
AssertOnWorkQueue();

size_t length = s.GetLength();
char *to = new char[s.GetLength() * 2 + 1];
String utf8s = Utility::ValidateUTF8(s);

PQescapeStringConn(m_Connection, to, s.CStr(), length, NULL);
size_t length = utf8s.GetLength();
char *to = new char[utf8s.GetLength() * 2 + 1];

PQescapeStringConn(m_Connection, to, utf8s.CStr(), length, NULL);

String result = String(to);

Expand Down

0 comments on commit fba0b49

Please sign in to comment.