Fix memory leak in AuditLog::init()

This commit is contained in:
Wenfeng Liu 2018-09-12 03:50:33 +00:00 committed by Felipe Zimmerle
parent 8c549c65c4
commit ec1112c648
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277

View File

@ -209,24 +209,36 @@ bool AuditLog::setType(AuditLogType audit_type) {
bool AuditLog::init(std::string *error) {
audit_log::writer::Writer *tmp_writer;
if (m_status == OffAuditLogStatus || m_status == NotSetLogStatus) {
if (m_writer) {
delete m_writer;
m_writer = NULL;
}
return true;
}
if (m_type == ParallelAuditLogType) {
m_writer = new audit_log::writer::Parallel(this);
tmp_writer = new audit_log::writer::Parallel(this);
} else if (m_type == HttpsAuditLogType) {
m_writer = new audit_log::writer::Https(this);
tmp_writer = new audit_log::writer::Https(this);
} else {
/*
* if (m_type == SerialAuditLogType
* || m_type == NotSetAuditLogType)
*
*/
m_writer = new audit_log::writer::Serial(this);
tmp_writer = new audit_log::writer::Serial(this);
}
if (m_status == OffAuditLogStatus || m_status == NotSetLogStatus) {
return true;
if (tmp_writer == NULL) {
error->assign("Writer memory alloc failed!");
return false;
}
if (m_writer == NULL || m_writer->init(error) == false) {
if (tmp_writer->init(error) == false) {
delete tmp_writer;
return false;
}
@ -245,6 +257,12 @@ bool AuditLog::init(std::string *error) {
}
}
if (m_writer) {
delete m_writer;
}
m_writer = tmp_writer;
return true;
}