From f5593c44e8cac29b4f5f5451b5140cc82984ed73 Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 31 Oct 2023 12:40:49 +1100 Subject: [PATCH] fix(color): Bitmap name truncation if exactly 14 characters long (#4232) Co-authored-by: Peter Feerick --- radio/src/gui/212x64/model_setup.cpp | 10 +++++----- radio/src/gui/colorlcd/model_setup.cpp | 13 +++++-------- radio/src/gui/colorlcd/radio_sdmanager.cpp | 8 +++++--- radio/src/gui/colorlcd/widgets/modelbmp.cpp | 2 +- radio/src/gui/common/stdlcd/radio_sdmanager.cpp | 6 +++--- radio/src/lua/api_model.cpp | 2 +- radio/src/storage/modelslist.cpp | 2 +- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/radio/src/gui/212x64/model_setup.cpp b/radio/src/gui/212x64/model_setup.cpp index d06d5621f..624d3cf3b 100644 --- a/radio/src/gui/212x64/model_setup.cpp +++ b/radio/src/gui/212x64/model_setup.cpp @@ -239,14 +239,14 @@ void copySelection(char * dst, const char * src, uint8_t size) void onModelSetupBitmapMenu(const char * result) { if (result == STR_UPDATE_LIST) { - if (!sdListFiles(BITMAPS_PATH, BITMAPS_EXT, sizeof(g_model.header.bitmap), nullptr)) { + if (!sdListFiles(BITMAPS_PATH, BITMAPS_EXT, LEN_BITMAP_NAME, nullptr)) { POPUP_WARNING(STR_NO_BITMAPS_ON_SD); } } else if (result != STR_EXIT) { // The user choosed a bmp file in the list - copySelection(g_model.header.bitmap, result, sizeof(g_model.header.bitmap)); - memcpy(modelHeaders[g_eeGeneral.currModel].bitmap, g_model.header.bitmap, sizeof(g_model.header.bitmap)); + copySelection(g_model.header.bitmap, result, LEN_BITMAP_NAME); + memcpy(modelHeaders[g_eeGeneral.currModel].bitmap, g_model.header.bitmap, LEN_BITMAP_NAME); storageDirty(EE_MODEL); } } @@ -641,12 +641,12 @@ void menuModelSetup(event_t event) case ITEM_MODEL_SETUP_BITMAP: lcdDrawTextAlignedLeft(y, STR_BITMAP); if (ZEXIST(g_model.header.bitmap)) - lcdDrawSizedText(MODEL_SETUP_2ND_COLUMN, y, g_model.header.bitmap, sizeof(g_model.header.bitmap), attr); + lcdDrawSizedText(MODEL_SETUP_2ND_COLUMN, y, g_model.header.bitmap, LEN_BITMAP_NAME, attr); else lcdDrawTextAtIndex(MODEL_SETUP_2ND_COLUMN, y, STR_VCSWFUNC, 0, attr); if (attr && event==EVT_KEY_BREAK(KEY_ENTER) && READ_ONLY_UNLOCKED()) { s_editMode = 0; - if (sdListFiles(BITMAPS_PATH, BITMAPS_EXT, sizeof(g_model.header.bitmap), g_model.header.bitmap, LIST_NONE_SD_FILE)) { + if (sdListFiles(BITMAPS_PATH, BITMAPS_EXT, LEN_BITMAP_NAME, g_model.header.bitmap, LIST_NONE_SD_FILE)) { POPUP_MENU_START(onModelSetupBitmapMenu); } else { diff --git a/radio/src/gui/colorlcd/model_setup.cpp b/radio/src/gui/colorlcd/model_setup.cpp index 9d9440027..2a60bb732 100644 --- a/radio/src/gui/colorlcd/model_setup.cpp +++ b/radio/src/gui/colorlcd/model_setup.cpp @@ -66,19 +66,16 @@ struct ModelNameEdit : public ModelTextEdit { static std::string getModelBitmap() { - return std::string(g_model.header.bitmap, sizeof(g_model.header.bitmap)); + return std::string(g_model.header.bitmap, LEN_BITMAP_NAME); } static void setModelBitmap(std::string newValue) { - strncpy(g_model.header.bitmap, newValue.c_str(), - sizeof(g_model.header.bitmap)); - g_model.header.bitmap[sizeof(g_model.header.bitmap)-1] = '\0'; + strncpy(g_model.header.bitmap, newValue.c_str(), LEN_BITMAP_NAME); auto model = modelslist.getCurrentModel(); if (model) { - strncpy(model->modelBitmap, newValue.c_str(), - sizeof(ModelCell::modelBitmap)); - model->modelBitmap[sizeof(ModelCell::modelBitmap)-1] = '\0'; + strncpy(model->modelBitmap, newValue.c_str(), LEN_BITMAP_NAME); + model->modelBitmap[LEN_BITMAP_NAME] = '\0'; } SET_DIRTY(); } @@ -86,7 +83,7 @@ static void setModelBitmap(std::string newValue) struct ModelBitmapEdit : public FileChoice { ModelBitmapEdit(Window *parent, const rect_t &rect) : FileChoice(parent, rect, BITMAPS_PATH, BITMAPS_EXT, - sizeof(g_model.header.bitmap), getModelBitmap, setModelBitmap) + LEN_BITMAP_NAME, getModelBitmap, setModelBitmap) { } }; diff --git a/radio/src/gui/colorlcd/radio_sdmanager.cpp b/radio/src/gui/colorlcd/radio_sdmanager.cpp index 1028642fa..5ea3f4592 100644 --- a/radio/src/gui/colorlcd/radio_sdmanager.cpp +++ b/radio/src/gui/colorlcd/radio_sdmanager.cpp @@ -363,12 +363,14 @@ void RadioSdManagerPage::fileAction(const char* path, const char* name, MultiFirmwareUpdate(fullpath, EXTERNAL_MODULE, MULTI_TYPE_ELRS); }); } else if (!strcasecmp(BITMAPS_PATH, path) && - isExtensionMatching(ext, BITMAPS_EXT)) { + isExtensionMatching(ext, BITMAPS_EXT) && + strlen(name) <= LEN_BITMAP_NAME) { menu->addLine(STR_ASSIGN_BITMAP, [=]() { - memcpy(g_model.header.bitmap, name, sizeof(g_model.header.bitmap)); + memcpy(g_model.header.bitmap, name, LEN_BITMAP_NAME); storageDirty(EE_MODEL); }); - } else if (!strcasecmp(ext, TEXT_EXT) || !strcasecmp(ext, LOGS_EXT) || !strcasecmp(ext, SCRIPT_EXT)) { + } else if (!strcasecmp(ext, TEXT_EXT) || !strcasecmp(ext, LOGS_EXT) || + !strcasecmp(ext, SCRIPT_EXT)) { menu->addLine(STR_VIEW_TEXT, [=]() { FIL file; if (FR_OK == f_open(&file, fullpath, FA_OPEN_EXISTING | FA_READ)) { diff --git a/radio/src/gui/colorlcd/widgets/modelbmp.cpp b/radio/src/gui/colorlcd/widgets/modelbmp.cpp index c8056c2e4..632f60a22 100644 --- a/radio/src/gui/colorlcd/widgets/modelbmp.cpp +++ b/radio/src/gui/colorlcd/widgets/modelbmp.cpp @@ -97,7 +97,7 @@ class ModelBitmapWidget: public Widget uint32_t getHash() { - return hash(g_model.header.bitmap, sizeof(g_model.header.bitmap)); + return hash(g_model.header.bitmap, LEN_BITMAP_NAME); } void loadBitmap() diff --git a/radio/src/gui/common/stdlcd/radio_sdmanager.cpp b/radio/src/gui/common/stdlcd/radio_sdmanager.cpp index 760a68383..f25d021f7 100644 --- a/radio/src/gui/common/stdlcd/radio_sdmanager.cpp +++ b/radio/src/gui/common/stdlcd/radio_sdmanager.cpp @@ -170,8 +170,8 @@ void onSdManagerMenu(const char * result) } #if LCD_DEPTH > 1 else if (result == STR_ASSIGN_BITMAP) { - strAppendFilename(g_model.header.bitmap, line, sizeof(g_model.header.bitmap)); - memcpy(modelHeaders[g_eeGeneral.currModel].bitmap, g_model.header.bitmap, sizeof(g_model.header.bitmap)); + strAppendFilename(g_model.header.bitmap, line, LEN_BITMAP_NAME); + memcpy(modelHeaders[g_eeGeneral.currModel].bitmap, g_model.header.bitmap, LEN_BITMAP_NAME); storageDirty(EE_MODEL); } #endif @@ -363,7 +363,7 @@ void menuRadioSdManager(event_t _event) } #if LCD_DEPTH > 1 else if (isExtensionMatching(ext, BITMAPS_EXT)) { - if (!READ_ONLY() && (ext-line) <= (int)sizeof(g_model.header.bitmap)) { + if (!READ_ONLY() && (ext-line) <= LEN_BITMAP_NAME) { POPUP_MENU_ADD_ITEM(STR_ASSIGN_BITMAP); } } diff --git a/radio/src/lua/api_model.cpp b/radio/src/lua/api_model.cpp index edb6d587d..5b67f6ccb 100644 --- a/radio/src/lua/api_model.cpp +++ b/radio/src/lua/api_model.cpp @@ -112,7 +112,7 @@ static int luaModelSetInfo(lua_State *L) #if LCD_DEPTH > 1 else if (!strcmp(key, "bitmap")) { const char * name = luaL_checkstring(L, -1); - strncpy(g_model.header.bitmap, name, sizeof(g_model.header.bitmap)); + strncpy(g_model.header.bitmap, name, LEN_BITMAP_NAME); } #endif } diff --git a/radio/src/storage/modelslist.cpp b/radio/src/storage/modelslist.cpp index 7d2ecab02..1fa3e28d9 100644 --- a/radio/src/storage/modelslist.cpp +++ b/radio/src/storage/modelslist.cpp @@ -1350,7 +1350,7 @@ void ModelsList::updateCurrentModelCell() if (currentModel) { #if LEN_BITMAP_NAME > 0 strncpy(currentModel->modelBitmap, g_model.header.bitmap, LEN_BITMAP_NAME); - currentModel->modelBitmap[LEN_BITMAP_NAME - 1] = '\0'; + currentModel->modelBitmap[LEN_BITMAP_NAME] = '\0'; #endif strncpy(currentModel->modelFilename, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME); currentModel->modelFilename[LEN_MODEL_FILENAME] = '\0';