Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/YGFlexShrinkBorderBugTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// Regression test for https://github.com/facebook/yoga/issues/1665
// flexBasis:0 + flexShrink:1 + borderWidth + minWidth produces an astronomically
// large width (~1.65e11) instead of being clamped to minWidth.

#include <gtest/gtest.h>
#include <yoga/Yoga.h>

TEST(YGFlexShrinkBorderBug, flex_basis_0_border_minwidth_row) {
YGConfigRef config = YGConfigNew();
YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 393.0f);
YGNodeStyleSetHeight(root, 100.0f);

// Four row children: flexBasis:0, flexGrow:1, flexShrink:1, minWidth:160
// First child has borderWidth:0.594443, rest 0.594442.
// Bug: first child (and all others) get width ~1.65e11 instead of 160.
float borders[] = {0.594443f, 0.594442f, 0.594442f, 0.594442f};
for (int i = 0; i < 4; i++) {
YGNodeRef child = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexBasis(child, 0.0f);
YGNodeStyleSetFlexGrow(child, 1.0f);
YGNodeStyleSetFlexShrink(child, 1.0f);
YGNodeStyleSetMinWidth(child, 160.0f);
YGNodeStyleSetBorder(child, YGEdgeAll, borders[i]);
YGNodeInsertChild(root, child, i);
}

YGNodeCalculateLayout(root, 393.0f, 100.0f, YGDirectionLTR);

for (int i = 0; i < 4; i++) {
YGNodeRef child = YGNodeGetChild(root, i);
EXPECT_GE(YGNodeLayoutGetWidth(child), 160.0f)
<< "child[" << i << "] width below minWidth";
EXPECT_LE(YGNodeLayoutGetWidth(child), 200.0f)
<< "child[" << i << "] width is astronomically large (bug)";
}

YGNodeFreeRecursive(root);
YGConfigFree(config);
}
9 changes: 8 additions & 1 deletion yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,15 @@ static float distributeFreeSpaceSecondPass(
if (flexShrinkScaledFactor != 0) {
float childSize = YGUndefined;

// 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 produces a gigantic
// childSize that overwhelms the min/max clamp.
const float shrinkFactorMagnitude =
std::abs(flexLine.layout.totalFlexShrinkScaledFactors);
if (yoga::isDefined(flexLine.layout.totalFlexShrinkScaledFactors) &&
flexLine.layout.totalFlexShrinkScaledFactors == 0) {
shrinkFactorMagnitude < 1e-6f) {
childSize = childFlexBasis + flexShrinkScaledFactor;
} else {
childSize = childFlexBasis +
Expand Down
Loading