From 43524a9127f78ec950f79cd4a07d6552ee5c18d4 Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 02:08:10 -0200 Subject: [PATCH 1/6] Fixes heap corruption in Map::findPath --- src/client/map.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/client/map.cpp b/src/client/map.cpp index 701769be..d04a5fb0 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -666,12 +666,6 @@ std::tuple, Otc::PathFindResult> Map::findPath(const bool evaluated; }; - struct LessNode : std::binary_function { - bool operator()(Node* a, Node* b) const { - return b->totalCost < a->totalCost; - } - }; - std::tuple, Otc::PathFindResult> ret; std::vector& dirs = std::get<0>(ret); Otc::PathFindResult& result = std::get<1>(ret); @@ -703,7 +697,8 @@ std::tuple, Otc::PathFindResult> Map::findPath(const } std::unordered_map nodes; - std::priority_queue, LessNode> searchList; + //std::priority_queue, LessNode> searchList; + std::deque searchList; Node *currentNode = new Node(startPos); currentNode->pos = startPos; @@ -798,15 +793,18 @@ std::tuple, Otc::PathFindResult> Map::findPath(const neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos); neighborNode->dir = walkDir; neighborNode->evaluated = false; - searchList.push(neighborNode); + searchList.push_back(neighborNode); } } + std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; }); + std::unique(searchList.begin(), searchList.end()); + currentNode->evaluated = true; currentNode = nullptr; while(searchList.size() > 0 && !currentNode) { - Node *node = searchList.top(); - searchList.pop(); + Node *node = searchList.front(); + searchList.pop_front(); if(!node->evaluated) currentNode = node; From ff617c3fabbf6cf385d4e505fcbc204c28ecf57e Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 03:36:19 -0200 Subject: [PATCH 2/6] More fixes in Map::findPath Fixed the styling, std::unique and removed the priority_queue comment --- src/client/map.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client/map.cpp b/src/client/map.cpp index d04a5fb0..b410344b 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -697,8 +697,7 @@ std::tuple, Otc::PathFindResult> Map::findPath(const } std::unordered_map nodes; - //std::priority_queue, LessNode> searchList; - std::deque searchList; + std::deque searchList; Node *currentNode = new Node(startPos); currentNode->pos = startPos; @@ -797,12 +796,13 @@ std::tuple, Otc::PathFindResult> Map::findPath(const } } - std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; }); - std::unique(searchList.begin(), searchList.end()); + std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; }); + auto end = std::unique(searchList.begin(), searchList.end()); + searchList.resize(std::distance(searchList.begin(), end)); currentNode->evaluated = true; currentNode = nullptr; - while(searchList.size() > 0 && !currentNode) { + while(!searchList.empty() && !currentNode) { Node *node = searchList.front(); searchList.pop_front(); From f4f79f47bd769a91e6e25dc4603b57095a1a4521 Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 03:57:17 -0200 Subject: [PATCH 3/6] Optimizations in Map::findPath --- src/client/map.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client/map.cpp b/src/client/map.cpp index b410344b..41c86874 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -798,13 +798,11 @@ std::tuple, Otc::PathFindResult> Map::findPath(const std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; }); auto end = std::unique(searchList.begin(), searchList.end()); - searchList.resize(std::distance(searchList.begin(), end)); currentNode->evaluated = true; currentNode = nullptr; - while(!searchList.empty() && !currentNode) { - Node *node = searchList.front(); - searchList.pop_front(); + for (auto begin = searchList.begin(); begin != end && !currentNode; ++begin) { + Node *node = *begin; if(!node->evaluated) currentNode = node; From cc1e3c534e7e8f4275cfa7514c066987015a4c30 Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 04:34:04 -0200 Subject: [PATCH 4/6] Even more fixes Forgot to clean the garbage generated in the last commit. --- src/client/map.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/map.cpp b/src/client/map.cpp index 41c86874..4b9c9eeb 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -807,6 +807,7 @@ std::tuple, Otc::PathFindResult> Map::findPath(const if(!node->evaluated) currentNode = node; } + searchList.clear(); } if(foundNode) { From cb1f28a3a3a0198ecbdb644af4edd9ce47115118 Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 04:34:54 -0200 Subject: [PATCH 5/6] Meh, spacing. --- src/client/map.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/map.cpp b/src/client/map.cpp index 4b9c9eeb..10551b07 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -806,8 +806,8 @@ std::tuple, Otc::PathFindResult> Map::findPath(const if(!node->evaluated) currentNode = node; - } - searchList.clear(); + + searchList.clear(); } if(foundNode) { From f0fbd4790fcff58deb8d9e2c3431e532b8845a84 Mon Sep 17 00:00:00 2001 From: sakagushi Date: Thu, 6 Feb 2014 04:36:39 -0200 Subject: [PATCH 6/6] And do not delete curly brackets. --- src/client/map.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/map.cpp b/src/client/map.cpp index 10551b07..8bc3c6cc 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -806,6 +806,7 @@ std::tuple, Otc::PathFindResult> Map::findPath(const if(!node->evaluated) currentNode = node; + } searchList.clear(); }