| 1 |
From 599fd0f023181c0a484df2a18cf1d75a3553852e Mon Sep 17 00:00:00 2001 |
| 2 |
From: Roman Siromakha <[email protected]> |
| 3 |
Date: Sun, 28 Jan 2024 19:26:50 +0100 |
| 4 |
Subject: [PATCH 32/36] Fix out of bounds access in dtMergeCorridorStartMoved |
| 5 |
and add tests (#635) |
| 6 |
|
| 7 |
size can become negative if req > maxPath. This may happen when visited buffer is larger than path buffer. |
| 8 |
|
| 9 |
Add tests to cover different use cases of the function including Should add visited points not present in path up to the path capacity to cover the fix. |
| 10 |
|
| 11 |
List tests files explicitly. When new file is added CMake does not add it to the already generated list if GLOB is used. |
| 12 |
--- |
| 13 |
DetourCrowd/Source/DetourPathCorridor.cpp | 10 +- |
| 14 |
RecastDemo/premake5.lua | 3 +- |
| 15 |
Tests/CMakeLists.txt | 15 ++- |
| 16 |
.../DetourCrowd/Tests_DetourPathCorridor.cpp | 119 ++++++++++++++++++ |
| 17 |
4 files changed, 136 insertions(+), 11 deletions(-) |
| 18 |
create mode 100644 Tests/DetourCrowd/Tests_DetourPathCorridor.cpp |
| 19 |
|
| 20 |
diff --git a/DetourCrowd/Source/DetourPathCorridor.cpp b/DetourCrowd/Source/DetourPathCorridor.cpp |
| 21 |
index bddea5a..3835a38 100644 |
| 22 |
--- a/DetourCrowd/Source/DetourPathCorridor.cpp |
| 23 |
+++ b/DetourCrowd/Source/DetourPathCorridor.cpp |
| 24 |
@@ -59,13 +59,13 @@ int dtMergeCorridorStartMoved(dtPolyRef* path, const int npath, const int maxPat |
| 25 |
int size = dtMax(0, npath-orig); |
| 26 |
if (req+size > maxPath) |
| 27 |
size = maxPath-req; |
| 28 |
- if (size) |
| 29 |
+ if (size > 0) |
| 30 |
memmove(path+req, path+orig, size*sizeof(dtPolyRef)); |
| 31 |
- |
| 32 |
+ |
| 33 |
// Store visited |
| 34 |
- for (int i = 0; i < req; ++i) |
| 35 |
- path[i] = visited[(nvisited-1)-i]; |
| 36 |
- |
| 37 |
+ for (int i = 0, n = dtMin(req, maxPath); i < n; ++i) |
| 38 |
+ path[i] = visited[(nvisited-1)-i]; |
| 39 |
+ |
| 40 |
return req+size; |
| 41 |
} |
| 42 |
|
| 43 |
diff --git a/RecastDemo/premake5.lua b/RecastDemo/premake5.lua |
| 44 |
index 7ede4d6..826859e 100644 |
| 45 |
--- a/RecastDemo/premake5.lua |
| 46 |
+++ b/RecastDemo/premake5.lua |
| 47 |
@@ -223,14 +223,15 @@ project "Tests" |
| 48 |
"../Tests/Recast/*.cpp", |
| 49 |
"../Tests/Detour/*.h", |
| 50 |
"../Tests/Detour/*.cpp", |
| 51 |
+ "../Tests/DetourCrowd/*.cpp", |
| 52 |
"../Tests/Contrib/catch2/*.cpp" |
| 53 |
} |
| 54 |
|
| 55 |
-- project dependencies |
| 56 |
links { |
| 57 |
"DebugUtils", |
| 58 |
- "Detour", |
| 59 |
"DetourCrowd", |
| 60 |
+ "Detour", |
| 61 |
"DetourTileCache", |
| 62 |
"Recast", |
| 63 |
} |
| 64 |
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt |
| 65 |
index 9a87479..c37a6c9 100644 |
| 66 |
--- a/Tests/CMakeLists.txt |
| 67 |
+++ b/Tests/CMakeLists.txt |
| 68 |
@@ -1,14 +1,19 @@ |
| 69 |
-file(GLOB TESTS_SOURCES Detour/*.cpp Recast/*.cpp) |
| 70 |
- |
| 71 |
include_directories(../Detour/Include) |
| 72 |
include_directories(../Recast/Include) |
| 73 |
|
| 74 |
-add_executable(Tests ${TESTS_SOURCES}) |
| 75 |
+add_executable(Tests |
| 76 |
+ Detour/Tests_Detour.cpp |
| 77 |
+ Recast/Bench_rcVector.cpp |
| 78 |
+ Recast/Tests_Alloc.cpp |
| 79 |
+ Recast/Tests_Recast.cpp |
| 80 |
+ Recast/Tests_RecastFilter.cpp |
| 81 |
+ DetourCrowd/Tests_DetourPathCorridor.cpp |
| 82 |
+) |
| 83 |
|
| 84 |
set_property(TARGET Tests PROPERTY CXX_STANDARD 17) |
| 85 |
|
| 86 |
-add_dependencies(Tests Recast Detour) |
| 87 |
-target_link_libraries(Tests Recast Detour) |
| 88 |
+add_dependencies(Tests Recast Detour DetourCrowd) |
| 89 |
+target_link_libraries(Tests Recast Detour DetourCrowd) |
| 90 |
|
| 91 |
find_package(Catch2 QUIET) |
| 92 |
if (Catch2_FOUND) |
| 93 |
diff --git a/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp b/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp |
| 94 |
new file mode 100644 |
| 95 |
index 0000000..1c199cb |
| 96 |
--- /dev/null |
| 97 |
+++ b/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp |
| 98 |
@@ -0,0 +1,119 @@ |
| 99 |
+#include "catch2/catch_all.hpp" |
| 100 |
+ |
| 101 |
+#include "DetourPathCorridor.h" |
| 102 |
+ |
| 103 |
+TEST_CASE("dtMergeCorridorStartMoved") |
| 104 |
+{ |
| 105 |
+ SECTION("Should handle empty input") |
| 106 |
+ { |
| 107 |
+ dtPolyRef* const path = nullptr; |
| 108 |
+ const int npath = 0; |
| 109 |
+ const int maxPath = 0; |
| 110 |
+ const dtPolyRef* const visited = nullptr; |
| 111 |
+ const int nvisited = 0; |
| 112 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 113 |
+ CHECK(result == 0); |
| 114 |
+ } |
| 115 |
+ |
| 116 |
+ SECTION("Should handle empty visited") |
| 117 |
+ { |
| 118 |
+ dtPolyRef path[] = {1}; |
| 119 |
+ const int npath = 1; |
| 120 |
+ const int maxPath = 1; |
| 121 |
+ const dtPolyRef* const visited = nullptr; |
| 122 |
+ const int nvisited = 0; |
| 123 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 124 |
+ CHECK(result == 1); |
| 125 |
+ const dtPolyRef expectedPath[] = {1}; |
| 126 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 127 |
+ } |
| 128 |
+ |
| 129 |
+ SECTION("Should handle empty path") |
| 130 |
+ { |
| 131 |
+ dtPolyRef* const path = nullptr; |
| 132 |
+ const int npath = 0; |
| 133 |
+ const int maxPath = 0; |
| 134 |
+ const dtPolyRef visited[] = {1}; |
| 135 |
+ const int nvisited = 1; |
| 136 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 137 |
+ CHECK(result == 0); |
| 138 |
+ } |
| 139 |
+ |
| 140 |
+ SECTION("Should strip visited points from path except last") |
| 141 |
+ { |
| 142 |
+ dtPolyRef path[] = {1, 2}; |
| 143 |
+ const int npath = 2; |
| 144 |
+ const int maxPath = 2; |
| 145 |
+ const dtPolyRef visited[] = {1, 2}; |
| 146 |
+ const int nvisited = 2; |
| 147 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 148 |
+ CHECK(result == 1); |
| 149 |
+ const dtPolyRef expectedPath[] = {2, 2}; |
| 150 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 151 |
+ } |
| 152 |
+ |
| 153 |
+ SECTION("Should add visited points not present in path in reverse order") |
| 154 |
+ { |
| 155 |
+ dtPolyRef path[] = {1, 2, 0}; |
| 156 |
+ const int npath = 2; |
| 157 |
+ const int maxPath = 3; |
| 158 |
+ const dtPolyRef visited[] = {1, 2, 3, 4}; |
| 159 |
+ const int nvisited = 4; |
| 160 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 161 |
+ CHECK(result == 3); |
| 162 |
+ const dtPolyRef expectedPath[] = {4, 3, 2}; |
| 163 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 164 |
+ } |
| 165 |
+ |
| 166 |
+ SECTION("Should add visited points not present in path up to the path capacity") |
| 167 |
+ { |
| 168 |
+ dtPolyRef path[] = {1, 2, 0}; |
| 169 |
+ const int npath = 2; |
| 170 |
+ const int maxPath = 3; |
| 171 |
+ const dtPolyRef visited[] = {1, 2, 3, 4, 5}; |
| 172 |
+ const int nvisited = 5; |
| 173 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 174 |
+ CHECK(result == 3); |
| 175 |
+ const dtPolyRef expectedPath[] = {5, 4, 3}; |
| 176 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 177 |
+ } |
| 178 |
+ |
| 179 |
+ SECTION("Should not change path if there is no intersection with visited") |
| 180 |
+ { |
| 181 |
+ dtPolyRef path[] = {1, 2}; |
| 182 |
+ const int npath = 2; |
| 183 |
+ const int maxPath = 2; |
| 184 |
+ const dtPolyRef visited[] = {3, 4}; |
| 185 |
+ const int nvisited = 2; |
| 186 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 187 |
+ CHECK(result == 2); |
| 188 |
+ const dtPolyRef expectedPath[] = {1, 2}; |
| 189 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 190 |
+ } |
| 191 |
+ |
| 192 |
+ SECTION("Should save unvisited path points") |
| 193 |
+ { |
| 194 |
+ dtPolyRef path[] = {1, 2, 0}; |
| 195 |
+ const int npath = 2; |
| 196 |
+ const int maxPath = 3; |
| 197 |
+ const dtPolyRef visited[] = {1, 3}; |
| 198 |
+ const int nvisited = 2; |
| 199 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 200 |
+ CHECK(result == 3); |
| 201 |
+ const dtPolyRef expectedPath[] = {3, 1, 2}; |
| 202 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 203 |
+ } |
| 204 |
+ |
| 205 |
+ SECTION("Should save unvisited path points up to the path capacity") |
| 206 |
+ { |
| 207 |
+ dtPolyRef path[] = {1, 2}; |
| 208 |
+ const int npath = 2; |
| 209 |
+ const int maxPath = 2; |
| 210 |
+ const dtPolyRef visited[] = {1, 3}; |
| 211 |
+ const int nvisited = 2; |
| 212 |
+ const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited); |
| 213 |
+ CHECK(result == 2); |
| 214 |
+ const dtPolyRef expectedPath[] = {3, 1}; |
| 215 |
+ CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath)); |
| 216 |
+ } |
| 217 |
+} |
| 218 |
-- |
| 219 |
2.43.0 |
| 220 |
|