summarylogtreecommitdiffstats
path: root/guard-invalid-ray-length.patch
blob: ab775ba3f5cb3d39b20e016432eab4d0e38b7475 (plain)
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
--- a/rts/Sim/Misc/QuadField.cpp
+++ b/rts/Sim/Misc/QuadField.cpp
@@ -1,6 +1,7 @@
 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
 
 #include <algorithm>
+#include <cmath>
 
 #include "QuadField.h"
 #include "Map/ReadMap.h"
@@ -9,6 +10,7 @@
 #include "Sim/Misc/GlobalConstants.h"
 #include "Sim/Misc/TeamHandler.h"
 #include "System/ContainerUtil.h"
+#include "System/Log/ILog.h"
 #include "System/Threading/ThreadPool.h"
 
 #ifndef UNIT_TEST
@@ -201,6 +203,16 @@
 	dir.AssertNaNs();
 	start.AssertNaNs();
 
+	// callers may pass invalid lengths (see https://github.com/beyond-all-reason/RecoilEngine/issues/3018),
+	// which violates the preconditions of std::clamp(t0, 0.0f, length) below and aborts on
+	// hardened builds (_GLIBCXX_ASSERTIONS). Clamp to 0 instead, which routes execution
+	// into the "special case" below and turns this into a zero-length ray query.
+	if (!std::isfinite(length) || length < 0.0f) {
+		LOG_L(L_ERROR, "[CQuadField::%s] invalid ray length %f (start=(%g,%g,%g) dir=(%g,%g,%g)), clamping to 0"
+			, __func__, length, start.x, start.y, start.z, dir.x, dir.y, dir.z);
+		length = 0.0f;
+	}
+
 	auto& queryQuads = *(qfq.quads = tempQuads[qfq.threadOwner].ReserveVector());
 
 	const float3 to = start + (dir * length);
@@ -289,6 +301,13 @@
 	dir.AssertNaNs();
 	start.AssertNaNs();
 
+	// same rationale as GetQuadsOnRay above (see https://github.com/beyond-all-reason/RecoilEngine/issues/3018)
+	if (!std::isfinite(length) || length < 0.0f) {
+		LOG_L(L_ERROR, "[CQuadField::%s] invalid ray length %f (start=(%g,%g,%g) dir=(%g,%g,%g)), clamping to 0"
+			, __func__, length, start.x, start.y, start.z, dir.x, dir.y, dir.z);
+		length = 0.0f;
+	}
+
 	const float3 baseTo = start + (dir * length);
 
 	const bool noZdir = (math::floor(start.z * invQuadSize.y) == math::floor(baseTo.z * invQuadSize.y));