From 121a1ffee5e4d2dbb909e7a4e5a012bf81d6b32c Mon Sep 17 00:00:00 2001 From: Peter Feerick Date: Fri, 21 Jan 2022 19:53:45 +1000 Subject: [PATCH] fix: Potential out of bounds access of flightmode name on main screen (#1337) Take 2, without the Mac Companion breakage... * fixed incorrect use of non-terminated-fixed-string for ctor of std::string * introduced helper * removed commented lines * removed constexpr since in C++11 std::string ctro isn't still constexpr Co-authored-by: wimalopaan --- radio/src/gui/colorlcd/view_main_decoration.cpp | 4 ++-- radio/src/strhelpers.h | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/radio/src/gui/colorlcd/view_main_decoration.cpp b/radio/src/gui/colorlcd/view_main_decoration.cpp index 0066efef0..a156eb015 100644 --- a/radio/src/gui/colorlcd/view_main_decoration.cpp +++ b/radio/src/gui/colorlcd/view_main_decoration.cpp @@ -328,8 +328,8 @@ void ViewMainDecoration::createFlightMode() }; std::function getFM = []() -> std::string { - return g_model.flightModeData[mixerCurrentFlightMode].name; - }; + return stringFromNtString(g_model.flightModeData[mixerCurrentFlightMode].name); +}; flightMode = new DynamicText(this, r, getFM, CENTERED | COLOR_THEME_SECONDARY1); } diff --git a/radio/src/strhelpers.h b/radio/src/strhelpers.h index 683de26d4..5ed7d8807 100644 --- a/radio/src/strhelpers.h +++ b/radio/src/strhelpers.h @@ -25,6 +25,8 @@ #include "definitions.h" #include "opentx_types.h" +#include + #define SHOW_TIME 0x1 #define SHOW_TIMER 0x0 #define SHOW_TIMER_UPPER_CASE 0x2 @@ -90,4 +92,8 @@ char *getTimerString(int32_t tme, TimerOptions timerOptions = {.options = 0}); void splitTimer(char *s0, char *s1, char *s2, char *s3, int tme, bool bLowercase = true); +template +std::string stringFromNtString(const char (&a)[N]) { + return std::string(a, strnlen(a, N)); +} #endif // _STRHELPERS_H_