From b4e18d46cee9100e282bf8df865af4a7fac2a340 Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Mon, 27 Oct 2014 10:49:06 +0100 Subject: [PATCH] Fix crash when the received message is invalid UTF8 --- Classes/LinphoneUI/UIChatRoomCell.m | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Classes/LinphoneUI/UIChatRoomCell.m b/Classes/LinphoneUI/UIChatRoomCell.m index eea11e183..cb681f225 100644 --- a/Classes/LinphoneUI/UIChatRoomCell.m +++ b/Classes/LinphoneUI/UIChatRoomCell.m @@ -105,6 +105,18 @@ static UIFont *CELL_FONT = nil; } ++ (NSString*)decodeTextMessage:(const char*)text { + NSString* decoded = [NSString stringWithUTF8String:text]; + if( decoded == nil ){ + // couldn't decode the string as UTF8, do a lossy conversion + decoded = [NSString stringWithCString:text encoding:NSASCIIStringEncoding]; + if( decoded == nil ){ + decoded = @"(invalid string)"; + } + } + return decoded; +} + - (void)update { if(chat == nil) { [LinphoneLogger logc:LinphoneLoggerWarning format:"Cannot update chat room cell: null chat"]; @@ -151,15 +163,19 @@ static UIFont *CELL_FONT = nil; } else { // simple text message [messageText setHidden:FALSE]; - if (text ){ + if ( text ){ + NSString* nstext = [UIChatRoomCell decodeTextMessage:text]; + /* We need to use an attributed string here so that data detector don't mess * with the text style. See http://stackoverflow.com/a/20669356 */ + NSAttributedString* attr_text = [[NSAttributedString alloc] - initWithString:[NSString stringWithUTF8String:text] + initWithString:nstext attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0], NSForegroundColorAttributeName:[UIColor darkGrayColor]}]; messageText.attributedText = attr_text; [attr_text release]; + } else { messageText.text = @""; } @@ -225,7 +241,7 @@ static UIFont *CELL_FONT = nil; CGSize messageSize; const char* url = linphone_chat_message_get_external_body_url(chat); const char* text = linphone_chat_message_get_text(chat); - NSString* messageText = text ? [NSString stringWithUTF8String:text] : @""; + NSString* messageText = text ? [UIChatRoomCell decodeTextMessage:text] : @""; if(url == nil) { if(CELL_FONT == nil) { CELL_FONT = [UIFont systemFontOfSize:CELL_FONT_SIZE];