java-topology/whitepaper/outreach/megaglest-0002.md
russell@unturf.com 652608142a feat: close outreach doc gap — 276 docs (batches 11-16)
All projects with patches now have outreach docs. 276 new docs covering
CWE-407, CWE-312, CWE-362 across C, C++, Java, Python, Go, Rust, C#,
PHP, Ruby, JavaScript, Dart, Erlang, R, and more.

Outreach gap: 276 -> 0.
2026-04-15 13:57:42 -04:00

2.6 KiB
Raw Permalink Blame History

MegaGlest — CWE-407 Disclosure Brief (megaglest-0002)

2026-04-13 · Patch available — awaiting upstream merge

Finding

One O(n²) defect in MegaGlest's unit range-finding system. Patched. The defect fires during every range query in combat and area-of-effect resolution.

The Defect

megaglest-0002 (PATCHED — HIGH): source/glest_game/world/unit_updater.cpp:3461

// In UnitUpdater::findUnitsForCell() — fires per cell in range search:
bool found = false;
for (unsigned int i = 0; i < units.size(); ++i) {
    Unit *unitInList = units[i];
    if (unitInList->getId() == cellUnit->getId()) {
        found = true;
        break;
    }
}
if (found == false) {
    units.push_back(cellUnit);
}

units is vector<Unit*>. Dedup uses a linear scan over the entire collected-units list for every cell checked. findUnitsForCell fires for every cell within attack/spell radius. With U units already found and C cells to scan, total cost: O(C × U).

Complexity Proof

At U=200 units in range across C=400 cells:

  • Defective: 400 × (200/2 avg) = 40,000 comparisons
  • Fixed: 400 × O(1) hash insert/check = 400 operations
  • 100× op reduction in dense combat scenarios.

Impact

MegaGlest is an open-source real-time strategy game. Range queries fire during combat resolution, area-of-effect spells, and AI target selection. In late-game battles with hundreds of units in proximity, the quadratic dedup degrades frame rate during the most action-intensive moments.

The Fix

Add std::unordered_set<int> seenIds parameter to findUnitsForCell() for O(1) dedup:

// Before
void UnitUpdater::findUnitsForCell(Cell *cell, vector<Unit *> &units) {
    // O(U) linear scan for each unit found
}

// After
void UnitUpdater::findUnitsForCell(Cell *cell, vector<Unit *> &units, std::unordered_set<int> &seenIds) {
    // O(1) dedup via hash set
    if (seenIds.insert(cellUnit->getId()).second) {
        units.push_back(cellUnit);
    }
}

Patch

Fix available: defects/megaglest-0002/patch/megaglest-0002.patch

Touches unit_updater.h and unit_updater.cpp. Adds std::unordered_set<int> for O(1) membership test. 100× speedup at 200 units in range.

What We Ask

A patch is ready for review.

  1. Confirm receipt and assign a GitHub issue reference.
  2. Assess severity — fires during combat with many units in proximity.
  3. Coordinate a disclosure date — we are targeting 90 days from first contact.
  4. We will credit the MegaGlest team in the public disclosure. Preferred acknowledgment format welcome.

Contact: see cover email. This brief is confidential until coordinated disclosure.