From 7bdac0b7a3e08d35e3299f2d9a47812ad662f328 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Fri, 15 Jan 2016 11:06:50 +0100
Subject: [PATCH] [MessagingMMS] Fixed detection of non-existent attachments.
Passing a non-existent attachment file path to msg-service may crash it.
In such case, return with error message.
Change-Id: Ic9034788bb6057456a93cd4a80af3118f67ca27b
Signed-off-by: Pawel Andruszkiewicz
---
src/messaging/message.cc | 32 +++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)
diff --git a/src/messaging/message.cc b/src/messaging/message.cc
index 4444387d..d438dbcb 100755
--- a/src/messaging/message.cc
+++ b/src/messaging/message.cc
@@ -784,28 +784,38 @@ PlatformResult Message::addMMSBodyAndAttachmentsToStruct(const AttachmentPtrVect
//-------------------------------------------------------------------------
// set file path, file name, file size
if (attach.at(i)->isFilePathSet()) {
+ // get the file path
std::string filepath = attach.at(i)->getFilePath();
- LoggerD("att[%d]: org filepath: %s", i, filepath.c_str());
+ SLoggerD("att[%d]: org filepath: %s", i, filepath.c_str());
filepath = common::FilesystemProvider::Create().GetRealPath(filepath);
- LoggerD("att[%d]: org virtual filepath: %s", i, filepath.c_str());
+ SLoggerD("att[%d]: org virtual filepath: %s", i, filepath.c_str());
+ // check if file exists
+ struct stat st = {0};
+ if (stat(const_cast(filepath.c_str()), &st)) {
+ LoggerE("Stat error: %d (%s)", errno, strerror(errno));
+ return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
+ "Attachment file not found",
+ ("att[%d]: attachment file not found", i));
+ }
+ // set the attachment size
+ const int fsize = st.st_size;
+ msg_set_int_value(tmpAtt, MSG_MMS_ATTACH_FILESIZE_INT, fsize);
+ LoggerD("att[%d]: filesize: %d", i, fsize);
+
+ // set the attachment file path
msg_set_str_value(tmpAtt, MSG_MMS_ATTACH_FILEPATH_STR,
const_cast(filepath.c_str()), filepath.size());
+
+ // get the file name, update the attachment structure
const size_t last_slash_idx = filepath.find_last_of("\\/");
if (std::string::npos != last_slash_idx) {
filepath.erase(0, last_slash_idx + 1);
}
- LoggerD("att[%d] filename: %s", i, filepath.c_str());
+ SLoggerD("att[%d] filename: %s", i, filepath.c_str());
msg_set_str_value(tmpAtt, MSG_MMS_ATTACH_FILENAME_STR,
const_cast(filepath.c_str()), filepath.size());
- struct stat st;
- if (stat(const_cast(filepath.c_str()), &st)) {
- LoggerE("Stat error");
- }
- const int fsize = st.st_size;
- msg_set_int_value(tmpAtt, MSG_MMS_ATTACH_FILESIZE_INT, fsize);
- LoggerD("att[%d]: filesize: %d", i,fsize);
}
//-------------------------------------------------------------------------
@@ -824,7 +834,7 @@ PlatformResult Message::addMMSBodyAndAttachmentsToStruct(const AttachmentPtrVect
} else {
return LogAndCreateResult(
ErrorCode::UNKNOWN_ERR, "failed to add attachment",
- ("att[%d]: failed to add attachment"));
+ ("att[%d]: failed to add attachment", i));
}
}
return PlatformResult(ErrorCode::NO_ERROR);
--
2.34.1