File: 0032-Fix-out-of-bounds-access-in-dtMergeCorridorStartMove.patch

package info (click to toggle)
recastnavigation 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,892 kB
  • sloc: cpp: 50,116; ansic: 2,674; xml: 182; makefile: 16
file content (220 lines) | stat: -rw-r--r-- 7,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
From 599fd0f023181c0a484df2a18cf1d75a3553852e Mon Sep 17 00:00:00 2001
From: Roman Siromakha <elsid.mail@gmail.com>
Date: Sun, 28 Jan 2024 19:26:50 +0100
Subject: [PATCH 32/36] Fix out of bounds access in dtMergeCorridorStartMoved
 and add tests (#635)

size can become negative if req > maxPath. This may happen when visited buffer is larger than path buffer.

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.

List tests files explicitly. When new file is added CMake does not add it to the already generated list if GLOB is used.
---
 DetourCrowd/Source/DetourPathCorridor.cpp     |  10 +-
 RecastDemo/premake5.lua                       |   3 +-
 Tests/CMakeLists.txt                          |  15 ++-
 .../DetourCrowd/Tests_DetourPathCorridor.cpp  | 119 ++++++++++++++++++
 4 files changed, 136 insertions(+), 11 deletions(-)
 create mode 100644 Tests/DetourCrowd/Tests_DetourPathCorridor.cpp

diff --git a/DetourCrowd/Source/DetourPathCorridor.cpp b/DetourCrowd/Source/DetourPathCorridor.cpp
index bddea5a..3835a38 100644
--- a/DetourCrowd/Source/DetourPathCorridor.cpp
+++ b/DetourCrowd/Source/DetourPathCorridor.cpp
@@ -59,13 +59,13 @@ int dtMergeCorridorStartMoved(dtPolyRef* path, const int npath, const int maxPat
 	int size = dtMax(0, npath-orig);
 	if (req+size > maxPath)
 		size = maxPath-req;
-	if (size)
+	if (size > 0)
 		memmove(path+req, path+orig, size*sizeof(dtPolyRef));
-	
+
 	// Store visited
-	for (int i = 0; i < req; ++i)
-		path[i] = visited[(nvisited-1)-i];				
-	
+	for (int i = 0, n = dtMin(req, maxPath); i < n; ++i)
+		path[i] = visited[(nvisited-1)-i];
+
 	return req+size;
 }
 
diff --git a/RecastDemo/premake5.lua b/RecastDemo/premake5.lua
index 7ede4d6..826859e 100644
--- a/RecastDemo/premake5.lua
+++ b/RecastDemo/premake5.lua
@@ -223,14 +223,15 @@ project "Tests"
 		"../Tests/Recast/*.cpp",
 		"../Tests/Detour/*.h",
 		"../Tests/Detour/*.cpp",
+		"../Tests/DetourCrowd/*.cpp",
 		"../Tests/Contrib/catch2/*.cpp"
 	}
 
 	-- project dependencies
 	links { 
 		"DebugUtils",
-		"Detour",
 		"DetourCrowd",
+		"Detour",
 		"DetourTileCache",
 		"Recast",
 	}
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 9a87479..c37a6c9 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1,14 +1,19 @@
-file(GLOB TESTS_SOURCES Detour/*.cpp Recast/*.cpp)
-
 include_directories(../Detour/Include)
 include_directories(../Recast/Include)
 
-add_executable(Tests ${TESTS_SOURCES})
+add_executable(Tests
+	Detour/Tests_Detour.cpp
+	Recast/Bench_rcVector.cpp
+	Recast/Tests_Alloc.cpp
+	Recast/Tests_Recast.cpp
+	Recast/Tests_RecastFilter.cpp
+	DetourCrowd/Tests_DetourPathCorridor.cpp
+)
 
 set_property(TARGET Tests PROPERTY CXX_STANDARD 17)
 
-add_dependencies(Tests Recast Detour)
-target_link_libraries(Tests Recast Detour)
+add_dependencies(Tests Recast Detour DetourCrowd)
+target_link_libraries(Tests Recast Detour DetourCrowd)
 
 find_package(Catch2 QUIET)
 if (Catch2_FOUND)
diff --git a/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp b/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp
new file mode 100644
index 0000000..1c199cb
--- /dev/null
+++ b/Tests/DetourCrowd/Tests_DetourPathCorridor.cpp
@@ -0,0 +1,119 @@
+#include "catch2/catch_all.hpp"
+
+#include "DetourPathCorridor.h"
+
+TEST_CASE("dtMergeCorridorStartMoved")
+{
+    SECTION("Should handle empty input")
+    {
+        dtPolyRef* const path = nullptr;
+        const int npath = 0;
+        const int maxPath = 0;
+        const dtPolyRef* const visited = nullptr;
+        const int nvisited = 0;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 0);
+    }
+
+    SECTION("Should handle empty visited")
+    {
+        dtPolyRef path[] = {1};
+        const int npath = 1;
+        const int maxPath = 1;
+        const dtPolyRef* const visited = nullptr;
+        const int nvisited = 0;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 1);
+        const dtPolyRef expectedPath[] = {1};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should handle empty path")
+    {
+        dtPolyRef* const path = nullptr;
+        const int npath = 0;
+        const int maxPath = 0;
+        const dtPolyRef visited[] = {1};
+        const int nvisited = 1;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 0);
+    }
+
+    SECTION("Should strip visited points from path except last")
+    {
+        dtPolyRef path[] = {1, 2};
+        const int npath = 2;
+        const int maxPath = 2;
+        const dtPolyRef visited[] = {1, 2};
+        const int nvisited = 2;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 1);
+        const dtPolyRef expectedPath[] = {2, 2};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should add visited points not present in path in reverse order")
+    {
+        dtPolyRef path[] = {1, 2, 0};
+        const int npath = 2;
+        const int maxPath = 3;
+        const dtPolyRef visited[] = {1, 2, 3, 4};
+        const int nvisited = 4;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 3);
+        const dtPolyRef expectedPath[] = {4, 3, 2};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should add visited points not present in path up to the path capacity")
+    {
+        dtPolyRef path[] = {1, 2, 0};
+        const int npath = 2;
+        const int maxPath = 3;
+        const dtPolyRef visited[] = {1, 2, 3, 4, 5};
+        const int nvisited = 5;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 3);
+        const dtPolyRef expectedPath[] = {5, 4, 3};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should not change path if there is no intersection with visited")
+    {
+        dtPolyRef path[] = {1, 2};
+        const int npath = 2;
+        const int maxPath = 2;
+        const dtPolyRef visited[] = {3, 4};
+        const int nvisited = 2;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 2);
+        const dtPolyRef expectedPath[] = {1, 2};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should save unvisited path points")
+    {
+        dtPolyRef path[] = {1, 2, 0};
+        const int npath = 2;
+        const int maxPath = 3;
+        const dtPolyRef visited[] = {1, 3};
+        const int nvisited = 2;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 3);
+        const dtPolyRef expectedPath[] = {3, 1, 2};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+
+    SECTION("Should save unvisited path points up to the path capacity")
+    {
+        dtPolyRef path[] = {1, 2};
+        const int npath = 2;
+        const int maxPath = 2;
+        const dtPolyRef visited[] = {1, 3};
+        const int nvisited = 2;
+        const int result = dtMergeCorridorStartMoved(path, npath, maxPath, visited, nvisited);
+        CHECK(result == 2);
+        const dtPolyRef expectedPath[] = {3, 1};
+        CHECK_THAT(path, Catch::Matchers::RangeEquals(expectedPath));
+    }
+}
-- 
2.43.0