aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 21:51:06 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 21:51:06 +0100
commit80ed1262bd654fe2de30389f97a985b5f2c1d783 (patch)
tree020b1b63d637e07882c9a10d81fe2ac04ce82bcc /backend
parentc45a436fc594101f676cfabe90225d825d935fec (diff)
add more containers and fix use after free
Diffstat (limited to 'backend')
-rw-r--r--backend/Dungeon.cpp10
-rw-r--r--backend/Dungeon.h11
-rw-r--r--backend/List.h4
-rw-r--r--backend/List.hpp5
-rw-r--r--backend/Location.cpp6
-rw-r--r--backend/Location.h16
-rw-r--r--backend/PtrList.h14
-rw-r--r--backend/PtrList.hpp10
-rw-r--r--backend/String.cpp1
-rw-r--r--backend/util.h6
-rw-r--r--backend/util.hpp11
11 files changed, 47 insertions, 47 deletions
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<Location *> locations;
+ PtrList<Location> locations;
};
diff --git a/backend/List.h b/backend/List.h
index 58efbe2..1af0b35 100644
--- a/backend/List.h
+++ b/backend/List.h
@@ -18,6 +18,10 @@ struct ListLink {
template<typename T>
class List {
public:
+ List() = default;
+ virtual ~List();
+
+public:
size_t size() const;
void push_back(T & el);
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<T> List<T>::end() const {
return this->range().end();
}
+template <typename T>
+List<T>::~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<Enemy*> enemies;
- List<Object*> hidden_objects;
- List<Object*> visible_objects;
+ PtrList<Enemy> enemies;
+ PtrList<Object> hidden_objects;
+ PtrList<Object> 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<typename T>
+class PtrList : public List<T *> {
+public:
+ using List<T *>::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 <typename T>
+PtrList<T>::~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<char *>(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 <stdio.h>
-#include "List.h"
-
void safe_free(void * & ptr);
void safe_free(char * & ptr);
void safe_free(const char * & ptr);
-template <typename T>
-void safe_free(List<T *> & 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 <typename T>
-void safe_free(List<T *> & ptr_list) {
- for (T * obj : ptr_list)
- delete obj;
- ptr_list.clear();
-}
-