Skip to content

Commit 8a9341a

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Reland Fix possible invalid measurements when width or height is zero pixels
Summary: X-link: facebook/yoga#1823 X-link: facebook/react-native#52348 Fixes facebook/yoga#1819 Yoga has a fast path when measuring a node, if it thinks it knows its dimensions ahead of time. This path has some eroneous logic, to set both axis to owner size, if *either* will evaluate to zero, while having an `YGMeasureModeAtMost`/`FitContent` constraint. This means that if a node is given a zero width, and Yoga later measures with with `FitContent`, its height will become the maximum allowable height, even if it shouldn't be that large. We can fix this, by only allowing if both axis are this fixed case, instead of just one. This bug has existed for about a decade (going back to at least D3312496). Changelog: [General][Fixed] - Fix possible invalid measurements with width or height is zero pixels Reviewed By: yungsters Differential Revision: D76851589 fbshipit-source-id: 6f5a0e6beccc51f591726c9e83e9b90f3350ed0f
1 parent 9756618 commit 8a9341a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/yoga/src/main/cpp/yoga/algorithm/CalculateLayout.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ static void measureNodeWithoutChildren(
415415
Dimension::Height);
416416
}
417417

418+
inline bool isFixedSize(float dim, SizingMode sizingMode) {
419+
return sizingMode == SizingMode::StretchFit ||
420+
(yoga::isDefined(dim) && sizingMode == SizingMode::FitContent &&
421+
dim <= 0.0);
422+
}
423+
418424
static bool measureNodeWithFixedSize(
419425
yoga::Node* const node,
420426
const Direction direction,
@@ -424,12 +430,8 @@ static bool measureNodeWithFixedSize(
424430
const SizingMode heightSizingMode,
425431
const float ownerWidth,
426432
const float ownerHeight) {
427-
if ((yoga::isDefined(availableWidth) &&
428-
widthSizingMode == SizingMode::FitContent && availableWidth <= 0.0f) ||
429-
(yoga::isDefined(availableHeight) &&
430-
heightSizingMode == SizingMode::FitContent && availableHeight <= 0.0f) ||
431-
(widthSizingMode == SizingMode::StretchFit &&
432-
heightSizingMode == SizingMode::StretchFit)) {
433+
if (isFixedSize(availableWidth, widthSizingMode) &&
434+
isFixedSize(availableHeight, heightSizingMode)) {
433435
node->setLayoutMeasuredDimension(
434436
boundAxis(
435437
node,

0 commit comments

Comments
 (0)