diff --git a/CHANGELOG.md b/CHANGELOG.md index 254d9c0579a..463ef1e5912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: - build: replace sprintf by snprintf due to upgraded MacOS compiler, change linking of googletest [PR #1361] - storage daemon: fix crash on volume swap [PR #1356] - core: make resource/configuration locking safer [PR #1325] +- json generation: Fix some leaks and an integer overflow [PR #1130] ### Removed - remove no longer used pkglists [PR #1335] @@ -26,6 +27,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: - add explanation about binary version numbers [PR #1354] [PR #935]: https://github.com/bareos/bareos/pull/935 +[PR #1130]: https://github.com/bareos/bareos/pull/1130 [PR #1325]: https://github.com/bareos/bareos/pull/1325 [PR #1335]: https://github.com/bareos/bareos/pull/1335 [PR #1343]: https://github.com/bareos/bareos/pull/1343 diff --git a/core/src/console/console_conf.cc b/core/src/console/console_conf.cc index 5fcebf355e4..9d3aecead2b 100644 --- a/core/src/console/console_conf.cc +++ b/core/src/console/console_conf.cc @@ -274,16 +274,19 @@ bool PrintConfigSchemaJson(PoolMem& buffer) json_object_set_new(json, "version", json_string(kBareosVersionStrings.Full)); json_t* json_resource_object = json_object(); - json_object_set(json, "resource", json_resource_object); + json_object_set_new(json, "resource", json_resource_object); json_t* bconsole = json_object(); - json_object_set(json_resource_object, "bconsole", bconsole); + json_object_set_new(json_resource_object, "bconsole", bconsole); ResourceTable* resources = my_config->resource_definitions_; for (; resources->name; ++resources) { - json_object_set(bconsole, resources->name, json_items(resources->items)); + json_object_set_new(bconsole, resources->name, + json_items(resources->items)); } - PmStrcat(buffer, json_dumps(json, JSON_INDENT(2))); + char* const json_str = json_dumps(json, JSON_INDENT(2)); + PmStrcat(buffer, json_str); + free(json_str); json_decref(json); return true; diff --git a/core/src/dird/dird_conf.cc b/core/src/dird/dird_conf.cc index 6120d7b498e..9cda704a4bb 100644 --- a/core/src/dird/dird_conf.cc +++ b/core/src/dird/dird_conf.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 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 @@ -819,18 +819,18 @@ bool PrintConfigSchemaJson(PoolMem& buffer) // Resources json_t* resource = json_object(); - json_object_set(json, "resource", resource); + json_object_set_new(json, "resource", resource); json_t* bareos_dir = json_object(); - json_object_set(resource, "bareos-dir", bareos_dir); + json_object_set_new(resource, "bareos-dir", bareos_dir); for (int r = 0; resources[r].name; r++) { ResourceTable resource = my_config->resource_definitions_[r]; - json_object_set(bareos_dir, resource.name, json_items(resource.items)); + json_object_set_new(bareos_dir, resource.name, json_items(resource.items)); } // Datatypes json_t* json_datatype_obj = json_object(); - json_object_set(json, "datatype", json_datatype_obj); + json_object_set_new(json, "datatype", json_datatype_obj); int d = 0; while (GetDatatype(d)->name != NULL) { @@ -838,75 +838,86 @@ bool PrintConfigSchemaJson(PoolMem& buffer) switch (datatype->number) { case CFG_TYPE_RUNSCRIPT: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_RUNSCRIPT, runscript_items)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_RUNSCRIPT, runscript_items)); break; case CFG_TYPE_INCEXC: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_incexc(CFG_TYPE_INCEXC)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_incexc(CFG_TYPE_INCEXC)); break; case CFG_TYPE_OPTIONS: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_options(CFG_TYPE_OPTIONS)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_options(CFG_TYPE_OPTIONS)); break; case CFG_TYPE_PROTOCOLTYPE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_PROTOCOLTYPE, backupprotocols)); + json_object_set_new( + json_datatype_obj, DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_PROTOCOLTYPE, backupprotocols)); break; case CFG_TYPE_AUTHPROTOCOLTYPE: - json_object_set( + json_object_set_new( json_datatype_obj, DatatypeToString(datatype->number), json_datatype(CFG_TYPE_AUTHPROTOCOLTYPE, authprotocols)); break; case CFG_TYPE_AUTHTYPE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_AUTHTYPE, authmethods)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_AUTHTYPE, authmethods)); break; case CFG_TYPE_LEVEL: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_LEVEL, joblevels)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_LEVEL, joblevels)); break; case CFG_TYPE_JOBTYPE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_JOBTYPE, jobtypes)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_JOBTYPE, jobtypes)); break; case CFG_TYPE_MIGTYPE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_MIGTYPE, migtypes)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_MIGTYPE, migtypes)); break; case CFG_TYPE_REPLACE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_REPLACE, ReplaceOptions)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_REPLACE, ReplaceOptions)); break; case CFG_TYPE_ACTIONONPURGE: - json_object_set( + json_object_set_new( json_datatype_obj, DatatypeToString(datatype->number), json_datatype(CFG_TYPE_ACTIONONPURGE, ActionOnPurgeOptions)); break; case CFG_TYPE_POOLTYPE: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_POOLTYPE, PoolTypes)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_POOLTYPE, PoolTypes)); break; case CFG_TYPE_RUN: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(CFG_TYPE_RUN, RunFields)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(CFG_TYPE_RUN, RunFields)); break; default: - json_object_set(json_datatype_obj, DatatypeToString(datatype->number), - json_datatype(datatype->number)); + json_object_set_new(json_datatype_obj, + DatatypeToString(datatype->number), + json_datatype(datatype->number)); break; } d++; } - /* - * following datatypes are ignored: + /* following datatypes are ignored: * - VolumeStatus: only used in ua_dotcmds, not a datatype * - FS_option_kw: from inc_conf. Replaced by CFG_TYPE_OPTIONS", - * options_items. - */ - - PmStrcat(buffer, json_dumps(json, JSON_INDENT(2))); + * options_items. */ + char* const json_str = json_dumps(json, JSON_INDENT(2)); + PmStrcat(buffer, json_str); + free(json_str); json_decref(json); return true; @@ -972,10 +983,8 @@ const char* GetUsageStringForConsoleConfigureCommand() if (configure_usage_string->strlen() == 0) { // subcommand: add for (int r = 0; resources[r].name; r++) { - /* - * Only one Director is allowed. - * If the resource have not items, there is no need to add it. - */ + /* Only one Director is allowed. + * If the resource have not items, there is no need to add it. */ if ((resources[r].rcode != R_DIRECTOR) && (resources[r].items)) { configure_usage_string->strcat("add "); resourcename.strcpy(resources[r].name); @@ -1122,10 +1131,8 @@ static void PropagateResource(ResourceItem* items, case CFG_TYPE_REPLACE: { uint32_t *def_ivalue, *ivalue; - /* - * Handle integer fields - * Note, our StoreBit does not handle bitmaped fields - */ + /* Handle integer fields + * Note, our StoreBit does not handle bitmaped fields */ def_ivalue = (uint32_t*)((char*)(source) + offset); ivalue = (uint32_t*)((char*)dest + offset); *ivalue = *def_ivalue; @@ -1216,12 +1223,10 @@ bool ValidateResource(int res_type, ResourceItem* items, BareosResource* res) bool JobResource::Validate() { - /* - * For Copy and Migrate we can have Jobs without a client or fileset. + /* For Copy and Migrate we can have Jobs without a client or fileset. * As for a copy we use the original Job as a reference for the Read storage * we also don't need to check if there is an explicit storage definition in - * either the Job or the Read pool. - */ + * either the Job or the Read pool. */ switch (JobType) { case JT_COPY: case JT_MIGRATE: @@ -1519,10 +1524,8 @@ static std::string PrintConfigRun(RunResource* run) } if (run->accurate) { - /* - * TODO: You cannot distinct if accurate was not set or if it was set to - * no maybe we need an additional variable like "accurate_set". - */ + /* TODO: You cannot distinct if accurate was not set or if it was set to + * no maybe we need an additional variable like "accurate_set". */ Mmsg(temp, "accurate=\"%s\" ", "yes"); PmStrcat(run_str, temp.c_str()); } @@ -1566,10 +1569,8 @@ static std::string PrintConfigRun(RunResource* run) } } - /* - * See if we are still in an interval and the last bit is also set then - * the interval stretches to the last item. - */ + /* See if we are still in an interval and the last bit is also set then + * the interval stretches to the last item. */ i = nr_items - 1; if (interval_start != -1 && BitIsSet(i, run->date_time_bitfield.mday)) { if ((i - interval_start) > 1) { @@ -1584,11 +1585,9 @@ static std::string PrintConfigRun(RunResource* run) PmStrcat(run_str, temp.c_str() + 1); /* jump over first comma*/ } - /* - * run->wom output is 1st, 2nd... 5th comma separated + /* run->wom output is 1st, 2nd... 5th comma separated * first, second, third... is also allowed - * but we ignore that for now - */ + * but we ignore that for now */ all_set = true; nr_items = 5; for (i = 0; i < nr_items; i++) { @@ -1623,10 +1622,8 @@ static std::string PrintConfigRun(RunResource* run) } } - /* - * See if we are still in an interval and the last bit is also set then - * the interval stretches to the last item. - */ + /* See if we are still in an interval and the last bit is also set then + * the interval stretches to the last item. */ i = nr_items - 1; if (interval_start != -1 && BitIsSet(i, run->date_time_bitfield.wom)) { if ((i - interval_start) > 1) { @@ -1676,10 +1673,8 @@ static std::string PrintConfigRun(RunResource* run) } } - /* - * See if we are still in an interval and the last bit is also set then - * the interval stretches to the last item. - */ + /* See if we are still in an interval and the last bit is also set then + * the interval stretches to the last item. */ i = nr_items - 1; if (interval_start != -1 && BitIsSet(i, run->date_time_bitfield.wday)) { if ((i - interval_start) > 1) { @@ -1729,10 +1724,8 @@ static std::string PrintConfigRun(RunResource* run) } } - /* - * See if we are still in an interval and the last bit is also set then - * the interval stretches to the last item. - */ + /* See if we are still in an interval and the last bit is also set then + * the interval stretches to the last item. */ i = nr_items - 1; if (interval_start != -1 && BitIsSet(i, run->date_time_bitfield.month)) { if ((i - interval_start) > 1) { @@ -1782,10 +1775,8 @@ static std::string PrintConfigRun(RunResource* run) } } - /* - * See if we are still in an interval and the last bit is also set then - * the interval stretches to the last item. - */ + /* See if we are still in an interval and the last bit is also set then + * the interval stretches to the last item. */ i = nr_items - 1; if (interval_start != -1 && BitIsSet(i, run->date_time_bitfield.woy)) { if ((i - interval_start) > 1) { @@ -1800,10 +1791,8 @@ static std::string PrintConfigRun(RunResource* run) PmStrcat(run_str, temp.c_str() + 1); /* jump over first comma*/ } - /* - * run->hour output is HH:MM for hour and minute though its a bitfield. - * only "hourly" sets all bits. - */ + /* run->hour output is HH:MM for hour and minute though its a bitfield. + * only "hourly" sets all bits. */ PmStrcpy(temp, ""); for (i = 0; i < 24; i++) { if (BitIsSet(i, run->date_time_bitfield.hour)) { @@ -1837,10 +1826,8 @@ static void PrintConfigRun(OutputFormatterResource& send, std::string FilesetResource::GetOptionValue(const char** option) { - /* - * Extract substring until next ":" chararcter. - * Modify the string pointer. Move it forward. - */ + /* Extract substring until next ":" chararcter. + * Modify the string pointer. Move it forward. */ std::string value; (*option)++; /* skip option key */ for (; *option[0] && *option[0] != ':'; (*option)++) { value += *option[0]; } @@ -2027,11 +2014,9 @@ void FilesetResource::PrintConfigIncludeExcludeOptions( send.KeyMultipleStringsOnePerLine("Wild", std::addressof(fo->wild)); send.KeyMultipleStringsOnePerLine("WildDir", std::addressof(fo->wilddir)); send.KeyMultipleStringsOnePerLine("WildFile", std::addressof(fo->wildfile)); - /* - * Wildbase is WildFile not containing a / or \\ + /* Wildbase is WildFile not containing a / or \\ * see void StoreWild() in inc_conf.c - * so we need to translate it back to a Wild File entry - */ + * so we need to translate it back to a Wild File entry */ send.KeyMultipleStringsOnePerLine("WildBase", std::addressof(fo->wildbase)); send.KeyMultipleStringsOnePerLine("Base", std::addressof(fo->base)); send.KeyMultipleStringsOnePerLine("FsType", std::addressof(fo->fstype)); @@ -2191,15 +2176,13 @@ static bool UpdateResourcePointer(int type, ResourceItem* items) // Resources not containing a resource break; case R_POOL: { - /* - * Resources containing another resource or alist. First + /* Resources containing another resource or alist. First * look up the resource which contains another resource. It * was written during pass 1. Then stuff in the pointers to * the resources it contains, which were inserted this pass. * Finally, it will all be stored back. * - * Find resource saved in pass 1 - */ + * Find resource saved in pass 1 */ PoolResource* p = dynamic_cast( my_config->GetResWithName(R_POOL, res_pool->resource_name_)); if (!p) { @@ -2340,13 +2323,11 @@ static bool UpdateResourcePointer(int type, ResourceItem* items) res_job->resource_name_, BitIsSet(69, res_job->inherit_content_), BitIsSet(69, p->inherit_content_)); - /* - * TODO: JobDefs where/regexwhere doesn't work well (but this is not + /* TODO: JobDefs where/regexwhere doesn't work well (but this is not * very useful) We have to SetBit(index, item_present_); or * something like that * - * We take RegexWhere before all other options - */ + * We take RegexWhere before all other options */ if (!p->RegexWhere && (p->strip_prefix || p->add_suffix || p->add_prefix)) { int len = BregexpGetBuildWhereSize(p->strip_prefix, p->add_prefix, @@ -2408,12 +2389,10 @@ static bool UpdateResourcePointer(int type, ResourceItem* items) break; } case R_SCHEDULE: { - /* - * Schedule is a bit different in that it contains a RunResource record + /* Schedule is a bit different in that it contains a RunResource record * chain which isn't a "named" resource. This chain was linked * in by run_conf.c during pass 2, so here we jam the pointer - * into the Schedule resource. - */ + * into the Schedule resource. */ ScheduleResource* p = dynamic_cast( my_config->GetResWithName(type, res_sch->resource_name_)); if (!p) { @@ -2519,10 +2498,8 @@ static void StoreActiononpurge(LEX* lc, ResourceItem* item, int index, int) uint32_t* destination = GetItemVariablePointer(*item); LexGetToken(lc, BCT_NAME); - /* - * Store the type both in pass 1 and pass 2 - * Scan ActionOnPurge options - */ + /* Store the type both in pass 1 and pass 2 + * Scan ActionOnPurge options */ bool found = false; for (int i = 0; ActionOnPurgeOptions[i].name; i++) { if (Bstrcasecmp(lc->str, ActionOnPurgeOptions[i].name)) { @@ -2763,11 +2740,9 @@ static void StoreAutopassword(LEX* lc, ResourceItem* item, int index, int pass) { switch ((*item->allocated_resource)->rcode_) { case R_DIRECTOR: - /* - * As we need to store both clear and MD5 hashed within the same + /* As we need to store both clear and MD5 hashed within the same * resource class we use the item->code as a hint default is 0 - * and for clear we need a code of 1. - */ + * and for clear we need a code of 1. */ switch (item->code) { case 1: my_config->StoreResource(CFG_TYPE_CLEARPASSWORD, lc, item, index, @@ -3020,12 +2995,10 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass) res_runscript = new RunScript(); - /* - * Run on client by default. + /* Run on client by default. * Set this here, instead of in the class constructor, * as the class is also used by other daemon, - * where the default differs. - */ + * where the default differs. */ if (res_runscript->target.empty()) { res_runscript->SetTarget("%c"); } while ((token = LexGetToken(lc, BCT_SKIP_EOL)) != BCT_EOF) { @@ -3158,10 +3131,8 @@ extern "C" char* job_code_callback_director(JobControlRecord* jcr, return my_name; case 'V': if (jcr) { - /* - * If this is a migration/copy we need the volume name from the - * mig_jcr. - */ + /* If this is a migration/copy we need the volume name from the + * mig_jcr. */ if (jcr->dir_impl->mig_jcr) { jcr = jcr->dir_impl->mig_jcr; } if (jcr->VolumeName) { @@ -3364,10 +3335,8 @@ static bool HasDefaultValue(ResourceItem& item) break; } case CFG_TYPE_SHRTRUNSCRIPT: - /* - * We don't get here as this type is converted to a CFG_TYPE_RUNSCRIPT - * when parsed - */ + /* We don't get here as this type is converted to a CFG_TYPE_RUNSCRIPT + * when parsed */ break; case CFG_TYPE_ACL: { alist** alistvalue @@ -3477,10 +3446,8 @@ static void PrintConfigCb(ResourceItem& item, PrintConfigRunscript(send, item, inherited, verbose); break; case CFG_TYPE_SHRTRUNSCRIPT: - /* - * We don't get here as this type is converted to a CFG_TYPE_RUNSCRIPT - * when parsed - */ + /* We don't get here as this type is converted to a CFG_TYPE_RUNSCRIPT + * when parsed */ break; case CFG_TYPE_ACL: { alist** alistvalue @@ -4023,11 +3990,9 @@ static bool SaveResource(int type, ResourceItem* items, int pass) switch (type) { case R_DIRECTOR: { - /* - * IP Addresses can be set by multiple directives. + /* IP Addresses can be set by multiple directives. * If they differ from the default, - * the set the main directive to be set. - */ + * the set the main directive to be set. */ if ((res_dir->DIRaddrs) && (res_dir->DIRaddrs->size() > 0)) { for (int i = 0; items[i].name; i++) { if (Bstrcasecmp(items[i].name, "DirAddresses")) { @@ -4041,10 +4006,8 @@ static bool SaveResource(int type, ResourceItem* items, int pass) case R_JOBDEFS: break; case R_JOB: - /* - * Check Job requirements after applying JobDefs - * Ensure that the name item is present however. - */ + /* Check Job requirements after applying JobDefs + * Ensure that the name item is present however. */ if (items[0].flags & CFG_ITEM_REQUIRED) { if (!BitIsSet(0, allocated_resource->item_present_)) { Emsg2(M_ERROR, 0, @@ -4060,12 +4023,10 @@ static bool SaveResource(int type, ResourceItem* items, int pass) break; } - /* - * During pass 2 in each "store" routine, we looked up pointers + /* During pass 2 in each "store" routine, we looked up pointers * to all the resources referenced in the current resource, now we * must copy their addresses from the static record to the allocated - * record. - */ + * record. */ if (pass == 2) { bool ret = UpdateResourcePointer(type, items); return ret; diff --git a/core/src/filed/filed_conf.cc b/core/src/filed/filed_conf.cc index c5fe7942496..cb485a11678 100644 --- a/core/src/filed/filed_conf.cc +++ b/core/src/filed/filed_conf.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2008 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 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 @@ -276,16 +276,18 @@ bool PrintConfigSchemaJson(PoolMem& buffer) // Resources json_t* resource = json_object(); - json_object_set(json, "resource", resource); + json_object_set_new(json, "resource", resource); json_t* bareos_fd = json_object(); - json_object_set(resource, "bareos-fd", bareos_fd); + json_object_set_new(resource, "bareos-fd", bareos_fd); for (int r = 0; resources[r].name; r++) { ResourceTable resource = my_config->resource_definitions_[r]; - json_object_set(bareos_fd, resource.name, json_items(resource.items)); + json_object_set_new(bareos_fd, resource.name, json_items(resource.items)); } - PmStrcat(buffer, json_dumps(json, JSON_INDENT(2))); + char* const json_str = json_dumps(json, JSON_INDENT(2)); + PmStrcat(buffer, json_str); + free(json_str); json_decref(json); return true; @@ -381,9 +383,7 @@ static void FreeResource(BareosResource* res, int type) if (p->pki_signing_key_files) { delete p->pki_signing_key_files; } if (p->pki_signers) { X509_KEYPAIR* keypair = nullptr; - foreach_alist (keypair, p->pki_signers) { - CryptoKeypairFree(keypair); - } + foreach_alist (keypair, p->pki_signers) { CryptoKeypairFree(keypair); } delete p->pki_signers; } if (p->pki_master_key_files) { delete p->pki_master_key_files; } diff --git a/core/src/lib/bsys.cc b/core/src/lib/bsys.cc index a06ed874829..63a9a443f61 100644 --- a/core/src/lib/bsys.cc +++ b/core/src/lib/bsys.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 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 @@ -161,7 +161,7 @@ int Bmicrosleep(int32_t sec, int32_t usec) int status; timeout.tv_sec = sec; - timeout.tv_nsec = usec * 1000; + timeout.tv_nsec = static_cast(usec) * 1000l; #ifdef HAVE_NANOSLEEP status = nanosleep(&timeout, NULL); @@ -171,7 +171,7 @@ int Bmicrosleep(int32_t sec, int32_t usec) // Do it the old way gettimeofday(&tv, &tz); - timeout.tv_nsec += tv.tv_usec * 1000; + timeout.tv_nsec += static_cast(tv.tv_usec) * 1000l; timeout.tv_sec += tv.tv_sec; while (timeout.tv_nsec >= 1000000000) { timeout.tv_nsec -= 1000000000; @@ -196,20 +196,16 @@ char* bstrinlinecpy(char* dest, const char* src) { int len; - /* - * Sanity check. We can only inline copy if the src > dest + /* Sanity check. We can only inline copy if the src > dest * otherwise the resulting copy will overwrite the end of - * the string. - */ + * the string. */ if (src <= dest) { return NULL; } len = strlen(src); - /* - * Cannot use strcpy or memcpy as those functions are not + /* Cannot use strcpy or memcpy as those functions are not * allowed on overlapping data and this is inline replacement * for sure is. So we use memmove which is allowed on - * overlapping data. - */ + * overlapping data. */ memmove(dest, src, len + 1); return dest; @@ -838,10 +834,8 @@ bool PathIsAbsolute(const char* path) if (IsPathSeparator(path[0])) { return true; } #ifdef HAVE_WIN32 - /* - * Windows: - * Does path begin with drive? if yes, it is absolute - */ + /* Windows: + * Does path begin with drive? if yes, it is absolute */ if (strlen(path) >= 3) { if (isalpha(path[0]) && path[1] == ':' && IsPathSeparator(path[2])) { return true; diff --git a/core/src/qt-tray-monitor/tray_conf.cc b/core/src/qt-tray-monitor/tray_conf.cc index 3b08f6601a0..2a6504a2799 100644 --- a/core/src/qt-tray-monitor/tray_conf.cc +++ b/core/src/qt-tray-monitor/tray_conf.cc @@ -3,7 +3,7 @@ Copyright (C) 2004-2011 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 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 @@ -291,12 +291,10 @@ static bool SaveResource(int type, ResourceItem* items, int pass) } } - /* - * During pass 2 in each "store" routine, we looked up pointers + /* During pass 2 in each "store" routine, we looked up pointers * to all the resource_definitions referrenced in the current resource, now we * must copy their addresses from the static record to the allocated - * record. - */ + * record. */ if (pass == 2) { switch (type) { case R_MONITOR: @@ -401,17 +399,19 @@ bool PrintConfigSchemaJson(PoolMem& buffer) // Resources json_t* resource = json_object(); - json_object_set(json, "resource", resource); + json_object_set_new(json, "resource", resource); json_t* bareos_tray_monitor = json_object(); - json_object_set(resource, "bareos-tray-monitor", bareos_tray_monitor); + json_object_set_new(resource, "bareos-tray-monitor", bareos_tray_monitor); for (int r = 0; resource_definitions[r].name; r++) { ResourceTable resource = my_config->resource_definitions_[r]; - json_object_set(bareos_tray_monitor, resource.name, - json_items(resource.items)); + json_object_set_new(bareos_tray_monitor, resource.name, + json_items(resource.items)); } - PmStrcat(buffer, json_dumps(json, JSON_INDENT(2))); + char* const json_str = json_dumps(json, JSON_INDENT(2)); + PmStrcat(buffer, json_str); + free(json_str); json_decref(json); return true; diff --git a/core/src/stored/stored_conf.cc b/core/src/stored/stored_conf.cc index 2f5486f2cc2..25608166d05 100644 --- a/core/src/stored/stored_conf.cc +++ b/core/src/stored/stored_conf.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2009 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 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 @@ -290,11 +290,9 @@ static void StoreAutopassword(LEX* lc, ResourceItem* item, int index, int pass) { switch ((*item->allocated_resource)->rcode_) { case R_DIRECTOR: - /* - * As we need to store both clear and MD5 hashed within the same + /* As we need to store both clear and MD5 hashed within the same * resource class we use the item->code as a hint default is 0 - * and for clear we need a code of 1. - */ + * and for clear we need a code of 1. */ switch (item->code) { case 1: my_config->StoreResource(CFG_TYPE_CLEARPASSWORD, lc, item, index, @@ -491,10 +489,8 @@ static void CheckDropletDevices(ConfigurationParser& my_config) DeviceResource* d = dynamic_cast(p); if (d && d->device_type == DeviceType::B_DROPLET_DEV) { if (d->max_concurrent_jobs == 0) { - /* - * 0 is the general default. However, for this device_type, only 1 - * works. So we set it to this value. - */ + /* 0 is the general default. However, for this device_type, only 1 + * works. So we set it to this value. */ Jmsg1(nullptr, M_WARNING, 0, _("device %s is set to the default 'Maximum Concurrent Jobs' = " "1.\n"), @@ -632,16 +628,18 @@ bool PrintConfigSchemaJson(PoolMem& buffer) // Resources json_t* resource = json_object(); - json_object_set(json, "resource", resource); + json_object_set_new(json, "resource", resource); json_t* bareos_sd = json_object(); - json_object_set(resource, "bareos-sd", bareos_sd); + json_object_set_new(resource, "bareos-sd", bareos_sd); for (int r = 0; resources[r].name; r++) { ResourceTable resource = my_config->resource_definitions_[r]; - json_object_set(bareos_sd, resource.name, json_items(resource.items)); + json_object_set_new(bareos_sd, resource.name, json_items(resource.items)); } - PmStrcat(buffer, json_dumps(json, JSON_INDENT(2))); + char* const json_str = json_dumps(json, JSON_INDENT(2)); + PmStrcat(buffer, json_str); + free(json_str); json_decref(json); return true;