From 7effb1a5f64b4334e82a2a0f8eb1c89af2d9a118 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Tue, 9 Jun 2015 14:44:40 +0200
Subject: [PATCH] [Messaging] Fixed memory handling in
convertPlatformShortMessageToObject().
Prevent CID: 402406
[Verification] Pass rate should not change.
Change-Id: Ib2ac577e0f7fc62e7b0d52b47ea26e38f65798cb
Signed-off-by: Pawel Andruszkiewicz
---
src/messaging/message.cc | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/src/messaging/message.cc b/src/messaging/message.cc
index 975f678..6405202 100755
--- a/src/messaging/message.cc
+++ b/src/messaging/message.cc
@@ -1336,14 +1336,14 @@ PlatformResult Message::setMMSBodyAndAttachmentsFromStruct(Message* message,
PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Message** result_message){
LoggerD("Entered");
- Message *message = nullptr;
+ std::unique_ptr message;
int infoInt;
bool infoBool;
char infoStr[MAX_ADDRESS_VAL_LEN + 1];
//get type
msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &infoInt);
if (infoInt == MSG_TYPE_SMS) {
- message = new MessageSMS();
+ message = std::unique_ptr(new MessageSMS());
// get SMS body
std::shared_ptr body(new MessageBody());
char msgInfoStr[MAX_MSG_TEXT_LEN + 1];
@@ -1355,13 +1355,12 @@ PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Me
PlatformResult ret = message->getSMSRecipientsFromStruct(msg, &recp_list);
if (ret.IsError()) {
LoggerE("failed to get SMS recipients from struct");
- if (message) delete message;
return ret;
}
message->setTO(recp_list);
} else if (infoInt == MSG_TYPE_MMS) {
- message = new MessageMMS();
+ message = std::unique_ptr(new MessageMMS());
// get MMS body
msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &infoInt);
@@ -1403,21 +1402,18 @@ PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Me
PlatformResult ret = getMMSRecipientsFromStruct(msg, MSG_RECIPIENTS_TYPE_TO, &recp_list);
if (ret.IsError()) {
LoggerE("failed to get MMS recipients from struct");
- if (message) delete message;
return ret;
}
message->setTO(recp_list);
ret = getMMSRecipientsFromStruct(msg, MSG_RECIPIENTS_TYPE_CC, &recp_list);
if (ret.IsError()) {
LoggerE("failed to get MMS recipients from struct");
- if (message) delete message;
return ret;
}
message->setCC(recp_list);
ret = getMMSRecipientsFromStruct(msg, MSG_RECIPIENTS_TYPE_BCC, &recp_list);
if (ret.IsError()) {
LoggerE("failed to get MMS recipients from struct");
- if (message) delete message;
return ret;
}
message->setBCC(recp_list);
@@ -1426,10 +1422,9 @@ PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Me
msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, infoStr, MAX_SUBJECT_LEN);
message->setSubject(infoStr);
//set attachments
- ret = setMMSBodyAndAttachmentsFromStruct(message, msg);
+ ret = setMMSBodyAndAttachmentsFromStruct(message.get(), msg);
if (ret.IsError()) {
LoggerE("failed to set body attachments from struct");
- if (message) delete message;
return ret;
}
} else {
@@ -1452,10 +1447,10 @@ PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Me
// get from
const std::string& from = Message::getShortMsgSenderFromStruct(msg);
message->setFrom(from);
- LoggerD("Message(%p) from is: %s", message, message->getFrom().c_str());
+ LoggerD("Message(%p) from is: %s", message.get(), message->getFrom().c_str());
// get if is in response
msg_get_int_value(msg, MSG_MESSAGE_DIRECTION_INT, &infoInt);
- LoggerD("Message(%p) direction is: %d", message, infoInt);
+ LoggerD("Message(%p) direction is: %d", message.get(), infoInt);
message->setInResponseTo(infoInt);
// get is read
msg_get_bool_value(msg, MSG_MESSAGE_READ_BOOL, &infoBool);
@@ -1517,7 +1512,7 @@ PlatformResult Message::convertPlatformShortMessageToObject(msg_struct_t msg, Me
}
LoggerD("End");
- *result_message = message;
+ *result_message = message.release(); // release ownership
return PlatformResult(ErrorCode::NO_ERROR);
}
--
2.7.4