foxygit / fake-08-xbox360 Log in
commit 3e047849fa060b94826ba7b8d9938618cf932e03
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Fri Apr 8 19:15:26 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Fri Apr 8 19:15:26 2022 -0600

    Fix regression in mget bounds checking

    Always return 0 for out of bounds values
---
 source/graphics.cpp    | 32 ++++++++++++++++++--------------
 source/graphics.h      |  4 ++--
 test/graphicstests.cpp |  5 +++++
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index e1f990c..d955b51 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -50,7 +50,7 @@ uint8_t* Graphics::GetScreenPaletteMap(){
 //start helper methods
 //based on tac08 implementation of blitter()
 void Graphics::copySpriteToScreen(
-	uint8_t spritebuffer[],
+	uint8_t* spritebuffer,
 	int scr_x,
 	int scr_y,
 	int spr_x,
@@ -194,7 +194,7 @@ void Graphics::copySpriteToScreen(
 //based on tac08 implementation of stretch_blitter()
 //uses ints so we can shift bits and do integer division instead of floating point
 void Graphics::copyStretchSpriteToScreen(
-	uint8_t spritebuffer[],
+	uint8_t* spritebuffer,
 	int spr_x,
 	int spr_y,
 	int spr_w,
@@ -1406,19 +1406,21 @@ std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> Graphics::clip(int x, int y, int

 uint8_t Graphics::mget(int celx, int cely){
 	const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
+	const int bigMapLocation = _memory->hwState.mapMemMapping << 8;
+	const int mapSize = bigMap
+		? 0x10000 - bigMapLocation
+		: 8192;
+
 	const int mapW = _memory->hwState.widthOfTheMap == 0 ? 256 : _memory->hwState.widthOfTheMap;
+	const int mapH = mapSize / mapW;
+
 	const int idx = cely * mapW + celx;

-	if (idx < 0) {
+	if (celx < 0 || celx > mapW || cely < 0 || cely > mapH) {
         return 0;
 	}

 	if (bigMap){
-		const int mapLocation = _memory->hwState.mapMemMapping << 8;
-		const int mapSize = 0x10000 - mapLocation;
-		if (idx >= mapSize){
-			return 0;
-		}
 		const int offset = 0x8000 - mapSize;
 		return _memory->userData[offset + idx];
 	}
@@ -1436,19 +1438,21 @@ uint8_t Graphics::mget(int celx, int cely){

 void Graphics::mset(int celx, int cely, uint8_t snum){
 	const bool bigMap = _memory->hwState.mapMemMapping >= 0x80;
+	const int bigMapLocation = _memory->hwState.mapMemMapping << 8;
+	const int mapSize = bigMap
+		? 0x10000 - bigMapLocation
+		: 8192;
+
 	const int mapW = _memory->hwState.widthOfTheMap == 0 ? 256 : _memory->hwState.widthOfTheMap;
+	const int mapH = mapSize / mapW;
+
 	const int idx = cely * mapW + celx;

-	if (idx < 0) {
+	if (celx < 0 || celx > mapW || cely < 0 || cely > mapH) {
         return;
 	}

 	if (bigMap){
-		const int mapLocation = _memory->hwState.mapMemMapping << 8;
-		const int mapSize = 0x10000 - mapLocation;
-		if (idx >= mapSize){
-			return;
-		}
 		const int offset = 0x8000 - mapSize;
 		_memory->userData[offset + idx] = snum;
 	}
diff --git a/source/graphics.h b/source/graphics.h
index 757030a..173ef60 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -14,7 +14,7 @@ class Graphics {
 	PicoRam* _memory;

 	void copySpriteToScreen(
-		uint8_t spritebuffer[],
+		uint8_t* spritebuffer,
 		int scr_x,
 		int scr_y,
 		int spr_x,
@@ -25,7 +25,7 @@ class Graphics {
 		bool flip_y);

 	void copyStretchSpriteToScreen(
-		uint8_t spritebuffer[],
+		uint8_t* spritebuffer,
 		int spr_x,
 		int spr_y,
 		int spr_w,
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index f6f719b..2a96d61 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1480,6 +1480,11 @@ TEST_CASE("graphics class behaves as expected") {
         CHECK_EQ(71, result);
     }
     SUBCASE("mget out of bounds returns 0"){
+        for(int y = 0; y < 64; y++){
+            for(int x = 0; x < 128; x++){
+                graphics->mset(x,y,1);
+            }
+        }
         auto result1 = graphics->mget(-1, 16);
         auto result2 = graphics->mget(129, 16);
         auto result3 = graphics->mget(10, -1);