java-topology/defects/redot/patch/redot-0004-softbody-node-links-hashset.patch
russell@unturf.com fdbb9a1aa9 redot: 12 CWE-407 defects, patches, outreach brief, UNDF-2026-000001231..1242
All 12 O(N²) algorithmic complexity defects confirmed in Redot Engine 26.2-alpha
(commit 360a8d3). Inherited verbatim from Godot Engine upstream. All patched.

Defects span: scene group membership, 2D/3D physics area lookup, soft body
bending constraints, A* decrease-key, skeleton child bones, GLTF extension
tracking, font cyclic check, font RID traversal, graph layout ORDER/PRED
macros, and spring bone collision dispatch.

Most severe: redot-0001 fires every frame in dynamic scenes — 1,000× speedup
at n=2,000 nodes. redot-0002/0003 fire 60Hz in physics-heavy games — 50×.

Strategy: patch Redot first, Godot follows our lead.
2026-04-03 21:00:29 -04:00

44 lines
1.8 KiB
Diff
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UNDF: UNDF-2026-000001234
# CWE-407: Algorithmic Complexity — O(N²) → O(N) in GodotSoftBody3D::generate_bending_constraints()
#
# Defect: node_links[ia].has(ib) and node_links[ib].has(ia) are O(d) linear scans
# inside a loop over all links (L). For a mesh with L links and avg degree D,
# total ops = O(L × D) = O(N²) for dense meshes.
#
# Fix: add LocalVector<HashSet<int>> node_link_set shadow index.
# has() and insert() are O(1). LocalVector preserved for iteration.
# Total cost after: O(L). 4× speedup at D=4; worse for denser meshes.
#
# Complexity gate (unit/test-redot-0004-softbody-constraints.cpp):
# Scale L=1000 links, D=8: must complete in <1s
--- a/modules/godot_physics_3d/godot_soft_body_3d.cpp
+++ b/modules/godot_physics_3d/godot_soft_body_3d.cpp
@@ -652,16 +652,18 @@ void GodotSoftBody3D::generate_bending_constraints(int p_n_iterations) {
- LocalVector<LocalVector<int>> node_links;
+ // FIX redot-0004: was LocalVector<int>.has() — O(n) per link, O(n²) total, CWE-407
+ // Each node_links[i].has(j) scans the growing adjacency list linearly.
+ // Fix: shadow HashSet per node for O(1) membership; LocalVector preserved for iteration.
+ LocalVector<LocalVector<int>> node_links;
+ LocalVector<HashSet<int>> node_link_set;
// Build node links.
node_links.resize(nodes.size());
+ node_link_set.resize(nodes.size());
for (Link &link : links) {
const int ia = (int)(link.n[0] - &nodes[0]);
const int ib = (int)(link.n[1] - &nodes[0]);
- if (!node_links[ia].has(ib)) {
+ if (!node_link_set[ia].has(ib)) {
node_links[ia].push_back(ib);
+ node_link_set[ia].insert(ib);
}
- if (!node_links[ib].has(ia)) {
+ if (!node_link_set[ib].has(ia)) {
node_links[ib].push_back(ia);
+ node_link_set[ib].insert(ia);
}
}