From 80ed1262bd654fe2de30389f97a985b5f2c1d783 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 21:51:06 +0100 Subject: add more containers and fix use after free --- backend/Dungeon.cpp | 10 ---------- backend/Dungeon.h | 11 +++++------ backend/List.h | 4 ++++ backend/List.hpp | 5 +++++ backend/Location.cpp | 6 ------ backend/Location.h | 16 ++++++++-------- backend/PtrList.h | 14 ++++++++++++++ backend/PtrList.hpp | 10 ++++++++++ backend/String.cpp | 1 + backend/util.h | 6 ------ backend/util.hpp | 11 ----------- frontend/cmd/search.cpp | 5 ++++- 12 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 backend/PtrList.h create mode 100644 backend/PtrList.hpp delete mode 100644 backend/util.hpp diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index e1cee70..d4df1ef 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -1,16 +1,6 @@ #include "Location.h" #include "Dungeon.h" #include "RNG.h" -#include "util.h" - - -Dungeon::Dungeon() { - -} - -Dungeon::~Dungeon() { - safe_free(this->locations); -} void Dungeon::update() { // TODO: iterators are broken (!????) diff --git a/backend/Dungeon.h b/backend/Dungeon.h index eaca6c3..be8b52e 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -1,13 +1,12 @@ #pragma once -#include "List.h" - -class Location; +#include "Location.h" +#include "PtrList.h" class Dungeon { public: - Dungeon(); - ~Dungeon(); + Dungeon() = default; + virtual ~Dungeon() = default; public: void update(); @@ -15,7 +14,7 @@ public: Location * get_start_location(); private: - List locations; + PtrList locations; }; diff --git a/backend/List.h b/backend/List.h index 58efbe2..1af0b35 100644 --- a/backend/List.h +++ b/backend/List.h @@ -17,6 +17,10 @@ struct ListLink { template class List { +public: + List() = default; + virtual ~List(); + public: size_t size() const; diff --git a/backend/List.hpp b/backend/List.hpp index c2c5ce2..f4dce23 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -85,3 +85,8 @@ ListIterator List::end() const { return this->range().end(); } +template +List::~List() { + this->clear(); +} + diff --git a/backend/Location.cpp b/backend/Location.cpp index 7246246..bfd2107 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -20,12 +20,6 @@ Direction random_direction(const Location & location) { Location::Location(const String & name, const String & description) : name(name), description(description) { } -Location::~Location() { - safe_free(this->enemies); - safe_free(this->hidden_objects); - safe_free(this->visible_objects); -} - void Location::set_name(const String & name) { this->name = name; } const String & Location::get_name() const { return this->name; } diff --git a/backend/Location.h b/backend/Location.h index 464328b..3a1f5ac 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -1,11 +1,11 @@ #pragma once -#include "List.h" +#include "PtrList.h" #include "ListIterator.h" -#include "backend/String.h" +#include "String.h" +#include "Enemy.h" +#include "Object.h" -class Enemy; -class Object; class Location; enum Direction { @@ -49,14 +49,14 @@ private: friend class LocationFactory; Location(const String & name, const String & description); public: - virtual ~Location(); + virtual ~Location() = default; private: String name; String description; - List enemies; - List hidden_objects; - List visible_objects; + PtrList enemies; + PtrList hidden_objects; + PtrList visible_objects; Location * edges[4] = { nullptr, diff --git a/backend/PtrList.h b/backend/PtrList.h new file mode 100644 index 0000000..5442043 --- /dev/null +++ b/backend/PtrList.h @@ -0,0 +1,14 @@ +#pragma once + +#include "List.h" + +template +class PtrList : public List { +public: + using List::List; + virtual ~PtrList(); +}; + +#include "PtrList.hpp" + + diff --git a/backend/PtrList.hpp b/backend/PtrList.hpp new file mode 100644 index 0000000..96f2643 --- /dev/null +++ b/backend/PtrList.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "PtrList.h" + +template +PtrList::~PtrList() { + for (T * obj : *this) + delete obj; +} + diff --git a/backend/String.cpp b/backend/String.cpp index e387589..a8b648a 100644 --- a/backend/String.cpp +++ b/backend/String.cpp @@ -34,6 +34,7 @@ String String::va_fmt(va_list args, const char * fmt) { out._data_len = vsnprintf(NULL, 0, fmt, args_copy); va_end(args_copy); + safe_free(out._data); out._data = static_cast(malloc(out._data_len + 1)); vsnprintf(out._data, out._data_len + 1, fmt, args); return out; diff --git a/backend/util.h b/backend/util.h index e544e19..83cae58 100644 --- a/backend/util.h +++ b/backend/util.h @@ -2,14 +2,8 @@ #include -#include "List.h" - void safe_free(void * & ptr); void safe_free(char * & ptr); void safe_free(const char * & ptr); -template -void safe_free(List & ptr_list); void safe_free(FILE * & ptr); -#include "util.hpp" - diff --git a/backend/util.hpp b/backend/util.hpp deleted file mode 100644 index 2d9e76a..0000000 --- a/backend/util.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "util.h" - -template -void safe_free(List & ptr_list) { - for (T * obj : ptr_list) - delete obj; - ptr_list.clear(); -} - diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp index f10709a..16e4592 100644 --- a/frontend/cmd/search.cpp +++ b/frontend/cmd/search.cpp @@ -7,14 +7,17 @@ using namespace std; FollowupAction Player::cmd_search(string &) { bool found = false; + List to_unhide; for (Object * object : this->location.get_hidden_objects()) { if (!found) lprtf("Je vindt:\n"); lprtf("- %s\n", object->get_displayname().c_str()); - this->location.unhide_object(object); + to_unhide.push_back(object); found = true; } if (!found) lprtf("Je hebt niks gevonden.\n"); + for (Object * object : to_unhide) + this->location.unhide_object(object); return FollowupAction::UPDATE; } -- cgit v1.2.3