From c282663a96ae9704a59c55c9300dbc8c49e0ef39 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Mon, 18 Nov 2019 09:41:22 -0800 Subject: Add sanity checks for overwriting buffers --- Source/Puzzle.h | 4 ++-- Source/PuzzleSerializer.cpp | 15 +++++++++++++++ Source/Randomizer2.cpp | 20 ++++++++++---------- Source/Randomizer2Core.cpp | 30 +++++++++--------------------- Source/Randomizer2Core.h | 3 +-- 5 files changed, 37 insertions(+), 35 deletions(-) (limited to 'Source') diff --git a/Source/Puzzle.h b/Source/Puzzle.h index 962874e..1e00ef4 100644 --- a/Source/Puzzle.h +++ b/Source/Puzzle.h @@ -65,8 +65,8 @@ struct Pos {int x; int y;}; class Puzzle { public: - int16_t height = 0; - int16_t width = 0; + int height = 0; + int width = 0; bool hasDecorations = false; enum class Symmetry {NONE, X, Y, XY}; diff --git a/Source/PuzzleSerializer.cpp b/Source/PuzzleSerializer.cpp index c1e93a5..e845578 100644 --- a/Source/PuzzleSerializer.cpp +++ b/Source/PuzzleSerializer.cpp @@ -1,5 +1,6 @@ #include "PuzzleSerializer.h" #include "Memory.h" +#include #pragma warning (disable:26451) #pragma warning (disable:26812) @@ -47,6 +48,16 @@ void PuzzleSerializer::WritePuzzle(const Puzzle& p, int id) { WriteDecorations(p, id); WriteSequence(p, id); +#ifndef NDEBUG + int maxDots = _memory->ReadEntityData(id, NUM_DOTS, 1)[0]; + assert(_intersectionFlags.size() <= maxDots); + assert(_intersectionLocations.size() <= maxDots*2); + + int maxConnections = _memory->ReadEntityData(id, NUM_CONNECTIONS, 1)[0]; + assert(_connectionsA.size() <= maxConnections); + assert(_connectionsB.size() <= maxConnections); +#endif + _memory->WriteEntityData(id, GRID_SIZE_X, {(p.width + 1)/2}); _memory->WriteEntityData(id, GRID_SIZE_Y, {(p.height + 1)/2}); _memory->WriteEntityData(id, NUM_DOTS, {static_cast(_intersectionFlags.size())}); @@ -376,6 +387,10 @@ void PuzzleSerializer::WriteDecorations(const Puzzle& p, int id) { } } +#ifndef NDEBUG + int maxDecorations = _memory->ReadEntityData(id, NUM_DECORATIONS, 1)[0]; + assert(decorations.size() < maxDecorations); +#endif _memory->WriteEntityData(id, NUM_DECORATIONS, {static_cast(decorations.size())}); _memory->WriteArray(id, DECORATIONS, decorations); } diff --git a/Source/Randomizer2.cpp b/Source/Randomizer2.cpp index 7a50c7b..c28795a 100644 --- a/Source/Randomizer2.cpp +++ b/Source/Randomizer2.cpp @@ -14,7 +14,7 @@ Randomizer2::Randomizer2(const std::shared_ptr& memory) : _memory(memory void Randomizer2::Randomize() { RandomizeTutorial(); - RandomizeKeep(); + // RandomizeKeep(); } void Randomizer2::RandomizeTutorial() { @@ -24,7 +24,7 @@ void Randomizer2::RandomizeTutorial() { p.grid[0][8].start = true; p.grid[8][0].end = Cell::Dir::UP; - for (Pos pos : Randomizer2Core::CutEdges(p, 14)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 14, true)) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(p, 0x293); @@ -32,7 +32,7 @@ void Randomizer2::RandomizeTutorial() { { Puzzle p; - p.NewGrid(7, 7); + p.NewGrid(6, 6); switch (Random::RandInt(1, 4)) { case 1: @@ -60,7 +60,7 @@ void Randomizer2::RandomizeTutorial() { break; } - for (Pos pos : Randomizer2Core::CutEdges(p, 35)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 35, true)) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -88,7 +88,7 @@ void Randomizer2::RandomizeKeep() { p.grid[4][8].start = true; p.grid[6][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdges2(p, 5); + std::vector cutEdges = Randomizer2Core::CutEdges(p, 5, false); Puzzle copy = p; std::vector gates = {0x00344, 0x00488, 0x00489, 0x00495, 0x00496}; for (int i=0; i cutEdges = Randomizer2Core::CutEdges2(p, 7); + std::vector cutEdges = Randomizer2Core::CutEdges(p, 7, false); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -133,7 +133,7 @@ void Randomizer2::RandomizeKeep() { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } // Cut to 6 of 9 additional edges - for (Pos pos : Randomizer2Core::CutEdges2(q, 6)) { + for (Pos pos : Randomizer2Core::CutEdges(q, 6, false)) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(q, 0x19DC); @@ -158,7 +158,7 @@ void Randomizer2::RandomizeKeep() { std::vector pebbleMarkers = {0x034a9, 0x034b1, 0x034be, 0x034c4}; - std::vector cutEdges = Randomizer2Core::CutEdges2(p, 7); + std::vector cutEdges = Randomizer2Core::CutEdges(p, 7, false); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } @@ -187,7 +187,7 @@ void Randomizer2::RandomizeKeep() { p.grid[0][8].start = true; p.grid[4][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdges2(p, 2); + std::vector cutEdges = Randomizer2Core::CutEdges(p, 2, false); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -201,7 +201,7 @@ void Randomizer2::RandomizeKeep() { for (Pos pos : cutEdges) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - for (Pos pos : Randomizer2Core::CutEdges2(q, 7)) { + for (Pos pos : Randomizer2Core::CutEdges(q, 7, false)) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(q, 0x1A0F); diff --git a/Source/Randomizer2Core.cpp b/Source/Randomizer2Core.cpp index 8bd5765..0310ae2 100644 --- a/Source/Randomizer2Core.cpp +++ b/Source/Randomizer2Core.cpp @@ -6,31 +6,19 @@ #include #include -// @Cutnpaste -std::vector Randomizer2Core::CutEdges(const Puzzle& p, size_t numEdges) { +std::vector Randomizer2Core::CutEdges(const Puzzle& p, size_t numEdges, bool allowEdges) { std::vector edges; - for (int x=0; x Randomizer2Core::CutEdges2(const Puzzle& p, size_t numEdges) { - std::vector edges; - // Note the iterator bounds; we skip the outer edges. - for (int x=1; x CutEdges(const Puzzle& p, size_t numEdges); - static std::vector CutEdges2(const Puzzle& p, size_t numEdges); + static std::vector CutEdges(const Puzzle& p, size_t numEdges, bool allowEdges); private: static std::vector CutEdgesInternal(const Puzzle& p, std::vector& edges, size_t numEdges); -- cgit 1.4.1