Skip to content

Commit 01c092c

Browse files
committed
add bounds check
1 parent ee2f05f commit 01c092c

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

AmeisenNavigation.Server/src/Main.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,19 @@ void OnClientConnect(ClientHandler* handler)
133133
{
134134
LogI("Client Connected: ", handler->GetIpAddress(), ":", handler->GetPort());
135135

136-
ClientPathBuffers[handler->GetUniqueId()] = std::make_pair(new float[Config->maxPointPath * 3], new float[Config->maxPointPath * 3]);
137-
Nav->NewClient(handler->GetUniqueId());
136+
ClientPathBuffers[handler->GetId()] = std::make_pair(new float[Config->maxPointPath * 3], new float[Config->maxPointPath * 3]);
137+
Nav->NewClient(handler->GetId());
138138
}
139139

140140
void OnClientDisconnect(ClientHandler* handler)
141141
{
142-
Nav->FreeClient(handler->GetUniqueId());
142+
Nav->FreeClient(handler->GetId());
143143

144-
delete[] ClientPathBuffers[handler->GetUniqueId()].first;
145-
ClientPathBuffers[handler->GetUniqueId()].first = nullptr;
144+
delete[] ClientPathBuffers[handler->GetId()].first;
145+
ClientPathBuffers[handler->GetId()].first = nullptr;
146146

147-
delete[] ClientPathBuffers[handler->GetUniqueId()].second;
148-
ClientPathBuffers[handler->GetUniqueId()].second = nullptr;
147+
delete[] ClientPathBuffers[handler->GetId()].second;
148+
ClientPathBuffers[handler->GetId()].second = nullptr;
149149

150150
LogI("Client Disconnected: ", handler->GetIpAddress(), ":", handler->GetPort());
151151
}
@@ -155,22 +155,22 @@ void PathCallback(ClientHandler* handler, char type, const void* data, int size)
155155
const PathRequestData request = *reinterpret_cast<const PathRequestData*>(data);
156156

157157
int pathSize = 0;
158-
float* pathBuffer = ClientPathBuffers[handler->GetUniqueId()].first;
158+
float* pathBuffer = ClientPathBuffers[handler->GetId()].first;
159159

160-
if (Nav->GetPath(handler->GetUniqueId(), request.mapId, request.start, request.end, pathBuffer, &pathSize))
160+
if (Nav->GetPath(handler->GetId(), request.mapId, request.start, request.end, pathBuffer, &pathSize))
161161
{
162162
if ((request.flags & static_cast<int>(PathRequestFlags::CATMULLROM)) && pathSize > 9)
163163
{
164164
int smoothedPathSize = 0;
165-
float* smoothedPathBuffer = ClientPathBuffers[handler->GetUniqueId()].second;
165+
float* smoothedPathBuffer = ClientPathBuffers[handler->GetId()].second;
166166
Nav->SmoothPathCatmullRom(pathBuffer, pathSize, smoothedPathBuffer, &smoothedPathSize, Config->catmullRomSplinePoints, Config->catmullRomSplineAlpha);
167167

168168
handler->SendData(type, smoothedPathBuffer, smoothedPathSize * sizeof(float));
169169
}
170170
else if ((request.flags & static_cast<int>(PathRequestFlags::CHAIKIN)) && pathSize > 6)
171171
{
172172
int smoothedPathSize = 0;
173-
float* smoothedPathBuffer = ClientPathBuffers[handler->GetUniqueId()].second;
173+
float* smoothedPathBuffer = ClientPathBuffers[handler->GetId()].second;
174174
Nav->SmoothPathChaikinCurve(pathBuffer, pathSize, smoothedPathBuffer, &smoothedPathSize);
175175

176176
handler->SendData(type, smoothedPathBuffer, smoothedPathSize * sizeof(float));
@@ -192,7 +192,7 @@ void RandomPointCallback(ClientHandler* handler, char type, const void* data, in
192192
const int mapId = *reinterpret_cast<const int*>(data);
193193
float point[3]{};
194194

195-
Nav->GetRandomPoint(handler->GetUniqueId(), mapId, point);
195+
Nav->GetRandomPoint(handler->GetId(), mapId, point);
196196
handler->SendData(type, point, VEC3_SIZE);
197197
}
198198

@@ -201,7 +201,7 @@ void RandomPointAroundCallback(ClientHandler* handler, char type, const void* da
201201
const RandomPointAroundData request = *reinterpret_cast<const RandomPointAroundData*>(data);
202202
float point[3]{};
203203

204-
Nav->GetRandomPointAround(handler->GetUniqueId(), request.mapId, request.start, request.radius, point);
204+
Nav->GetRandomPointAround(handler->GetId(), request.mapId, request.start, request.radius, point);
205205
handler->SendData(type, point, VEC3_SIZE);
206206
}
207207

@@ -210,7 +210,7 @@ void MoveAlongSurfaceCallback(ClientHandler* handler, char type, const void* dat
210210
const MoveRequestData request = *reinterpret_cast<const MoveRequestData*>(data);
211211
float point[3]{};
212212

213-
Nav->MoveAlongSurface(handler->GetUniqueId(), request.mapId, request.start, request.end, point);
213+
Nav->MoveAlongSurface(handler->GetId(), request.mapId, request.start, request.end, point);
214214
handler->SendData(type, point, VEC3_SIZE);
215215
}
216216

@@ -219,7 +219,7 @@ void CastRayCallback(ClientHandler* handler, char type, const void* data, int si
219219
const CastRayData request = *reinterpret_cast<const CastRayData*>(data);
220220
dtRaycastHit hit;
221221

222-
if (Nav->CastMovementRay(handler->GetUniqueId(), request.mapId, request.start, request.end, &hit))
222+
if (Nav->CastMovementRay(handler->GetId(), request.mapId, request.start, request.end, &hit))
223223
{
224224
handler->SendData(type, request.end, VEC3_SIZE);
225225
}

AmeisenNavigation/src/AmeisenNavigation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,14 @@ void AmeisenNavigation::SmoothPathChaikinCurve(const float* input, int inputSize
236236

237237
float result[3];
238238

239+
// make sure we dont go over the buffer bounds
240+
// why 10: we add 6 floats in the loop and 3 for the end position
241+
const int maxIndex = MaxPointPath - 10;
242+
239243
for (int i = 0; i < inputSize - 3; i += 3)
240244
{
245+
if (*outputSize > maxIndex) { break; }
246+
241247
ScaleAndAddVector3(input + i, 0.75f, input + i + 3, 0.25f, s0, s1, result);
242248
InsertVector3(output, *outputSize, result, 0);
243249

@@ -265,6 +271,10 @@ void AmeisenNavigation::SmoothPathCatmullRom(const float* input, int inputSize,
265271

266272
float C[3];
267273

274+
// make sure we dont go over the buffer bounds
275+
// why 4: we add 3 floats in the loop
276+
const int maxIndex = MaxPointPath - 4;
277+
268278
for (int i = 3; i < inputSize - 6; i += 3)
269279
{
270280
const float* p0 = input + i - 3;
@@ -291,6 +301,8 @@ void AmeisenNavigation::SmoothPathCatmullRom(const float* input, int inputSize,
291301

292302
if (!std::isnan(C[0]) && !std::isnan(C[1]) && !std::isnan(C[2]))
293303
{
304+
if (*outputSize > maxIndex) { return; }
305+
294306
InsertVector3(output, *outputSize, C, 0);
295307
}
296308
}

dep/AnTCP.Server/include/AnTcpServer.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
// toggle debug output here
4-
#if 1
4+
#if 0
55
#define DEBUG_ONLY(x) x
66
#define BENCHMARK(x) x
77
#else
@@ -72,7 +72,6 @@ class ClientHandler
7272
/// <param name="callbacks">Pointer to the server callback map.</param>
7373
ClientHandler
7474
(
75-
size_t id,
7675
SOCKET socket,
7776
const SOCKADDR_IN& socketInfo,
7877
std::atomic<bool>& shouldExit,
@@ -81,10 +80,9 @@ class ClientHandler
8180
std::function<void(ClientHandler*)>* onClientDisconnected = nullptr
8281
)
8382
: IsActive(true),
84-
Id(id),
83+
Id(static_cast<unsigned int>(socketInfo.sin_addr.S_un.S_addr + socketInfo.sin_port)),
8584
Socket(socket),
8685
SocketInfo(socketInfo),
87-
UniqueId(static_cast<unsigned int>(socketInfo.sin_addr.S_un.S_addr + socketInfo.sin_port)),
8886
ShouldExit(shouldExit),
8987
Callbacks(callbacks),
9088
Thread(new std::thread(&ClientHandler::Listen, this)),
@@ -110,11 +108,6 @@ class ClientHandler
110108
/// </summary>
111109
constexpr int GetId() noexcept { return Id; }
112110

113-
/// <summary>
114-
/// Get a unique id based on ip and port of the handler.
115-
/// </summary>
116-
constexpr unsigned int GetUniqueId() noexcept { return UniqueId; }
117-
118111
/// <summary>
119112
/// Used to delete old disconnected clients.
120113
/// </summary>
-300 KB
Binary file not shown.
-231 KB
Binary file not shown.
-86.5 KB
Binary file not shown.
-85.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)