diff --git a/src/apps/mail/MailWindow.cpp b/src/apps/mail/MailWindow.cpp index 1c246e892b..5b0c6bcbc9 100644 --- a/src/apps/mail/MailWindow.cpp +++ b/src/apps/mail/MailWindow.cpp @@ -2548,11 +2548,11 @@ TMailWindow::SaveAsDraft() return status; case B_OK: { - char fileName[B_FILE_NAME_LENGTH], *eofn; + char fileName[B_FILE_NAME_LENGTH]; // save as some version of the message's subject strlcpy(fileName, fHeaderView->fSubject->Text(), sizeof(fileName)); - eofn = fileName + strlen(fileName); + uint32 originalLength = strlen(fileName); // convert /, \ and : to - for (char *bad = fileName; (bad = strchr(bad, '/')) != NULL; @@ -2564,12 +2564,19 @@ TMailWindow::SaveAsDraft() // Create the file; if the name exists, find a unique name flags = B_WRITE_ONLY | B_CREATE_FILE | B_FAIL_IF_EXISTS; - for (int32 i = 1; (status = draft.SetTo(&dir, fileName, flags)) - != B_OK; i++) { - if (status != B_FILE_EXISTS) - return status; - sprintf(eofn, "%ld", i); - } + int32 i = 1; + do { + status = draft.SetTo(&dir, fileName, flags); + if (status == B_OK) + break; + char appendix[B_FILE_NAME_LENGTH]; + sprintf(appendix, " %ld", i++); + int32 pos = min_c(sizeof(fileName) - strlen(appendix), + originalLength); + sprintf(fileName + pos, "%s", appendix); + } while (status == B_FILE_EXISTS); + if (status != B_OK) + return status; // Cache the ref if (fRef == NULL)