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
|
--- a/rts/Rendering/Units/UnitDrawer.cpp
+++ b/rts/Rendering/Units/UnitDrawer.cpp
@@ -1625,6 +1625,7 @@ bool CUnitDrawerGLSL::ShowUnitBuildSquare(const BuildInfo& buildInfo, const std:
struct BuildCache {
uint64_t key;
+ int unitDefID;
int createFrame;
bool canBuild;
std::vector<uint8_t> statuses;
@@ -1636,6 +1637,14 @@ bool CUnitDrawerGLSL::ShowUnitBuildSquare(const BuildInfo& buildInfo, const std:
uint64_t hashKey = spring::LiteHash(pos);
hashKey = spring::hash_combine(spring::LiteHash(buildInfo.buildFacing), hashKey);
+ // TestUnitBuildSquare statuses also depend on the definition and queued
+ // commands. Both the world and minimap passes share this cache.
+ hashKey = spring::hash_combine(spring::LiteHash(buildInfo.def->id), hashKey);
+ for (const Command& command: commands) {
+ hashKey = spring::hash_combine(spring::LiteHash(command.GetID()), hashKey);
+ for (unsigned int i = 0; i < command.GetNumParams(); ++i)
+ hashKey = spring::hash_combine(spring::LiteHash(command.GetParam(i)), hashKey);
+ }
static constexpr int CACHE_VALIDITY_PERIOD = GAME_SPEED / 5;
std::erase_if(buildCache, [](const BuildCache& bc) {
@@ -1650,8 +1659,14 @@ bool CUnitDrawerGLSL::ShowUnitBuildSquare(const BuildInfo& buildInfo, const std:
bool canBuild;
- const auto it = std::find_if(buildCache.begin(), buildCache.end(), [hashKey](const BuildCache& bc) {
- return bc.key == hashKey;
+ // A hash collision may reuse incorrect statuses. Verify the UnitDef separately
+ // to prevent an OOB read from a mismatched footprint; collisions within the
+ // same UnitDef may still render incorrect status data.
+ const auto it = std::find_if(buildCache.begin(), buildCache.end(), [hashKey, &buildInfo](const BuildCache& bc) {
+ return (
+ bc.key == hashKey &&
+ bc.unitDefID == buildInfo.def->id
+ );
});
if (it != buildCache.end()) {
statuses = it->statuses;
@@ -1670,6 +1685,7 @@ bool CUnitDrawerGLSL::ShowUnitBuildSquare(const BuildInfo& buildInfo, const std:
auto& buildCacheItem = buildCache.back();
buildCacheItem.key = hashKey;
+ buildCacheItem.unitDefID = buildInfo.def->id;
buildCacheItem.canBuild = canBuild;
buildCacheItem.createFrame = gs->frameNum;
buildCacheItem.statuses = statuses;
|