chore(color): Use strAppend and strncmp on widgetName to avoid termination issues (#3905)

Co-authored-by: Phil Mitchell <phil.a.mitchell@gmail.com>
This commit is contained in:
philmoz
2023-08-05 18:09:53 +10:00
committed by GitHub
co-authored by Phil Mitchell
parent 659a25a6cd
commit ac4aeef579
3 changed files with 9 additions and 9 deletions
@@ -96,12 +96,12 @@ void TopbarImpl::removeWidget(unsigned int index)
bool mark = false; bool mark = false;
// If user manually removes 'system' widgets, mark name so widget does not get reloaded on restart // 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; 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; mark = true;
#if defined(INTERNAL_GPS) #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; mark = true;
#endif #endif
@@ -24,6 +24,7 @@
#include "widget.h" #include "widget.h"
#include "widgets_container.h" #include "widgets_container.h"
#include "draw_functions.h" #include "draw_functions.h"
#include "strhelpers.h"
template <int N, int O> template <int N, int O>
class WidgetsContainerImpl : public WidgetsContainer class WidgetsContainerImpl : public WidgetsContainer
@@ -48,8 +49,7 @@ class WidgetsContainerImpl : public WidgetsContainer
Widget* widget = nullptr; Widget* widget = nullptr;
if (factory) { if (factory) {
strncpy(persistentData->zones[index].widgetName, factory->getName(), strAppend(persistentData->zones[index].widgetName, factory->getName(), WIDGET_NAME_LEN);
sizeof(ZonePersistentData::widgetName));
widget = factory->create(this, getZone(index), widget = factory->create(this, getZone(index),
&persistentData->zones[index].widgetData); &persistentData->zones[index].widgetData);
} }
@@ -100,7 +100,7 @@ class WidgetsContainerImpl : public WidgetsContainer
if (persistentData->zones[i].widgetName[0]) { if (persistentData->zones[i].widgetName[0]) {
char name[WIDGET_NAME_LEN + 1]; char name[WIDGET_NAME_LEN + 1];
memset(name, 0, sizeof(name)); 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), widgets[i] = loadWidget(name, this, getZone(i),
&persistentData->zones[i].widgetData); &persistentData->zones[i].widgetData);
} }
+3 -3
View File
@@ -142,20 +142,20 @@ void postModelLoad(bool alarms)
#if defined(COLORLCD) #if defined(COLORLCD)
// Load 'date time' widget if slot is empty // Load 'date time' widget if slot is empty
if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetName[0] == 0) { 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].type = ZOV_Color;
g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetData.options[0].value.unsignedValue = 0xFFFFFF; g_model.topbarData.zones[MAX_TOPBAR_ZONES-1].widgetData.options[0].value.unsignedValue = 0xFFFFFF;
storageDirty(EE_MODEL); storageDirty(EE_MODEL);
} }
// Load 'radio info' widget if slot is empty // Load 'radio info' widget if slot is empty
if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-2].widgetName[0] == 0) { 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); storageDirty(EE_MODEL);
} }
#if defined(INTERNAL_GPS) #if defined(INTERNAL_GPS)
// Load 'internal gps' widget if slot is empty // Load 'internal gps' widget if slot is empty
if (g_model.topbarData.zones[MAX_TOPBAR_ZONES-3].widgetName[0] == 0) { 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); storageDirty(EE_MODEL);
} }
#endif #endif