From 9e7b7dd6f9faa260b9cff0c898ab3853385b01a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20T=C5=91k=C3=A9s?= Date: Sun, 28 Jul 2024 14:18:37 +0300 Subject: [PATCH] Fix MimeMultiPart leaks / double delete (issue #158 #160) --- demos/demo1/demo1.pro | 2 +- demos/demo2/demo2.pro | 2 +- demos/demo2/sendemail.cpp | 9 +-------- demos/demo3/demo3.pro | 2 +- demos/demo4/demo4.pro | 2 +- src/mimemessage.cpp | 9 +++++++-- src/mimemessage.h | 2 +- src/mimemultipart.cpp | 9 ++++++++- src/mimemultipart.h | 4 ++++ 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/demos/demo1/demo1.pro b/demos/demo1/demo1.pro index 9565297..8de05cc 100644 --- a/demos/demo1/demo1.pro +++ b/demos/demo1/demo1.pro @@ -19,7 +19,7 @@ SOURCES += \ demo1.cpp # Location of SMTP Library -SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug +SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Release win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPMime2 else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPMime2 diff --git a/demos/demo2/demo2.pro b/demos/demo2/demo2.pro index 8bb8427..c8e4d8c 100644 --- a/demos/demo2/demo2.pro +++ b/demos/demo2/demo2.pro @@ -16,7 +16,7 @@ SOURCES += \ sendemail.cpp # Location of SMTP Library -SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug +SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Release win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPMime2 else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPMime2 diff --git a/demos/demo2/sendemail.cpp b/demos/demo2/sendemail.cpp index 735c431..0deca10 100644 --- a/demos/demo2/sendemail.cpp +++ b/demos/demo2/sendemail.cpp @@ -107,16 +107,14 @@ void SendEmail::on_sendEmail_clicked() message.addPart(&content); QList files; - QList attachments; for (int i = 0; i < ui->attachments->count(); ++i) { QFile* file = new QFile(ui->attachments->item(i)->text()); files.append(file); MimeAttachment* attachment = new MimeAttachment(file); - attachments.append(attachment); - message.addPart(attachment); + message.addPart(attachment, true); } smtp.connectToHost(); @@ -154,11 +152,6 @@ void SendEmail::on_sendEmail_clicked() for (auto file : files) { delete file; } - - for (auto attachment : attachments) { - delete attachment; - } - } void SendEmail::errorMessage(const QString &message) diff --git a/demos/demo3/demo3.pro b/demos/demo3/demo3.pro index 71b54f5..9aab0b6 100644 --- a/demos/demo3/demo3.pro +++ b/demos/demo3/demo3.pro @@ -18,7 +18,7 @@ SOURCES += \ demo3.cpp # Location of SMTP Library -SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug +SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Release win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPMime2 else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPMime2 diff --git a/demos/demo4/demo4.pro b/demos/demo4/demo4.pro index 7e32125..3b3fa2b 100644 --- a/demos/demo4/demo4.pro +++ b/demos/demo4/demo4.pro @@ -18,7 +18,7 @@ SOURCES += \ demo4.cpp # Location of SMTP Library -SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Debug +SMTP_LIBRARY_LOCATION = $$PWD/../../../build/SMTPEmail-Desktop-Release win32:CONFIG(release, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/release/ -lSMTPMime2 else:win32:CONFIG(debug, debug|release): LIBS += -L$$SMTP_LIBRARY_LOCATION/debug/ -lSMTPMime2 diff --git a/src/mimemessage.cpp b/src/mimemessage.cpp index 0f45902..ed29556 100644 --- a/src/mimemessage.cpp +++ b/src/mimemessage.cpp @@ -22,6 +22,7 @@ #include #include #include "quotedprintable.h" +#include "mimemultipart.h" #include /* [1] Constructors and Destructors */ @@ -102,10 +103,14 @@ void MimeMessage::setSubject(const QString & subject) this->subject = subject; } -void MimeMessage::addPart(MimePart *part) +void MimeMessage::addPart(MimePart *part) { + this->addPart(part, false); +} + +void MimeMessage::addPart(MimePart *part, const bool takeOwnership) { if (typeid(*content) == typeid(MimeMultiPart)) { - ((MimeMultiPart*) content)->addPart(part); + ((MimeMultiPart*) content)->addPart(part, takeOwnership); }; } diff --git a/src/mimemessage.h b/src/mimemessage.h index 2dcd09e..bd9aeee 100644 --- a/src/mimemessage.h +++ b/src/mimemessage.h @@ -25,7 +25,6 @@ #include "smtpmime_global.h" #include "mimepart.h" -#include "mimemultipart.h" #include "emailaddress.h" class SMTP_MIME_EXPORT MimeMessage : public QObject @@ -57,6 +56,7 @@ class SMTP_MIME_EXPORT MimeMessage : public QObject void addCustomHeader(const QString &hdr); void setSubject(const QString &subject); void addPart(MimePart* part); + void addPart(MimePart* part, const bool takeOwnership); void setHeaderEncoding(MimePart::Encoding); diff --git a/src/mimemultipart.cpp b/src/mimemultipart.cpp index 5ad86ac..8cb0d3f 100644 --- a/src/mimemultipart.cpp +++ b/src/mimemultipart.cpp @@ -44,13 +44,20 @@ MimeMultiPart::MimeMultiPart(MultiPartType type) } MimeMultiPart::~MimeMultiPart() { - foreach (MimePart *part, parts) { + foreach (MimePart *part, ownedParts) { delete part; } } void MimeMultiPart::addPart(MimePart *part) { + this->addPart(part, false); +} + +void MimeMultiPart::addPart(MimePart *part, const bool takeOwnership) { parts.append(part); + if (takeOwnership) { + ownedParts.append(part); + } } const QList & MimeMultiPart::getParts() const { diff --git a/src/mimemultipart.h b/src/mimemultipart.h index 1450ad4..ac04552 100644 --- a/src/mimemultipart.h +++ b/src/mimemultipart.h @@ -60,6 +60,8 @@ class SMTP_MIME_EXPORT MimeMultiPart : public MimePart void addPart(MimePart *part); + void addPart(MimePart *part, const bool takeOwnership); + void writeContent(QIODevice &device) const; /* [3] --- */ @@ -67,6 +69,8 @@ class SMTP_MIME_EXPORT MimeMultiPart : public MimePart protected: QList< MimePart* > parts; + QList< MimePart* > ownedParts; + MultiPartType type; };