From 24a3be5f99be002fc5b19b05647cb241a7435df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nishan=20=28o=5E=E2=96=BD=5Eo=29?= Date: Tue, 20 Oct 2026 08:42:12 -0700 Subject: [PATCH] Fix flex shrink explosion when the shrink total is a positive float residual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes https://github.com/react/yoga/issues/2038 Fixes https://github.com/react/yoga/issues/1651 Closes https://github.com/react/yoga/issues/2039 Closes https://github.com/react/yoga/issues/1855 Thanks to sebholl for the analysis and the reproduction in https://github.com/react/yoga/issues/2038. ## Issue When a flex line overflows, the first pass freezes the items that hit their minimum size and removes them from the shrink total. The second pass then divides the remaining free space by that total. The total was computed by subtracting the frozen items flex shrink factors from the float total. When every item freezes, the total should become 0, but float subtraction leaves a small residual. ### Example All items have `flexShrink: 1`: ``` row (width: 350, flexDirection: row) ├── A width: 100.1 minWidth: 90 ├── B width: 200.3 minWidth: 190 └── C width: 300.7 minWidth: 290 ``` The items need 601.1 but the row is only 350, so they must shrink by 251.1. Each item would go below its `minWidth`, so the first pass freezes all three (at 90 / 190 / 290) and removes them from the shrink total. With no items left, the total should be 0. But in float, `-601.1 + 100.1 + 200.3 + 300.7 = 0.000061`. That is above the `1e-6` guard added in https://github.com/react/yoga/issues/1974 (for https://github.com/react/yoga/issues/1665), so the second pass divides the remaining free space by it: `-220 / 0.000061 ≈ -3.6 million`. Instead of staying at their min, the items grow to about **361M / 722M / 1084M** (expected **90 / 190 / 290**). ### Solution With non-negative `flexShrink`, every scaled shrink factor is ≤ 0, so a real total is never positive. A positive total can only be this float residual, and dividing by it is what makes the items explode. The second pass now treats a positive total like the near-zero case of the `1e-6` guard from https://github.com/react/yoga/issues/1974 and uses the existing fallback. Negative residuals (which the min clamp already handles) and all other layouts are unchanged. Fixing the fallback for `flexShrink < 1` (https://github.com/react/yoga/issues/931) is left for a separate PR. #### Why not `inexactEquals` (|total| < 0.0001)? The leftover grows with the size of the total. For example, 60 chips of about 100pt each in a 1000pt row give a total of about 6000, and the leftover is then often larger than 0.0001. In a test with rows like this, about half of them still exploded with `inexactEquals`. With the sign check, none did, because a real shrink total is never positive. X-link: https://github.com/react/yoga/pull/2044 Reviewed By: javache Differential Revision: D126038629 Pulled By: cortinico fbshipit-source-id: dc1ec3370783e227ce4477bcd632ae5907034193 --- .../ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 15277dfd464..7b1d7461d74 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1138,12 +1138,16 @@ static float distributeFreeSpaceSecondPass( // Use a relative epsilon guard instead of exact equality: after the // first pass removes all constrained items, // totalFlexShrinkScaledFactors may be near-zero rather than exactly 0 - // due to floating-point cancellation. Dividing by a near-zero value + // due to floating-point cancellation. Dividing by a near-zero value // produces a gigantic childSize that overwhelms the min/max clamp. + // A positive total is always such a residual: with non-negative + // flexShrink a real total is never positive + // (https://github.com/react/yoga/issues/2038). const float shrinkFactorMagnitude = std::abs(flexLine.layout.totalFlexShrinkScaledFactors); if (yoga::isDefined(flexLine.layout.totalFlexShrinkScaledFactors) && - shrinkFactorMagnitude < 1e-6f) { + (shrinkFactorMagnitude < 1e-6f || + flexLine.layout.totalFlexShrinkScaledFactors > 0)) { childSize = childFlexBasis + flexShrinkScaledFactor; } else { childSize = childFlexBasis +