From ac4aeef5792f85e459cb32d911607b4899bd8732 Mon Sep 17 00:00:00 2001 From: philmoz Date: Sat, 5 Aug 2023 18:09:53 +1000 Subject: [PATCH] chore(color): Use strAppend and strncmp on widgetName to avoid termination issues (#3905) Co-authored-by: Phil Mitchell --- radio/src/gui/colorlcd/layouts/topbar_impl.cpp | 6 +++--- radio/src/gui/colorlcd/widgets/widgets_container_impl.h | 6 +++--- radio/src/storage/storage_common.cpp | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp index b8a907261..1fe83a107 100644 --- a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp @@ -96,12 +96,12 @@ void TopbarImpl::removeWidget(unsigned int index) bool mark = false; // If user manually removes 'system' widgets, mark name so widget does not get reloaded on restart - if ((index == MAX_TOPBAR_ZONES - 1) && (strcmp(persistentData->zones[index].widgetName, "Date Time") == 0)) + if ((index == MAX_TOPBAR_ZONES - 1) && (strncmp(persistentData->zones[index].widgetName, "Date Time", WIDGET_NAME_LEN) == 0)) mark = true; - if ((index == MAX_TOPBAR_ZONES - 2) && (strcmp(persistentData->zones[index].widgetName, "Radio Info") == 0)) + if ((index == MAX_TOPBAR_ZONES - 2) && (strncmp(persistentData->zones[index].widgetName, "Radio Info", WIDGET_NAME_LEN) == 0)) mark = true; #if defined(INTERNAL_GPS) - if ((index == MAX_TOPBAR_ZONES - 3) && (strcmp(persistentData->zones[index].widgetName, "Internal GPS") == 0)) + if ((index == MAX_TOPBAR_ZONES - 3) && (strncmp(persistentData->zones[index].widgetName, "Internal GPS", WIDGET_NAME_LEN) == 0)) mark = true; #endif diff --git a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h index 6764f265b..238b715ad 100644 --- a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h +++ b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h @@ -24,6 +24,7 @@ #include "widget.h" #include "widgets_container.h" #include "draw_functions.h" +#include "strhelpers.h" template class WidgetsContainerImpl : public WidgetsContainer @@ -48,8 +49,7 @@ class WidgetsContainerImpl : public WidgetsContainer Widget* widget = nullptr; if (factory) { - strncpy(persistentData->zones[index].widgetName, factory->getName(), - sizeof(ZonePersistentData::widgetName)); + strAppend(persistentData->zones[index].widgetName, factory->getName(), WIDGET_NAME_LEN); widget = factory->create(this, getZone(index), &persistentData->zones[index].widgetData); } @@ -100,7 +100,7 @@ class WidgetsContainerImpl : public WidgetsContainer if (persistentData->zones[i].widgetName[0]) { char name[WIDGET_NAME_LEN + 1]; memset(name, 0, sizeof(name)); - strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN); + strAppend(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN); widgets[i] = loadWidget(name, this, getZone(i), &persistentData->zones[i].widgetData); } diff --git a/radio/src/storage/storage_common.cpp b/radio/src/storage/storage_common.cpp index 4c83d37a5..d10c244db 100644 --- a/radio/src/storage/storage_common.cpp +++ b/radio/src/storage/storage_common.cpp @@ -142,20 +142,20 @@ void postModelLoad(bool alarms) #if defined(COLORLCD) // Load 'date time' widget if slot is empty if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetName[0] == 0) { - strcpy(g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetName, "Date Time"); + strAppend(g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetName, "Date Time", WIDGET_NAME_LEN); g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetData.options[0].type = ZOV_Color; g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetData.options[0].value.unsignedValue = 0xFFFFFF; storageDirty(EE_MODEL); } // Load 'radio info' widget if slot is empty if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-2].widgetName[0] == 0) { - strcpy(g_model.topbarData.zones[MAX_TOPBAR_ZONES-2].widgetName, "Radio Info"); + strAppend(g_model.topbarData.zones[MAX_TOPBAR_ZONES-2].widgetName, "Radio Info", WIDGET_NAME_LEN); storageDirty(EE_MODEL); } #if defined(INTERNAL_GPS) // Load 'internal gps' widget if slot is empty if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-3].widgetName[0] == 0) { - strcpy(g_model.topbarData.zones[MAX_TOPBAR_ZONES-3].widgetName, "Internal GPS"); + strAppend(g_model.topbarData.zones[MAX_TOPBAR_ZONES-3].widgetName, "Internal GPS", WIDGET_NAME_LEN); storageDirty(EE_MODEL); } #endif