From fc28eefaed2c71a824677b613f87dc4ceaea243b Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 2 Feb 2017 19:22:45 +0300 Subject: Fix nullpointer access in special layer draw function. --- src/resources/map/maplayer.cpp | 4 +-- src/resources/map/maplayer_unittest.cc | 6 ++++ src/resources/map/speciallayer_unittest.cc | 45 ++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) (limited to 'src/resources/map') diff --git a/src/resources/map/maplayer.cpp b/src/resources/map/maplayer.cpp index 95e835302..deaeab9de 100644 --- a/src/resources/map/maplayer.cpp +++ b/src/resources/map/maplayer.cpp @@ -391,7 +391,7 @@ void MapLayer::drawSpecialLayer(Graphics *const graphics, const MapItem *item0 = mSpecialLayer->mTiles[ptr + startX]; if (!item0 || item0->mType == MapItemType::EMPTY) { - x0 += mSpecialLayer->mCache[ptr + startX]; + x0 += mSpecialLayer->mCache[ptr + startX] + 1; } for (int x = x0; x < endX1; x++) { @@ -414,7 +414,7 @@ void MapLayer::drawSpecialLayer(Graphics *const graphics, item0 = mTempLayer->mTiles[ptr + startX]; if (!item0 || item0->mType == MapItemType::EMPTY) { - x0 += mTempLayer->mCache[ptr + startX]; + x0 += mTempLayer->mCache[ptr + startX] + 1; } for (int x = x0; x < endX1; x++) { diff --git a/src/resources/map/maplayer_unittest.cc b/src/resources/map/maplayer_unittest.cc index 103d019cd..bd58d998c 100644 --- a/src/resources/map/maplayer_unittest.cc +++ b/src/resources/map/maplayer_unittest.cc @@ -2117,8 +2117,14 @@ TEST_CASE("MapLayer drawSpecialLayer (tempLayer)") layer->setTempLayer(map->getTempLayer()); TileInfo *const tiles = layer->getTiles(); specialLayer = map->getTempLayer(); + const int *const cache = specialLayer->getCache(); + REQUIRE(cache[0] == 10000); + REQUIRE(cache[1] == 10000); + specialLayer->setTile(1, 0, MapItemType::ARROW_UP); specialLayer->updateCache(); + REQUIRE(cache[0] == 0); + REQUIRE(cache[1] == 10000); layer->drawSpecialLayer(mock, 0, diff --git a/src/resources/map/speciallayer_unittest.cc b/src/resources/map/speciallayer_unittest.cc index 3e1b42043..b74ade7dc 100644 --- a/src/resources/map/speciallayer_unittest.cc +++ b/src/resources/map/speciallayer_unittest.cc @@ -62,8 +62,9 @@ TEST_CASE("SpecialLayer updateCache") { layer = new SpecialLayer("test", 1, 1); - layer->setTile(0, 0, MapItemType::ARROW_UP); const int *const cache = layer->getCache(); + REQUIRE(cache[0] == 10000); + layer->setTile(0, 0, MapItemType::ARROW_UP); layer->updateCache(); REQUIRE(cache[0] == 10000); } @@ -72,8 +73,10 @@ TEST_CASE("SpecialLayer updateCache") { layer = new SpecialLayer("test", 2, 1); - layer->setTile(0, 0, MapItemType::ARROW_UP); const int *const cache = layer->getCache(); + REQUIRE(cache[0] == 10000); + REQUIRE(cache[1] == 10000); + layer->setTile(0, 0, MapItemType::ARROW_UP); layer->updateCache(); REQUIRE(cache[0] == 10000); REQUIRE(cache[1] == 10000); @@ -143,6 +146,44 @@ TEST_CASE("SpecialLayer updateCache") REQUIRE(cache[2] == 10000); } + SECTION("simple 7") + { + layer = new SpecialLayer("test", + 3, 1); + const int *const cache = layer->getCache(); + layer->updateCache(); + REQUIRE(cache[0] == 10000); + REQUIRE(cache[1] == 10000); + REQUIRE(cache[2] == 10000); + } + + SECTION("simple 8") + { + layer = new SpecialLayer("test", + 3, 1); + layer->setTile(0, 0, MapItemType::EMPTY); + layer->setTile(1, 0, MapItemType::EMPTY); + layer->setTile(2, 0, MapItemType::EMPTY); + const int *const cache = layer->getCache(); + layer->updateCache(); + REQUIRE(cache[0] == 10000); + REQUIRE(cache[1] == 10000); + REQUIRE(cache[2] == 10000); + } + + SECTION("simple 9") + { + layer = new SpecialLayer("test", + 2, 1); + const int *const cache = layer->getCache(); + REQUIRE(cache[0] == 10000); + REQUIRE(cache[1] == 10000); + layer->setTile(1, 0, MapItemType::ARROW_UP); + layer->updateCache(); + REQUIRE(cache[0] == 0); + REQUIRE(cache[1] == 10000); + } + SECTION("normal 1") { layer = new SpecialLayer("test", -- cgit v1.2.3-70-g09d2