Skip to content

Commit 2ff07e1

Browse files
committed
fix(metrics): revise divide-by-zero branch in aggregator
- Ensures average cell length defaults to zero
1 parent 91dd3a4 commit 2ff07e1

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

packages/virtualized-lists/Lists/ListMetricsAggregator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,16 @@ export default class ListMetricsAggregator {
103103
this._measuredCellsCount += 1;
104104
}
105105

106+
// NOTE: _measuredCellsCount is incremented before this line whenever a new
107+
// cell is added, so it should never be 0 here. This guard is defense in
108+
// depth against a future bug where _measuredCellsCount is reset (e.g. during
109+
// orientation changes) without also clearing _cellMetrics — which would leave
110+
// `curr` with a stale value and count === 0, causing a divide-by-zero.
106111
if (this._measuredCellsCount > 0) {
107112
this._averageCellLength =
108113
this._measuredCellsLength / this._measuredCellsCount;
114+
} else {
115+
this._averageCellLength = 0;
109116
}
110117
this._cellMetrics.set(cellKey, next);
111118
this._highestMeasuredCellIndex = Math.max(

0 commit comments

Comments
 (0)