From f93473ea96012d47e7f2905585a0742113802426 Mon Sep 17 00:00:00 2001 From: neonew Date: Thu, 12 Jun 2014 13:13:35 +0200 Subject: [PATCH] bugfix: no more crashes on invalid UTF-8 messages in database --- data/test-database.sql | 3 ++ .../Messages/ChatControlMessageText.cpp | 51 +++++++++++-------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/data/test-database.sql b/data/test-database.sql index 4397947..1cd58d3 100644 --- a/data/test-database.sql +++ b/data/test-database.sql @@ -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); @@ -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; diff --git a/source/Platforms/Win32/GUI/ChatControl/Elements/Messages/ChatControlMessageText.cpp b/source/Platforms/Win32/GUI/ChatControl/Elements/Messages/ChatControlMessageText.cpp index b57589c..fd64ca7 100644 --- a/source/Platforms/Win32/GUI/ChatControl/Elements/Messages/ChatControlMessageText.cpp +++ b/source/Platforms/Win32/GUI/ChatControl/Elements/Messages/ChatControlMessageText.cpp @@ -1,3 +1,4 @@ +#include #include #include "ChatControlMessageText.h" @@ -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())); } }