Skip to content

Fix memory leak in AuditLog::init() #1897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/audit_log/audit_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,42 +209,60 @@ 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;
}

/* Sanity check */
if (m_status == RelevantOnlyAuditLogStatus) {
if (m_relevant.empty()) {
/*
error->assign("m_relevant cannot be null while status is set to " \
error->assign("m_relevant cannot be null while status is set to " \
"RelevantOnly");
return false;
*/
return false;
*/
// FIXME: this should be a warning. There is not point to
// have the logs on relevant only if nothing is relevant.
//
// Not returning an error to keep the compatibility with v2.
}
}

if (m_writer) {
delete m_writer;
}

m_writer = tmp_writer;

return true;
}

Expand Down