From 2a2fe2614eb1dc3ee3e2dcca7f3aad49a2642c27 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 30 Jan 2024 02:35:00 -0500 Subject: [PATCH] Avoid undefined behavior if "Cable color" menus stay open after a color is deleted. --- src/app/MenuBar.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app/MenuBar.cpp b/src/app/MenuBar.cpp index 7b1329b5..f54b2bbb 100644 --- a/src/app/MenuBar.cpp +++ b/src/app/MenuBar.cpp @@ -467,7 +467,7 @@ struct ViewButton : MenuButton { // Add color items for (size_t i = 0; i < settings::cableColors.size(); i++) { - NVGcolor& color = settings::cableColors[i]; + NVGcolor color = settings::cableColors[i]; ui::ColorDotMenuItem* item = createSubmenuItem(string::uppercase(color::toHexString(color)), string::f("%d", int(i + 1)), [=](ui::Menu* menu) { // Helper for launching color dialog auto selectColor = [](NVGcolor color) { @@ -482,16 +482,18 @@ struct ViewButton : MenuButton { }; menu->addChild(createMenuItem("Set color", "", [=]() { + if (i >= settings::cableColors.size()) + return; NVGcolor newColor = selectColor(color); std::memcpy(&settings::cableColors[i], &newColor, sizeof(newColor)); }, false, true)); menu->addChild(createMenuItem("New color above", "", [=]() { - if (i > settings::cableColors.size()) + if (i >= settings::cableColors.size()) return; settings::cableColors.insert(settings::cableColors.begin() + i, selectColor(color)); }, false, true)); menu->addChild(createMenuItem("New color below", "", [=]() { - if (i + 1 > settings::cableColors.size()) + if (i >= settings::cableColors.size()) return; settings::cableColors.insert(settings::cableColors.begin() + i + 1, selectColor(color)); }, false, true));