foxygit / fake-08-xbox360 Log in
commit bb0bf5da39def7a440d1ce7dfda353f940b32003
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Mar 19 08:15:24 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Fri Mar 19 08:15:24 2021 -0600

    Fix mget and mset for out of bounds numbers
---
 source/graphics.cpp    |  6 ++++++
 test/graphicstests.cpp | 11 +++++++++++
 2 files changed, 17 insertions(+)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 17dcc2d..992b97d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1132,6 +1132,9 @@ void Graphics::clip(int x, int y, int w, int h) {

 //map methods heavily based on tac08 implementation
 uint8_t Graphics::mget(int celx, int cely){
+	if (celx < 0 || celx >= 128 || cely < 0 || cely >= 64)
+        return 0;
+
 	if (cely < 32) {
 		return _memory->mapData[cely * 128 + celx];
 	}
@@ -1143,6 +1146,9 @@ uint8_t Graphics::mget(int celx, int cely){
 }

 void Graphics::mset(int celx, int cely, uint8_t snum){
+	if (celx < 0 || celx >= 128 || cely < 0 || cely >= 64)
+        return;
+
 	if (cely < 32) {
 		_memory->mapData[cely * 128 + celx] = snum;
 	}
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index da0615a..ae464aa 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1519,6 +1519,17 @@ TEST_CASE("graphics class behaves as expected") {

         CHECK_EQ(71, result);
     }
+    SUBCASE("mget out of bounds returns 0"){
+        auto result1 = graphics->mget(-1, 16);
+        auto result2 = graphics->mget(129, 16);
+        auto result3 = graphics->mget(10, -1);
+        auto result4 = graphics->mget(10, 67);
+
+        CHECK_EQ(0, result1);
+        CHECK_EQ(0, result2);
+        CHECK_EQ(0, result3);
+        CHECK_EQ(0, result4);
+    }
     SUBCASE("mset from upper half of map data (non-shared)"){
         int x = 13;
         int y = 7;