Skip to content

Commit

Permalink
Sanitize measure function results (#44557)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44557

We've started seeing assertion failures in Yoga where a `NaN` value makes its way to an `availableHeight` constraint when measuring Litho tree.

Because it's only happening on Litho, I have some suspicion this might be originating from a Litho-specific measure function. This adds sanitization in Yoga to measure function results, where we will log an error, and set size to zero, if either dimension ends up being negative of `NaN`.

This doesn't really help track down where the error was happening, but Yoga doesn't have great context to show this to begin with. If we see this is issue, next steps would be Litho internal intrumentation to find culprit.

Changelog: [Internal]

Reviewed By: joevilches

Differential Revision: D57285584

fbshipit-source-id: 935fcdd28c05bbac0d73e1c7654ae11a74898537
  • Loading branch information
NickGerleman authored and facebook-github-bot committed May 14, 2024
1 parent 5a100ae commit 44f9371
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 22 additions & 4 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>

#include <yoga/debug/AssertFatal.h>
#include <yoga/debug/Log.h>
#include <yoga/node/Node.h>
#include <yoga/numeric/Comparison.h>

Expand Down Expand Up @@ -49,12 +50,29 @@ Node::Node(Node&& node) noexcept
}

YGSize Node::measure(
float width,
float availableWidth,
MeasureMode widthMode,
float height,
float availableHeight,
MeasureMode heightMode) {
return measureFunc_(
this, width, unscopedEnum(widthMode), height, unscopedEnum(heightMode));
const auto size = measureFunc_(
this,
availableWidth,
unscopedEnum(widthMode),
availableHeight,
unscopedEnum(heightMode));

if (yoga::isUndefined(size.height) || size.height < 0 ||
yoga::isUndefined(size.width) || size.width < 0) {
yoga::log(
this,
LogLevel::Error,
"Measure function returned an invalid dimension to Yoga: [width=%f, height=%f]",
size.width,
size.height);
return {.width = 0.0f, .height = 0.0f};
}

return size;
}

float Node::baseline(float width, float height) const {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class YG_EXPORT Node : public ::YGNode {
}

YGSize measure(
float width,
float availableWidth,
MeasureMode widthMode,
float height,
float availableHeight,
MeasureMode heightMode);

bool hasBaselineFunc() const noexcept {
Expand Down

0 comments on commit 44f9371

Please sign in to comment.