Fix email API storage. 02/140502/7 accepted/tizen/unified/20170808.170930 submit/tizen/20170802.070535
authorSanjeev BA <iamsanjeev@gmail.com>
Tue, 25 Jul 2017 07:34:42 +0000 (16:34 +0900)
committerSanjeev BA <iamsanjeev@gmail.com>
Thu, 27 Jul 2017 02:50:42 +0000 (11:50 +0900)
Email API writes to same file irrespective of handle.
If two processes from same app, try to write to same file, simultaneously, it could corrupt the contents.
This patch uses mkstemp() API to create temporary file names.

Change-Id: I31460f65d20b5a8707d8684ca279bdc0c54d5acb

src/email.c

index f09d1cb..3b8082b 100755 (executable)
@@ -140,10 +140,12 @@ int email_create_message(email_h *msg)
        msg_s->mail->full_address_from = (char *)calloc(1, sizeof(char) * (len));/* "++"+<+ address +> + NULL */
        char *strfrom = msg_s->mail->full_address_from;
 
-       if (account->incoming_server_user_name)
-               snprintf(strfrom, len, "%s%s%s%s%s%s", "\"", account->incoming_server_user_name, "\"", "<", account->user_email_address, ">");
-       else
-               snprintf(strfrom, len, "%s%s%s", "<", account->user_email_address, ">");
+       if (NULL != strfrom) {
+               if (account->incoming_server_user_name)
+                       snprintf(strfrom, len, "%s%s%s%s%s%s", "\"", account->incoming_server_user_name, "\"", "<", account->user_email_address, ">");
+               else
+                       snprintf(strfrom, len, "%s%s%s", "<", account->user_email_address, ">");
+       }
 
        /* mbox */
        email_mailbox_t *mbox = msg_s->mbox;
@@ -259,7 +261,7 @@ int email_set_body(email_h msg, const char *body)
        }
        free(prefix_path);
 
-       file = fopen(file_path, "w");
+       file = fdopen(mkstemp(file_path), "w");
        if (NULL == file) {
                /* LCOV_EXCL_START */
                SECURE_SLOGE("fopen() Fail(%d)", EMAILS_ERROR_OPERATION_FAILED);
@@ -269,6 +271,7 @@ int email_set_body(email_h msg, const char *body)
 
        fputs(body, file);
        fclose(file);
+       unlink(file_path);
 
        msg_s->mail->file_path_plain = file_path;