Skip to content

Commit

Permalink
bugfix: no more crashes on invalid UTF-8 messages in database
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-mausch committed Jun 12, 2014
1 parent e014a47 commit f93473e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
3 changes: 3 additions & 0 deletions data/test-database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ INSERT INTO chat_list VALUES(15,'TestCaseLocation',0,NULL,NULL);
INSERT INTO chat_list VALUES(16,'TestCaseVideo',0,NULL,NULL);
INSERT INTO chat_list VALUES(17,'TestCaseContactVcard',0,NULL,NULL);
INSERT INTO chat_list VALUES(18,'TestCase&Ampersand',0,NULL,NULL);
INSERT INTO chat_list VALUES(19,'TestCaseInvalidUTF8',0,NULL,NULL);

INSERT INTO messages VALUES(1,-1,0,-1,-1,0,NULL,0,NULL,NULL,-1,-1,NULL,NULL,0,0,0.0,0.0,NULL,NULL,-1,-1,-1,-1,NULL,NULL);

Expand Down Expand Up @@ -59,4 +60,6 @@ INSERT INTO messages VALUES(801,'TestCaseLocation',0,'keyId-000801',0,0,NULL,139

INSERT INTO messages VALUES(900,'TestCase&Ampersand',0,'keyId-000900',0,0,'Test &test & test && test &&&&',1395999300000,NULL,NULL,0,0,NULL,NULL,0,1,0.0,0.0,NULL,'',1395999300000,-1,-1,-1,NULL,NULL);

INSERT INTO messages VALUES(1000,'TestCaseInvalidUTF8',0,'keyId-001000',0,0,X'48616861EDA0BD',1395999200000,NULL,NULL,0,0,NULL,NULL,0,1,0.0,0.0,NULL,'',1395999200000,-1,-1,-1,NULL,NULL);

COMMIT;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <sstream>
#include <windows.h>

#include "ChatControlMessageText.h"
Expand Down Expand Up @@ -50,34 +51,44 @@ bool ChatControlMessageText::isSmiley(int character)
void ChatControlMessageText::splitMessage(WhatsappMessage &message)
{
std::string messageString = message.getData();
int lastSplit = 0;
for (std::string::iterator it = messageString.begin(); it != messageString.end();)
{
bool begin = (it - messageString.begin()) == lastSplit;
std::string::iterator before = it;
int character = utf8::next(it, messageString.end());

if (isSmiley(character))
try
{
int lastSplit = 0;
for (std::string::iterator it = messageString.begin(); it != messageString.end();)
{
if (!begin)
bool begin = (it - messageString.begin()) == lastSplit;
std::string::iterator before = it;
int character = utf8::next(it, messageString.end());

if (isSmiley(character))
{
int start = lastSplit;
int end = before - messageString.begin();
std::string leftPart = messageString.substr(start, end - start);
elements.push_back(new ChatControlMessageTextElement(leftPart));
}
if (!begin)
{
int start = lastSplit;
int end = before - messageString.begin();
std::string leftPart = messageString.substr(start, end - start);
elements.push_back(new ChatControlMessageTextElement(leftPart));
}

elements.push_back(new ChatControlMessageTextElement(character));
elements.push_back(new ChatControlMessageTextElement(character));

lastSplit = (it - messageString.begin());
lastSplit = (it - messageString.begin());
}
}
}

int length = messageString.length();
if (lastSplit < length)
int length = messageString.length();
if (lastSplit < length)
{
std::string part = messageString.substr(lastSplit, length);
elements.push_back(new ChatControlMessageTextElement(part));
}
}
catch (utf8::exception &exception)
{
std::string part = messageString.substr(lastSplit, length);
elements.push_back(new ChatControlMessageTextElement(part));
std::stringstream invalidDataString;
invalidDataString << "[INVALID DATA: " << messageString << "]";
elements.push_back(new ChatControlMessageTextElement(invalidDataString.str()));
}
}

Expand Down

0 comments on commit f93473e

Please sign in to comment.