Skip to content

Commit

Permalink
Fix MimeMultiPart leaks / double delete (issue #158 #160)
Browse files Browse the repository at this point in the history
  • Loading branch information
attila-tokes committed Jul 28, 2024
1 parent db45937 commit 9e7b7dd
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion demos/demo1/demo1.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion demos/demo2/demo2.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions demos/demo2/sendemail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ void SendEmail::on_sendEmail_clicked()
message.addPart(&content);

QList<QFile*> files;
QList<MimeAttachment*> 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();
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion demos/demo3/demo3.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion demos/demo4/demo4.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/mimemessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QDateTime>
#include <QBuffer>
#include "quotedprintable.h"
#include "mimemultipart.h"
#include <typeinfo>

/* [1] Constructors and Destructors */
Expand Down Expand Up @@ -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);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/mimemessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "smtpmime_global.h"
#include "mimepart.h"
#include "mimemultipart.h"
#include "emailaddress.h"

class SMTP_MIME_EXPORT MimeMessage : public QObject
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion src/mimemultipart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MimePart*> & MimeMultiPart::getParts() const {
Expand Down
4 changes: 4 additions & 0 deletions src/mimemultipart.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ class SMTP_MIME_EXPORT MimeMultiPart : public MimePart

void addPart(MimePart *part);

void addPart(MimePart *part, const bool takeOwnership);

void writeContent(QIODevice &device) const;

/* [3] --- */

protected:
QList< MimePart* > parts;

QList< MimePart* > ownedParts;

MultiPartType type;

};
Expand Down

0 comments on commit 9e7b7dd

Please sign in to comment.