#14179 Fix crash building cell search tree for LGR-heavy grids

The leaf-node cap that selects between the optimized and the
non-optimized cell search tree construction used cellCount(), which
only counts main grid cells and excludes LGR cells. The non-optimized
buildCellSearchTree() iterates totalCellCount() (main grid plus all LGR
cells) and creates one leaf per cell, so a grid with few main cells but
many LGR cells could be routed into that path and exhaust memory,
crashing while building the AABB tree.

Base the threshold on totalCellCount() so LGR-inflated grids are routed
to the optimized aggregating path and the leaf count stays bounded.
This commit is contained in:
Magne Sjaastad authored and Kristian Bendiksen committed 2026-06-10 08:47:24 +02:00
1 parent 221305b741
commit 0410635815
1 file changed
+6 -2
@@ -858,8 +858,12 @@ std::vector<size_t> RigMainGrid::findIntersectingCells( const cvf::BoundingBox&
void RigMainGrid::doBuildCellSearchTree( std::string* aabbTreeInfo ) const
{
const double maxNumberOfLeafNodes = 4000000;
const double factor = std::ceil( cellCount() / maxNumberOfLeafNodes );
const size_t cellsPerBoundingBox = std::max( size_t( 1 ), static_cast<size_t>( factor ) );
// Use the total cell count (including LGR cells) when deciding the number of cells per bounding box.
// The non-optimized buildCellSearchTree() creates one leaf per cell across totalCellCount(), so basing
// the threshold on the main grid cell count only can route LGR-heavy grids into that path and exhaust
// memory while building the search tree.
const double factor = std::ceil( totalCellCount() / maxNumberOfLeafNodes );
const size_t cellsPerBoundingBox = std::max( size_t( 1 ), static_cast<size_t>( factor ) );
if ( cellsPerBoundingBox > 1 )
{