Skip to content

Commit

Permalink
Merge pull request bareos#1723
Browse files Browse the repository at this point in the history
fix various memory leaks
  • Loading branch information
BareosBot authored May 29, 2024
2 parents 3f33268 + 318258a commit ba928ee
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 71 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix issue #1780 libpng icc profil [PR #1788]
- Fix missing DLL in windows packaging [PR #1807]
- VMware Plugin: Adapt to older urllib3 versions [PR #1810]
- fix various memory leaks [PR #1723]

[PR #1538]: https://github.com/bareos/bareos/pull/1538
[PR #1581]: https://github.com/bareos/bareos/pull/1581
Expand Down Expand Up @@ -152,6 +153,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[PR #1719]: https://github.com/bareos/bareos/pull/1719
[PR #1721]: https://github.com/bareos/bareos/pull/1721
[PR #1722]: https://github.com/bareos/bareos/pull/1722
[PR #1723]: https://github.com/bareos/bareos/pull/1723
[PR #1724]: https://github.com/bareos/bareos/pull/1724
[PR #1727]: https://github.com/bareos/bareos/pull/1727
[PR #1728]: https://github.com/bareos/bareos/pull/1728
Expand Down
17 changes: 12 additions & 5 deletions core/src/cats/postgresql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ bool BareosDbPostgresql::OpenDatabase(JobControlRecord* jcr)
// If no connect, try once more in case it is a timing problem
if (PQstatus(db_handle_) == CONNECTION_OK) { break; }

// free memory if not successful
PQfinish(db_handle_);
db_handle_ = nullptr;

Bmicrosleep(5, 0);
}

Expand Down Expand Up @@ -263,7 +267,7 @@ void BareosDbPostgresql::CloseDatabase(JobControlRecord* jcr)
if (ref_count_ == 0) {
if (connected_) { SqlFreeResult(); }
db_list->remove(this);
if (connected_ && db_handle_) { PQfinish(db_handle_); }
if (db_handle_) { PQfinish(db_handle_); }
if (RwlIsInit(&lock_)) { RwlDestroy(&lock_); }
FreePoolMemory(errmsg);
FreePoolMemory(cmd);
Expand Down Expand Up @@ -608,10 +612,13 @@ bool BareosDbPostgresql::SqlQueryWithoutHandler(const char* query, int)

if (PQstatus(db_handle_) == CONNECTION_OK) {
// Reset the connection settings.
PQexec(db_handle_, "SET datestyle TO 'ISO, YMD'");
PQexec(db_handle_, "SET cursor_tuple_fraction=1");
PQexec(db_handle_, "SET client_min_messages TO WARNING");
result_ = PQexec(db_handle_, "SET standard_conforming_strings=on");
// prevent leak
if (result_) { PQclear(result_); }
result_ = PQexec(db_handle_,
"SET datestyle TO 'ISO, YMD';"
"SET cursor_tuple_fraction=1;"
"SET standard_conforming_strings=on;"
"SET client_min_messages TO WARNING;");

switch (PQresultStatus(result_)) {
case PGRES_COMMAND_OK:
Expand Down
5 changes: 2 additions & 3 deletions core/src/console/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,6 @@ int GetCmd(FILE* input, const char* prompt, BareosSocket* sock, int)
if (do_history) {
auto last_history_item = history_get(history_length);
if (!last_history_item || strcmp(last_history_item->line, line) != 0) {
if (history_length == history_max_entries) {
remove_history(history_base);
}
add_history(line);
}
}
Expand Down Expand Up @@ -1101,6 +1098,8 @@ int main(int argc, char* argv[])

if (history_file.size()) { ConsoleUpdateHistory(history_file.c_str()); }

clear_history();

TerminateConsole(BEXIT_SUCCESS);
return BEXIT_SUCCESS;
}
Expand Down
4 changes: 1 addition & 3 deletions core/src/dird/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2012 Free Software Foundation Europe e.V.
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -55,8 +55,6 @@ bool do_admin(JobControlRecord* jcr)
{
jcr->dir_impl->jr.JobId = jcr->JobId;

jcr->dir_impl->fname = (char*)GetPoolMemory(PM_FNAME);

Jmsg(jcr, M_INFO, 0, T_("Start Admin JobId %d, Job=%s\n"), jcr->JobId,
jcr->Job);

Expand Down
4 changes: 1 addition & 3 deletions core/src/dird/archive.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -54,8 +54,6 @@ bool DoArchive(JobControlRecord* jcr)
{
jcr->dir_impl->jr.JobId = jcr->JobId;

jcr->dir_impl->fname = (char*)GetPoolMemory(PM_FNAME);

Jmsg(jcr, M_INFO, 0, T_("Start Archive JobId %d, Job=%s\n"), jcr->JobId,
jcr->Job);

Expand Down
3 changes: 1 addition & 2 deletions core/src/dird/consolidate.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -291,7 +291,6 @@ static bool ConsolidateJobs(JobControlRecord* jcr)
bool DoConsolidate(JobControlRecord* jcr)
{
jcr->dir_impl->jr.JobId = jcr->JobId;
jcr->dir_impl->fname = (char*)GetPoolMemory(PM_FNAME);

// do not cancel virtual fulls started by consolidation
jcr->dir_impl->IgnoreDuplicateJobChecking = true;
Expand Down
7 changes: 7 additions & 0 deletions core/src/dird/dbcheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ static void eliminate_orphaned_storage_records()
printf(T_("Checking for orphaned Storage entries.\n"));
fflush(stdout);

if (!my_config) {
std::cout << "No configuration is loaded. Skipping ..." << std::endl;
return;
}

std::vector<std::string> orphaned_storage_names_list
= get_orphaned_storages_names(db);

Expand Down Expand Up @@ -932,6 +937,8 @@ int main(int argc, char* argv[])
// Drop temporary index idx_tmp_name
DropTmpIdx("idxPIchk", "File");

FreeIdList(&id_list);

db->CloseDatabase(nullptr);
CloseMsg(nullptr);
TermMsg();
Expand Down
15 changes: 14 additions & 1 deletion core/src/dird/dbcheck_utils.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2021-2023 Bareos GmbH & Co. KG
Copyright (C) 2021-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -105,6 +105,15 @@ int MakeIdList(BareosDb* db, const char* query, ID_LIST* id_list)
return 1;
}

void FreeIdList(s_id_ctx* id_list)
{
id_list->num_ids = 0;
id_list->num_del = 0;
id_list->tot_ids = 0;
free(id_list->Id);
id_list->Id = nullptr;
}

// Called here with each name to be added to the list
int NameListHandler(void* ctx, int, char** row)
{
Expand Down Expand Up @@ -153,6 +162,7 @@ void FreeNameList(NameList* name_list)
name_list->num_ids = 0;
name_list->max_ids = 0;
free(name_list->name);
name_list->name = nullptr;
}

std::vector<int> get_deletable_storageids(
Expand Down Expand Up @@ -222,7 +232,10 @@ std::vector<int> get_deletable_storageids(
storage_ids_to_delete.push_back(
orphaned_storage_ids_list.Id[orphaned_storage_id]);
}
FreeNameList(&device_names);
FreeNameList(&volume_names);
}
FreeIdList(&orphaned_storage_ids_list);
return storage_ids_to_delete;
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/dird/dbcheck_utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2021-2023 Bareos GmbH & Co. KG
Copyright (C) 2021-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -62,8 +62,9 @@ int PrintJobmediaHandler(void* ctx, int num_fields, char** row);
int PrintFileHandler(void* ctx, int num_fields, char** row);
int PrintFilesetHandler(void* ctx, int num_fields, char** row);
int PrintClientHandler(void* ctx, int num_fields, char** row);
int IdListHandler(void* ctx, int num_fields, char** row);
int MakeIdList(BareosDb* db, const char* query, ID_LIST* id_list);
int IdListHandler(void* ctx, int num_fields, char** row);
void FreeIdList(s_id_ctx* id_list);
int NameListHandler(void* ctx, int num_fields, char** row);
int MakeNameList(BareosDb* db, const char* query, s_name_ctx* name_list);
void PrintNameList(s_name_ctx* name_list);
Expand Down
3 changes: 2 additions & 1 deletion core/src/dird/director_jcr_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define BAREOS_DIRD_DIRECTOR_JCR_IMPL_H_

#include "cats/cats.h"
#include "lib/mem_pool.h"
#include "dird/client_connection_handshake_mode.h"
#include "dird/job_trigger.h"

Expand Down Expand Up @@ -135,7 +136,7 @@ struct DirectorJcrImpl {
JobId_t MigrateJobId; /**< Migration JobId specified by UA */
JobId_t VerifyJobId; /**< Verify JobId specified by UA */
};
POOLMEM* fname{}; /**< Name to put into catalog */
PoolMem fname{PM_FNAME}; /**< Name to put into catalog */
POOLMEM* client_uname{}; /**< Client uname */
POOLMEM* FDSecureEraseCmd{}; /**< Report: Secure Erase Command */
POOLMEM* SDSecureEraseCmd{}; /**< Report: Secure Erase Command */
Expand Down
8 changes: 4 additions & 4 deletions core/src/dird/fd_cmds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -893,15 +893,15 @@ int GetAttributesAndPutInCatalog(JobControlRecord* jcr)
}

// Any cached attr is flushed so we can reuse jcr->attr and jcr->ar
fn = jcr->dir_impl->fname
= CheckPoolMemorySize(jcr->dir_impl->fname, fd->message_length);
jcr->dir_impl->fname.check_size(fd->message_length);
fn = jcr->dir_impl->fname.c_str();
while (*p != 0) { *fn++ = *p++; /* copy filename */ }
*fn = *p++; /* term filename and point p to attribs */
PmStrcpy(jcr->attr, p); /* save attributes */
jcr->JobFiles++;
jcr->dir_impl->FileIndex = file_index;
ar->attr = jcr->attr;
ar->fname = jcr->dir_impl->fname;
ar->fname = jcr->dir_impl->fname.c_str();
ar->FileIndex = file_index;
ar->Stream = stream;
ar->link = NULL;
Expand All @@ -914,7 +914,7 @@ int GetAttributesAndPutInCatalog(JobControlRecord* jcr)
jcr->cached_attribute = true;

Dmsg2(debuglevel, "dird<filed: stream=%d %s\n", stream,
jcr->dir_impl->fname);
jcr->dir_impl->fname.c_str());
Dmsg1(debuglevel, "dird<filed: attr=%s\n", ar->attr);
jcr->FileId = ar->FileId;
} else if (CryptoDigestStreamType(stream) != CRYPTO_DIGEST_NONE) {
Expand Down
5 changes: 1 addition & 4 deletions core/src/dird/job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ bool SetupJob(JobControlRecord* jcr, bool suppress_output)
goto bail_out;
}
Dmsg0(150, "DB opened\n");
if (!jcr->dir_impl->fname) { jcr->dir_impl->fname = GetPoolMemory(PM_FNAME); }

if (!jcr->dir_impl->res.pool_source) {
jcr->dir_impl->res.pool_source = GetPoolMemory(PM_MESSAGE);
Expand Down Expand Up @@ -1499,7 +1498,6 @@ void DirdFreeJcrPointers(JobControlRecord* jcr)
FreeAndNullPoolMemory(jcr->JobIds);
FreeAndNullPoolMemory(jcr->dir_impl->client_uname);
FreeAndNullPoolMemory(jcr->attr);
FreeAndNullPoolMemory(jcr->dir_impl->fname);
}

/**
Expand Down Expand Up @@ -1544,7 +1542,6 @@ void DirdFreeJcr(JobControlRecord* jcr)
}

FreeAndNullPoolMemory(jcr->starttime_string);
FreeAndNullPoolMemory(jcr->dir_impl->fname);
FreeAndNullPoolMemory(jcr->dir_impl->res.pool_source);
FreeAndNullPoolMemory(jcr->dir_impl->res.npool_source);
FreeAndNullPoolMemory(jcr->dir_impl->res.rpool_source);
Expand All @@ -1554,6 +1551,7 @@ void DirdFreeJcr(JobControlRecord* jcr)
FreeAndNullPoolMemory(jcr->dir_impl->FDSecureEraseCmd);
FreeAndNullPoolMemory(jcr->dir_impl->SDSecureEraseCmd);
FreeAndNullPoolMemory(jcr->dir_impl->vf_jobids);
if (jcr->dir_impl->plugin_options) { free(jcr->dir_impl->plugin_options); }

// Delete lists setup to hold storage pointers
FreeRwstorage(jcr);
Expand Down Expand Up @@ -1632,7 +1630,6 @@ void SetJcrDefaults(JobControlRecord* jcr, JobResource* job)
break;
}

if (!jcr->dir_impl->fname) { jcr->dir_impl->fname = GetPoolMemory(PM_FNAME); }
if (!jcr->dir_impl->res.pool_source) {
jcr->dir_impl->res.pool_source = GetPoolMemory(PM_MESSAGE);
PmStrcpy(jcr->dir_impl->res.pool_source, T_("unknown source"));
Expand Down
1 change: 1 addition & 0 deletions core/src/dird/ua_restore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ static bool SelectBackupsBeforeDate(UaContext* ua,
}
// Select Client from the Catalog
if (!GetClientDbr(ua, &cr)) { goto bail_out; }
if (rx->ClientName) { free(rx->ClientName); }
rx->ClientName = strdup(cr.Name);

// Get FileSet
Expand Down
22 changes: 12 additions & 10 deletions core/src/dird/verify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,7 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId)

if (jcr->IsJobCanceled()) { goto bail_out; }
fname = CheckPoolMemorySize(fname, fd->message_length);
jcr->dir_impl->fname
= CheckPoolMemorySize(jcr->dir_impl->fname, fd->message_length);
jcr->dir_impl->fname.check_size(fd->message_length);
Dmsg1(200, "Atts+Digest=%s\n", fd->msg);
if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream, fname))
!= 3) {
Expand Down Expand Up @@ -641,16 +640,19 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId)
PmStrcpy(jcr->dir_impl->fname,
fname); /* move filename into JobControlRecord */

Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->dir_impl->fname);
Dmsg2(040, "dird<filed: stream=%d %s\n", stream,
jcr->dir_impl->fname.c_str());
Dmsg1(020, "dird<filed: attr=%s\n", attr);

// Find equivalent record in the database
fdbr.FileId = 0;
if (!jcr->db->GetFileAttributesRecord(jcr, jcr->dir_impl->fname,
if (!jcr->db->GetFileAttributesRecord(jcr, jcr->dir_impl->fname.c_str(),
&jcr->dir_impl->previous_jr,
&fdbr)) {
Jmsg(jcr, M_INFO, 0, T_("New file: %s\n"), jcr->dir_impl->fname);
Dmsg1(020, T_("File not in catalog: %s\n"), jcr->dir_impl->fname);
Jmsg(jcr, M_INFO, 0, T_("New file: %s\n"),
jcr->dir_impl->fname.c_str());
Dmsg1(020, T_("File not in catalog: %s\n"),
jcr->dir_impl->fname.c_str());
jcr->setJobStatusWithPriorityCheck(JS_Differences);
continue;
} else {
Expand All @@ -660,7 +662,7 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId)
}

Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n",
jcr->dir_impl->fname, file_index, Opts_Digest.c_str());
jcr->dir_impl->fname.c_str(), file_index, Opts_Digest.c_str());
DecodeStat(fdbr.LStat, &statc, sizeof(statc),
&LinkFIc); /* decode catalog stat */
/* Loop over options supplied by user and verify the
Expand Down Expand Up @@ -756,7 +758,7 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId)
}
break;
case '5': /* compare MD5 */
Dmsg1(500, "set Do_MD5 for %s\n", jcr->dir_impl->fname);
Dmsg1(500, "set Do_MD5 for %s\n", jcr->dir_impl->fname.c_str());
do_Digest = CRYPTO_DIGEST_MD5;
break;
case '1': /* compare SHA1 */
Expand All @@ -771,7 +773,7 @@ void GetAttributesAndCompareToCatalog(JobControlRecord* jcr, JobId_t JobId)
break;

case STREAM_RESTORE_OBJECT:
Dmsg1(400, "RESTORE_OBJECT %s\n", jcr->dir_impl->fname);
Dmsg1(400, "RESTORE_OBJECT %s\n", jcr->dir_impl->fname.c_str());
break;

default:
Expand Down Expand Up @@ -859,7 +861,7 @@ static int MissingHandler(void* ctx, int, char** row)
static void PrtFname(JobControlRecord* jcr)
{
if (!jcr->dir_impl->fn_printed) {
Jmsg(jcr, M_INFO, 0, T_("File: %s\n"), jcr->dir_impl->fname);
Jmsg(jcr, M_INFO, 0, T_("File: %s\n"), jcr->dir_impl->fname.c_str());
jcr->dir_impl->fn_printed = true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/filed/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,8 @@ bool EncodeAndSendAttributes(JobControlRecord* jcr,

// Note we send one extra byte so Dir can store zero after object
sd->message_length += comp_len + 1;
// initalize the extra byte
sd->msg[sd->message_length - 1] = '\0';
status = sd->send();
if (ff_pkt->object_compression) { FreeAndNullPoolMemory(ff_pkt->object); }
break;
Expand Down
Loading

0 comments on commit ba928ee

Please sign in to comment.