From 4e07dc3d81bc9903dee88a368a3574572b9896a1 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 10 Sep 2025 13:32:44 +0200 Subject: [PATCH 1/4] utest: update to detect the possible memory leak when merging --- .../case_overwrite_success.c | 2 +- .../samconfConfigMergeConfig/fetchapiConfig.h | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/utest/samconf/samconf/samconfConfigMergeConfig/case_overwrite_success.c b/test/utest/samconf/samconf/samconfConfigMergeConfig/case_overwrite_success.c index 2f01c2b..40cfcf2 100644 --- a/test/utest/samconf/samconf/samconfConfigMergeConfig/case_overwrite_success.c +++ b/test/utest/samconf/samconf/samconfConfigMergeConfig/case_overwrite_success.c @@ -25,7 +25,7 @@ void samconfTestSamconfConfigMergeConfigOverwriteSuccess(UNUSED void **state) { SHOULD("%s", "overwrite value of one config to the other"); testConfigToMerge = calloc(1, sizeof(samconfConfig_t)); - result = samconfUtilCreateMockConfigFromStr(_TEST_CONFIG_FETCHAPI, false, testConfigToMerge); + result = samconfUtilCreateMockConfigFromStr(_TEST_CONFIG_FETCHAPI_B, false, testConfigToMerge); assert_int_equal(result, SAMCONF_CONFIG_OK); testDefConfig = calloc(1, sizeof(samconfConfig_t)); diff --git a/test/utest/samconf/samconf/samconfConfigMergeConfig/fetchapiConfig.h b/test/utest/samconf/samconf/samconfConfigMergeConfig/fetchapiConfig.h index c27d456..e679749 100644 --- a/test/utest/samconf/samconf/samconfConfigMergeConfig/fetchapiConfig.h +++ b/test/utest/samconf/samconf/samconfConfigMergeConfig/fetchapiConfig.h @@ -22,3 +22,25 @@ }\ }\ }" + +#define _TEST_CONFIG_FETCHAPI_B \ + "{\ + \"root\": {\ + \"elos\": {\ + \"EventLogging\": {\ + \"Plugins\": {\ + \"fetchapi\": {\ + \"File\": \"backend_fetchapi.so\",\ + \"Run\": \"always\",\ + \"Filter\": [\ + \"1 1 EQ\"\ + ],\ + \"Config\": {\ + \"BufferSize\": \"100\"\ + }\ + }\ + }\ + }\ + }\ + }\ + }" From 474499b19aed46fd59a0ea6af486c2a9a638c7ed Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 10 Sep 2025 14:47:20 +0200 Subject: [PATCH 2/4] fix: memory leak when overwriting nodes of type string --- src/samconf/private/samconf.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/samconf/private/samconf.c b/src/samconf/private/samconf.c index 31fd923..e48b126 100644 --- a/src/samconf/private/samconf.c +++ b/src/samconf/private/samconf.c @@ -1005,6 +1005,9 @@ samconfConfigStatusE_t samconfConfigSetInt(samconfConfig_t *config, int64_t intV samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; if (config != NULL) { + if (config->type == SAMCONF_CONFIG_VALUE_STRING) { + free(config->value.string); + } config->value.integer = intValue; config->type = SAMCONF_CONFIG_VALUE_INT; status = SAMCONF_CONFIG_OK; @@ -1017,6 +1020,9 @@ samconfConfigStatusE_t samconfConfigSetBool(samconfConfig_t *config, bool value) samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; if (config != NULL) { + if (config->type == SAMCONF_CONFIG_VALUE_STRING) { + free(config->value.string); + } config->value.boolean = value; config->type = SAMCONF_CONFIG_VALUE_BOOLEAN; status = SAMCONF_CONFIG_OK; @@ -1029,6 +1035,9 @@ samconfConfigStatusE_t samconfConfigSetReal(samconfConfig_t *config, double valu samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; if (config != NULL) { + if (config->type == SAMCONF_CONFIG_VALUE_STRING) { + free(config->value.string); + } config->value.real = value; config->type = SAMCONF_CONFIG_VALUE_REAL; status = SAMCONF_CONFIG_OK; From 30663cc01b68c101b9f61ecc3201d3e65de0d227 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 10 Sep 2025 14:48:31 +0200 Subject: [PATCH 3/4] utiest(fix): nodes of value type string must be heap allocated --- .../case_success_set_bool.c | 31 ++++++++--------- .../case_success_set_int.c | 33 ++++++++++--------- .../case_success_set_real.c | 31 ++++++++--------- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/test/utest/samconf/samconf/samconfConfigSetBool/case_success_set_bool.c b/test/utest/samconf/samconf/samconfConfigSetBool/case_success_set_bool.c index 966f7b3..d1807fb 100644 --- a/test/utest/samconf/samconf/samconfConfigSetBool/case_success_set_bool.c +++ b/test/utest/samconf/samconf/samconfConfigSetBool/case_success_set_bool.c @@ -1,37 +1,38 @@ // SPDX-License-Identifier: MIT #include +#include #include "samconfConfigSetBool_utest.h" -int samconfTestSamconfConfigSetBoolSuccessSetup(UNUSED void **state) { +int samconfTestSamconfConfigSetBoolSuccessSetup(void **state) { + samconfConfig_t *root = NULL; + samconfConfigNew(&root); + root->key = strdup("node"); + root->type = SAMCONF_CONFIG_VALUE_STRING; + root->value.string = strdup("test"); + *state = root; return 0; } -int samconfTestSamconfConfigSetBoolSuccessTeardown(UNUSED void **state) { +int samconfTestSamconfConfigSetBoolSuccessTeardown(void **state) { + samconfConfig_t *root = *state; + samconfConfigDelete(root); return 0; } -void samconfTestSamconfConfigSetBoolSuccess(UNUSED void **state) { +void samconfTestSamconfConfigSetBoolSuccess(void **state) { + samconfConfig_t *root = *state; bool testValue[] = {true, false}; samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; - samconfConfig_t root = { - .parent = NULL, - .key = "node", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "test", - .children = NULL, - .childCount = 0, - }; - TEST("samconfConfigSetBool"); SHOULD("%s", "return SAMCONF_CONFIG_OK since given boolean value is set"); for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) { - status = samconfConfigSetBool(&root, testValue[i]); + status = samconfConfigSetBool(root, testValue[i]); assert_int_equal(status, SAMCONF_CONFIG_OK); - assert_int_equal(root.type, SAMCONF_CONFIG_VALUE_BOOLEAN); - assert_int_equal(root.value.boolean, testValue[i]); + assert_int_equal(root->type, SAMCONF_CONFIG_VALUE_BOOLEAN); + assert_int_equal(root->value.boolean, testValue[i]); } } diff --git a/test/utest/samconf/samconf/samconfConfigSetInt/case_success_set_int.c b/test/utest/samconf/samconf/samconfConfigSetInt/case_success_set_int.c index bf75640..71c7ac4 100644 --- a/test/utest/samconf/samconf/samconfConfigSetInt/case_success_set_int.c +++ b/test/utest/samconf/samconf/samconfConfigSetInt/case_success_set_int.c @@ -1,37 +1,40 @@ // SPDX-License-Identifier: MIT #include +#include +#include "samconf/samconf.h" +#include "samconf/samconf_types.h" #include "samconfConfigSetInt_utest.h" -int samconfTestSamconfConfigSetIntSuccessSetup(UNUSED void **state) { +int samconfTestSamconfConfigSetIntSuccessSetup(void **state) { + samconfConfig_t *root = NULL; + samconfConfigNew(&root); + root->key = strdup("node"); + root->type = SAMCONF_CONFIG_VALUE_STRING; + root->value.string = strdup("test"); + *state = root; return 0; } -int samconfTestSamconfConfigSetIntSuccessTeardown(UNUSED void **state) { +int samconfTestSamconfConfigSetIntSuccessTeardown(void **state) { + samconfConfig_t *root = *state; + samconfConfigDelete(root); return 0; } -void samconfTestSamconfConfigSetIntSuccess(UNUSED void **state) { +void samconfTestSamconfConfigSetIntSuccess(void **state) { + samconfConfig_t *root = *state; int64_t testValue[] = {0x7F102B892386AF1F, INT64_MAX, 0, INT64_MIN}; samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; - samconfConfig_t root = { - .parent = NULL, - .key = "node", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "test", - .children = NULL, - .childCount = 0, - }; - TEST("samconfConfigSetInt"); SHOULD("%s", "return SAMCONF_CONFIG_OK since given integer value is set"); for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) { - status = samconfConfigSetInt(&root, testValue[i]); + status = samconfConfigSetInt(root, testValue[i]); assert_int_equal(status, SAMCONF_CONFIG_OK); - assert_int_equal(root.type, SAMCONF_CONFIG_VALUE_INT); - assert_int_equal(root.value.integer, testValue[i]); + assert_int_equal(root->type, SAMCONF_CONFIG_VALUE_INT); + assert_int_equal(root->value.integer, testValue[i]); } } diff --git a/test/utest/samconf/samconf/samconfConfigSetReal/case_success_set_real.c b/test/utest/samconf/samconf/samconfConfigSetReal/case_success_set_real.c index c776d27..b0ce12c 100644 --- a/test/utest/samconf/samconf/samconfConfigSetReal/case_success_set_real.c +++ b/test/utest/samconf/samconf/samconfConfigSetReal/case_success_set_real.c @@ -2,38 +2,39 @@ #include #include #include +#include #include "samconfConfigSetReal_utest.h" -int samconfTestSamconfConfigSetRealSuccessSetup(UNUSED void **state) { +int samconfTestSamconfConfigSetRealSuccessSetup(void **state) { + samconfConfig_t *root = NULL; + samconfConfigNew(&root); + root->key = strdup("node"); + root->type = SAMCONF_CONFIG_VALUE_STRING; + root->value.string = strdup("test"); + *state = root; return 0; } -int samconfTestSamconfConfigSetRealSuccessTeardown(UNUSED void **state) { +int samconfTestSamconfConfigSetRealSuccessTeardown(void **state) { + samconfConfig_t *root = *state; + samconfConfigDelete(root); return 0; } -void samconfTestSamconfConfigSetRealSuccess(UNUSED void **state) { +void samconfTestSamconfConfigSetRealSuccess(void **state) { + samconfConfig_t *root = *state; double testValue[] = {6.2898, DBL_MAX, DBL_MIN, 0.0, 3.143}; samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; - samconfConfig_t root = { - .parent = NULL, - .key = "node", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "test", - .children = NULL, - .childCount = 0, - }; - TEST("samconfConfigSetReal"); SHOULD("%s", "return SAMCONF_CONFIG_OK since given real value is set"); for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) { - status = samconfConfigSetReal(&root, testValue[i]); + status = samconfConfigSetReal(root, testValue[i]); assert_int_equal(status, SAMCONF_CONFIG_OK); - assert_int_equal(root.type, SAMCONF_CONFIG_VALUE_REAL); - assert_float_equal(root.value.real, testValue[i], 0.0); + assert_int_equal(root->type, SAMCONF_CONFIG_VALUE_REAL); + assert_float_equal(root->value.real, testValue[i], 0.0); } } From 859f138db5eacc606587b7646393b61bbe426819 Mon Sep 17 00:00:00 2001 From: Armin Butscher Date: Thu, 11 Sep 2025 17:27:41 +0200 Subject: [PATCH 4/4] Update to 0.75.11 --- cmake/project.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/project.cmake b/cmake/project.cmake index f013dcd..a356cf1 100644 --- a/cmake/project.cmake +++ b/cmake/project.cmake @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -set(SAMCONF_VERSION 0.75.8) +set(SAMCONF_VERSION 0.75.11) # Attention: Aside from the version, as many things as possible in this file # should be put into functions, as this solves potential issues with commands