From ff9303861a0e24c3b96af4c3e64fb598b8b57f86 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Tue, 27 May 2025 21:47:02 +0200 Subject: [PATCH] Fix menu double free --- Makefile | 2 +- src/menu.cpp | 47 +++++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index eb2c7d3..0272abf 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CPPC = g++ CPPC_FLAGS = -std=c++23 -s -O3 -Wall -Wextra -lncurses -lmenu -lform -lcups DEBUG_FLAGS = -ggdb -std=c++23 -Wall -lncurses -lmenu -lform -lcups -DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncurses -lmenu -lcups -Wall -Wextra +DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncurses -lmenu -lform -lcups -Wall -Wextra SRC_PATH := src diff --git a/src/menu.cpp b/src/menu.cpp index 6aa9771..6f4683f 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,13 +1,9 @@ #include #include #include -#include #include -#include -#include #include #include -#include #include #include #include "const.h" @@ -39,30 +35,33 @@ void menu() { // Create items complete_menu main_menu = {nullptr, nullptr, 0, nullptr}; main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1}); - std::vector items; - if (std::filesystem::exists(COMPLAINTS_DIR)) { - for (const auto& directroy_entry : - std::filesystem::directory_iterator(COMPLAINTS_DIR)) { - if (directroy_entry.is_regular_file()) { - std::string name_date[2]; - std::stringstream ssfn(directroy_entry.path().filename().string()); - for (uint8_t i = 0; i < 2; i++) { - std::getline(ssfn, name_date[i], '_'); + { + std::vector items; + if (std::filesystem::exists(COMPLAINTS_DIR)) { + for (const auto& directroy_entry : + std::filesystem::directory_iterator(COMPLAINTS_DIR)) { + if (directroy_entry.is_regular_file()) { + std::string name_date[2]; + std::stringstream ssfn(directroy_entry.path().filename().string()); + for (uint8_t i = 0; i < 2; i++) { + std::getline(ssfn, name_date[i], '_'); + } + char* name = new char[name_date[0].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, name, 1}); + char* date = new char[name_date[1].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, date, 1}); + strcpy(name, name_date[0].c_str()); + strcpy(date, name_date[1].c_str()); + items.push_back(new_item(name, date)); } - char* name = new char[name_date[0].length() + 1]; - main_menu_allocated.push_back({GENERIC_TYPE, name, 1}); - char* date = new char[name_date[1].length() + 1]; - main_menu_allocated.push_back({GENERIC_TYPE, date, 1}); - strcpy(name, name_date[0].c_str()); - strcpy(date, name_date[1].c_str()); - items.push_back(new_item(name, date)); } + } else { + std::filesystem::create_directory(COMPLAINTS_DIR); } - } else { - std::filesystem::create_directory(COMPLAINTS_DIR); + items.push_back(nullptr); + main_menu.items = new ITEM*[items.size()]; + memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM*))); } - items.push_back(nullptr); - main_menu.items = items.data(); /* Crate menu */ main_menu.menu = new_menu(main_menu.items);