I am very sorry, I forgot to attach the patches: so this time with patches!
best regards, Adi Kriegisch
From f2bee6b45cfc617330b9a5057db6e8425943900b Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Mon, 3 Dec 2018 13:59:40 +0100 Subject: [PATCH] Fixed memory leak in filesexist function Changelog: Title Ticket: ENT-4313 Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit 64589e5cb308e46646a46ef84cb57e487f4f7ddc) --- libpromises/evalfunction.c | 1 + 1 file changed, 1 insertion(+) Index: cfengine3-3.12.1/libpromises/evalfunction.c =================================================================== --- cfengine3-3.12.1.orig/libpromises/evalfunction.c +++ cfengine3-3.12.1/libpromises/evalfunction.c @@ -7095,6 +7095,7 @@ static FnCallResult FnCallFileSexist(Eva { file_found = false; } + free(val); el = JsonIteratorNextValueByType(&iter, JSON_ELEMENT_TYPE_PRIMITIVE, true); }
From afd1c1b21de0ea378c68b397df140979b1b96fa4 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Thu, 8 Nov 2018 23:54:30 +0100 Subject: [PATCH] Fixed memory leak in JSON to policy conversion Ticket: ENT-4136 Changelog: Title Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit b90dce1d265e51a3d4352ed8bf9016653a34d96c) --- libpromises/policy.c | 2 ++ libpromises/rlist.c | 1 + 2 files changed, 3 insertions(+) diff --git a/libpromises/policy.c b/libpromises/policy.c index f433b66c062..d3b03639959 100644 --- a/libpromises/policy.c +++ b/libpromises/policy.c @@ -2111,6 +2111,7 @@ static Rval RvalFromJson(JsonElement *json_rval) { Rval list_value = RvalFromJson(JsonArrayGetAsObject(json_list, i)); RlistAppend(&rlist, list_value.item, list_value.type); + RvalDestroy(list_value); } return ((Rval) { rlist, RVAL_TYPE_LIST }); @@ -2127,6 +2128,7 @@ static Rval RvalFromJson(JsonElement *json_rval) Rval arg = RvalFromJson(json_arg); RlistAppend(&args, arg.item, arg.type); + RvalDestroy(arg); } FnCall *fn = FnCallNew(name, args); diff --git a/libpromises/rlist.c b/libpromises/rlist.c index 6e6024f5c0f..5048387c0aa 100644 --- a/libpromises/rlist.c +++ b/libpromises/rlist.c @@ -526,6 +526,7 @@ Rlist *RlistAppendScalar(Rlist **start, const char *scalar) return RlistAppendRval(start, RvalCopyScalar((Rval) { (char *)scalar, RVAL_TYPE_SCALAR })); } +// NOTE: Copies item, does NOT take ownership Rlist *RlistAppend(Rlist **start, const void *item, RvalType type) { return RlistAppendAllTypes(start, item, type, false);
From 7dc010d62474550cda7cddc2132988d6fcfeb51c Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Fri, 9 Nov 2018 00:10:18 +0100 Subject: [PATCH] Fixed small memory leak in cf-upgrade Changelog: Title Ticket: ENT-4136 Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit ee55bbad775d7a104aa6a7c997b512334d978f90) --- cf-upgrade/configuration.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cf-upgrade/configuration.c b/cf-upgrade/configuration.c index b4917907ecc..2f43504f233 100644 --- a/cf-upgrade/configuration.c +++ b/cf-upgrade/configuration.c @@ -61,12 +61,18 @@ void ConfigurationDestroy(Configuration **configuration) { return; } - free ((*configuration)->cf_upgrade); - free ((*configuration)->backup_path); - free ((*configuration)->backup_tool); - free ((*configuration)->copy_path); - free ((*configuration)->cfengine_path); - free (*configuration); + Configuration *config = *configuration; + for (int i = 0; i < config->number_of_arguments; ++i) + { + free(config->arguments[i]); + config->arguments[i] = NULL; + } + free(config->cf_upgrade); + free(config->backup_path); + free(config->backup_tool); + free(config->copy_path); + free(config->cfengine_path); + free(config); *configuration = NULL; }
From 9a46bd2e529c8d4099613ec1e502b7b8350bbc05 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Thu, 6 Dec 2018 16:41:31 +0100 Subject: [PATCH] Fixed memory leak in mustache rendering Changelog: Title Ticket: ENT-4313 Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit 8078b566047095eda15d21d4a788efdcec6bc135) --- cf-agent/verify_files.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf-agent/verify_files.c b/cf-agent/verify_files.c index 9c29288a9c0..8283f335482 100644 --- a/cf-agent/verify_files.c +++ b/cf-agent/verify_files.c @@ -602,9 +602,12 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx, const Promise *pp, { PromiseResult result = PROMISE_RESULT_NOOP; + JsonElement *destroy_this = NULL; + if (a.template_data == NULL) { a.template_data = DefaultTemplateData(ctx, NULL); + destroy_this = a.template_data; } unsigned char existing_output_digest[EVP_MAX_MD_SIZE + 1] = { 0 }; @@ -658,7 +661,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx, const Promise *pp, BufferDestroy(output_buffer); free(message); - + JsonDestroy(destroy_this); return result; } else @@ -666,6 +669,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx, const Promise *pp, cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Error rendering mustache template '%s'", a.edit_template); result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL); BufferDestroy(output_buffer); + JsonDestroy(destroy_this); return PromiseResultUpdate(result, PROMISE_RESULT_FAIL); } }
From 554b5bcadb58b3892f00ad3c0d46561d230ea259 Mon Sep 17 00:00:00 2001 From: Vratislav Podzimek <vratislav.podzi...@northern.tech> Date: Tue, 9 Apr 2019 17:19:35 +0200 Subject: [PATCH] Make sure map->load is 0 at the end of HashMapClear() Otherwise the size is unchanged and the map thinks it still has all the items that were there before. The items are gone, but when new ones are inserted, the table sooner or later hits the threshold and grows automatically. Repeat this process of "insert a couple hundred items, clear, repeat" multiple times and the table grows and grows...and grows. Let's decrement map->load everytime we remove an item and assert that it reaches 0 when all items are removed. Ticket: CFE-3032 Changelog: Fix growing memory footprint of daemons (cherry picked from commit 09e5d244793d80ec6e953ae6f57f045eda30ab0d) --- libutils/hash_map.c | 3 +++ tests/unit/map_test.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) Index: cfengine3-3.12.1/libutils/hash_map.c =================================================================== --- cfengine3-3.12.1.orig/libutils/hash_map.c +++ cfengine3-3.12.1/libutils/hash_map.c @@ -198,6 +198,7 @@ static void FreeBucketListItem(HashMap * map->destroy_key_fn(item->value.key); map->destroy_value_fn(item->value.value); free(item); + map->load--; } /* Do not destroy value item */ @@ -210,6 +211,7 @@ static void FreeBucketListItemSoft(HashM map->destroy_key_fn(item->value.key); free(item); + map->load--; } void HashMapClear(HashMap *map) @@ -222,6 +224,7 @@ void HashMapClear(HashMap *map) } map->buckets[i] = NULL; } + assert(map->load == 0); } void HashMapSoftDestroy(HashMap *map) Index: cfengine3-3.12.1/tests/unit/map_test.c =================================================================== --- cfengine3-3.12.1.orig/tests/unit/map_test.c +++ cfengine3-3.12.1/tests/unit/map_test.c @@ -369,6 +369,55 @@ static void test_clear(void) StringMapDestroy(map); } +static void test_clear_hashmap(void) +{ + HashMap *map = HashMapNew(StringHash_untyped, StringSafeEqual_untyped, + free, free, HASH_MAP_INIT_SIZE); + + assert_false(HashMapInsert(map, xstrdup("one"), xstrdup("first"))); + assert_false(HashMapInsert(map, xstrdup("two"), xstrdup("second"))); + + assert_true(HashMapGet(map, "one") != NULL); + assert_true(HashMapGet(map, "two") != NULL); + assert_int_equal(map->load, 2); + + HashMapClear(map); + assert_true(HashMapGet(map, "one") == NULL); + assert_true(HashMapGet(map, "two") == NULL); + assert_int_equal(map->load, 0); + + + /* make sure that inserting items after clear doesn't trigger growth */ + unsigned int i = 0; + + /* first populate the hashmap just below the threshold */ + size_t orig_size = map->size; + size_t orig_threshold = map->max_threshold; + + for (i = 1; i <= orig_threshold; i++) + { + test_add_n_as_to_map(map, i); + assert_int_equal(map->load, i); + } + assert_int_equal(map->size, orig_size); + assert_int_equal(map->max_threshold, orig_threshold); + + /* clear and repopulate again */ + HashMapClear(map); + for (i = 1; i <= orig_threshold; i++) + { + test_add_n_as_to_map(map, i); + assert_int_equal(map->load, i); + } + + /* the map was cleared before re-population, there's no reason for it to + * grow */ + assert_int_equal(map->size, orig_size); + assert_int_equal(map->max_threshold, orig_threshold); + + HashMapDestroy(map); +} + static void test_soft_destroy(void) { StringMap *map = StringMapNew(); @@ -589,6 +638,7 @@ int main() unit_test(test_get), unit_test(test_has_key), unit_test(test_clear), + unit_test(test_clear_hashmap), unit_test(test_soft_destroy), unit_test(test_hashmap_new_destroy), unit_test(test_hashmap_degenerate_hash_fn),
From efdcfffb6f35f4ea465d114a7762cdc859a358ef Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Tue, 14 May 2019 22:07:15 +0200 Subject: [PATCH] Fixed memory leak in: differences(), intersection(), unique() They all use FnCallSetOp() internally, which was lacking free for some error handling return paths. Changelog: Title Ticket: ENT-4586 Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit 46addfffa01229967b0942e0cb21d9a2e18c3385) --- libpromises/evalfunction.c | 3 +++ 1 file changed, 3 insertions(+) Index: cfengine3-3.12.1/libpromises/evalfunction.c =================================================================== --- cfengine3-3.12.1.orig/libpromises/evalfunction.c +++ cfengine3-3.12.1/libpromises/evalfunction.c @@ -4595,6 +4595,7 @@ static FnCallResult FnCallSublist(EvalCo /*********************************************************************/ +// TODO: This monstrosity needs refactoring static FnCallResult FnCallSetop(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs) @@ -4629,12 +4630,14 @@ static FnCallResult FnCallSetop(EvalCont // we failed to produce a valid JsonElement, so give up if (json_b == NULL) { + JsonDestroyMaybe(json, allocated); return FnFailure(); } else if (JsonGetElementType(json_b) != JSON_ELEMENT_TYPE_CONTAINER) { Log(LOG_LEVEL_VERBOSE, "Function '%s', argument '%s' was not a data container or list", fp->name, name_str_b); + JsonDestroyMaybe(json, allocated); JsonDestroyMaybe(json_b, allocated_b); return FnFailure(); }
From 4493eda08d6883f5cf1899a4bedd01989e980f27 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem <o...@northern.tech> Date: Sat, 1 Feb 2020 15:31:53 +0100 Subject: [PATCH] Fixed memory leak in handling of inline JSON in policy evaluation RlistAppendScalar already calls `xstrdup`. Found by running binaries (bootstrap) built with AddressSanitizer (ASAN) enabled. Changelog: Title Ticket: None Signed-off-by: Ole Herman Schumacher Elgesem <o...@northern.tech> (cherry picked from commit 84d4fa5008f84e43eb03f0174d9ac904cf420176) --- libpromises/cf3parse.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) Index: cfengine3-3.12.1/libpromises/cf3parse.y =================================================================== --- cfengine3-3.12.1.orig/libpromises/cf3parse.y +++ cfengine3-3.12.1/libpromises/cf3parse.y @@ -650,7 +650,7 @@ constraint: constraint_id if (json == NULL) { Rlist *synthetic_args = NULL; - RlistAppendScalar(&synthetic_args, xstrdup(P.rval.item)); + RlistAppendScalar(&synthetic_args, P.rval.item); RvalDestroy(P.rval); P.rval = (Rval) { FnCallNew(xstrdup(fname), synthetic_args), RVAL_TYPE_FNCALL };
signature.asc
Description: PGP signature