From d84fd56329425831bf5f7170dc3e6e37450f1219 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 17 Nov 2023 13:50:27 +0100 Subject: [PATCH] Free config items properly on early return --- libraries/libwups/WUPSConfigItemBoolean.cpp | 22 ++++++---- .../libwups/WUPSConfigItemIntegerRange.cpp | 14 +++++-- .../libwups/WUPSConfigItemMultipleValues.cpp | 42 ++++++++++--------- libraries/libwups/WUPSConfigItemStub.cpp | 18 ++++---- 4 files changed, 56 insertions(+), 40 deletions(-) diff --git a/libraries/libwups/WUPSConfigItemBoolean.cpp b/libraries/libwups/WUPSConfigItemBoolean.cpp index e74f10d..4695df1 100644 --- a/libraries/libwups/WUPSConfigItemBoolean.cpp +++ b/libraries/libwups/WUPSConfigItemBoolean.cpp @@ -4,8 +4,6 @@ #include #include -void WUPSConfigItemBoolean_onDelete(void *context); - int32_t WUPSConfigItemBoolean_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { auto *item = (ConfigItemBoolean *) context; snprintf(out_buf, out_size, " %s", item->value ? item->trueValue : item->falseValue); @@ -58,6 +56,18 @@ void WUPSConfigItemBoolean_restoreDefault(void *context) { void WUPSConfigItemBoolean_onSelected(void *context, bool isSelected) { } +static void WUPSConfigItemBoolean_Cleanup(ConfigItemBoolean *item) { + if (!item) { + return; + } + free(item->configId); + free(item); +} + +void WUPSConfigItemBoolean_onDelete(void *context) { + WUPSConfigItemBoolean_Cleanup((ConfigItemBoolean *) context); +} + extern "C" bool WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback, const char *trueValue, const char *falseValue) { @@ -92,7 +102,7 @@ WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char * .onDelete = &WUPSConfigItemBoolean_onDelete}; if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) { - free(item); + WUPSConfigItemBoolean_Cleanup(item); return false; } @@ -103,12 +113,6 @@ WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char * return true; } -void WUPSConfigItemBoolean_onDelete(void *context) { - auto *item = (ConfigItemBoolean *) context; - free(item->configId); - free(item); -} - extern "C" bool WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback) { return WUPSConfigItemBoolean_AddToCategoryEx(cat, configID, displayName, defaultValue, callback, "true", "false"); } diff --git a/libraries/libwups/WUPSConfigItemIntegerRange.cpp b/libraries/libwups/WUPSConfigItemIntegerRange.cpp index e685941..4065086 100644 --- a/libraries/libwups/WUPSConfigItemIntegerRange.cpp +++ b/libraries/libwups/WUPSConfigItemIntegerRange.cpp @@ -59,13 +59,19 @@ void WUPSConfigItemIntegerRange_restoreDefault(void *context) { item->value = item->defaultValue; } -void WUPSConfigItemIntegerRange_onDelete(void *context) { - auto *item = (ConfigItemIntegerRange *) context; +void WUPSConfigItemIntegerRange_onSelected(void *context, bool isSelected) { +} + +static void WUPSConfigItemIntegerRange_Cleanup(ConfigItemIntegerRange *item) { + if (!item) { + return; + } free(item->configId); free(item); } -void WUPSConfigItemIntegerRange_onSelected(void *context, bool isSelected) { +void WUPSConfigItemIntegerRange_onDelete(void *context) { + WUPSConfigItemIntegerRange_Cleanup((ConfigItemIntegerRange *) context); } extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int32_t defaultValue, int32_t minValue, int32_t maxValue, @@ -101,7 +107,7 @@ extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandl .onDelete = &WUPSConfigItemIntegerRange_onDelete}; if (WUPSConfigItem_Create(&(item->handle), configId, displayName, callbacks, item) < 0) { - free(item); + WUPSConfigItemIntegerRange_Cleanup(item); return false; }; diff --git a/libraries/libwups/WUPSConfigItemMultipleValues.cpp b/libraries/libwups/WUPSConfigItemMultipleValues.cpp index a1dc804..acbbdbb 100644 --- a/libraries/libwups/WUPSConfigItemMultipleValues.cpp +++ b/libraries/libwups/WUPSConfigItemMultipleValues.cpp @@ -4,8 +4,6 @@ #include #include -void WUPSConfigItemMultipleValues_onDelete(void *context); - int32_t WUPSConfigItemMultipleValues_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { auto *item = (ConfigItemMultipleValues *) context; @@ -72,6 +70,27 @@ void WUPSConfigItemMultipleValues_restoreDefault(void *context) { void WUPSConfigItemMultipleValues_onSelected(void *context, bool isSelected) { } +static void WUPSConfigItemMultipleValues_Cleanup(ConfigItemMultipleValues *item) { + if (!item) { + return; + } + + for (int i = 0; i < item->valueCount; ++i) { + free(item->values[i].valueName); + } + + free(item->configId); + free(item->values); + + free(item); +} + +void WUPSConfigItemMultipleValues_onDelete(void *context) { + auto *item = (ConfigItemMultipleValues *) context; + + WUPSConfigItemMultipleValues_Cleanup(item); +} + extern "C" bool WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int32_t defaultValueIndex, ConfigItemMultipleValuesPair *possibleValues, @@ -92,9 +111,7 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c values[i].valueName = nullptr; continue; } - auto bufLen = strlen(possibleValues[i].valueName) + 1; - values[i].valueName = (char *) malloc(bufLen); - strncpy(values[i].valueName, possibleValues[i].valueName, bufLen); + values[i].valueName = strdup(possibleValues[i].valueName); } item->valueCount = pairCount; @@ -120,7 +137,7 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c .onDelete = &WUPSConfigItemMultipleValues_onDelete}; if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) { - free(item); + WUPSConfigItemMultipleValues_Cleanup(item); return false; } @@ -129,17 +146,4 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c return false; } return true; -} - -void WUPSConfigItemMultipleValues_onDelete(void *context) { - auto *item = (ConfigItemMultipleValues *) context; - - for (int i = 0; i < item->valueCount; ++i) { - free(item->values[i].valueName); - } - - free(item->configId); - free(item->values); - - free(item); } \ No newline at end of file diff --git a/libraries/libwups/WUPSConfigItemStub.cpp b/libraries/libwups/WUPSConfigItemStub.cpp index 1824f0f..94601e4 100644 --- a/libraries/libwups/WUPSConfigItemStub.cpp +++ b/libraries/libwups/WUPSConfigItemStub.cpp @@ -4,8 +4,6 @@ #include #include -void WUPSConfigItemStub_onDelete(void *context); - int32_t WUPSConfigItemStub_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { memset(out_buf, 0, out_size); return 0; @@ -33,6 +31,15 @@ void WUPSConfigItemStub_restoreDefault(void *context) { void WUPSConfigItemStub_onSelected(void *context, bool isSelected) { } +static void WUPSConfigItemStub_Cleanup(ConfigItemStub *item) { + free(item); +} + +void WUPSConfigItemStub_onDelete(void *context) { + WUPSConfigItemStub_Cleanup((ConfigItemStub *) context); +} + + extern "C" bool WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) { if (cat == 0) { @@ -55,7 +62,7 @@ WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *con .onDelete = &WUPSConfigItemStub_onDelete}; if (WUPSConfigItem_Create(&item->handle, configID, displayName, callbacks, item) < 0) { - free(item); + WUPSConfigItemStub_Cleanup(item); return false; } @@ -66,11 +73,6 @@ WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *con return true; } -void WUPSConfigItemStub_onDelete(void *context) { - auto *item = (ConfigItemStub *) context; - free(item); -} - extern "C" bool WUPSConfigItemStub_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) { return WUPSConfigItemStub_AddToCategoryEx(cat, configID, displayName); }