From 68580fcab6890b9ae7dc3df10173133b281d70af Mon Sep 17 00:00:00 2001 From: Denis Koroskin Date: Thu, 10 Mar 2016 17:18:49 -0800 Subject: [PATCH] Fix RCTText crashing when negative width is passed to measure Summary: In some rare cases, RCTText.measure can receive negative width which will crash with an assertion in android.text.Layout because it expects a positive value. This is a temporary fix to treat negative values as unconstrained width until the original bug is fixed. Reviewed By: sriramramani Differential Revision: D3038767 --- .../src/main/java/com/facebook/react/flat/RCTText.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/RCTText.java b/ReactAndroid/src/main/java/com/facebook/react/flat/RCTText.java index b216dda30..43996a71a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/RCTText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/RCTText.java @@ -78,6 +78,10 @@ import com.facebook.react.uimanager.annotations.ReactProp; mText = text; + // technically, width should never be negative, but there is currently a bug in + // LayoutEngine where a negative value can be passed. + boolean unconstrainedWidth = Float.isNaN(width) || width < 0; + BoringLayout.Metrics metrics = BoringLayout.isBoring(text, PAINT, sBoringLayoutMetrics); if (metrics != null) { sBoringLayoutMetrics = mBoringLayoutMetrics; @@ -93,7 +97,7 @@ import com.facebook.react.uimanager.annotations.ReactProp; mBoringLayoutMetrics = metrics; float measuredWidth = (float) metrics.width; - if (Float.isNaN(width) || measuredWidth <= width) { + if (unconstrainedWidth || measuredWidth <= width) { measureOutput.width = measuredWidth; measureOutput.height = getMetricsHeight(metrics, INCLUDE_PADDING); @@ -106,7 +110,7 @@ import com.facebook.react.uimanager.annotations.ReactProp; // width < measuredWidth -> more that a single line -> not boring } - int maximumWidth = Float.isNaN(width) ? Integer.MAX_VALUE : (int) width; + int maximumWidth = unconstrainedWidth ? Integer.MAX_VALUE : (int) width; // Make sure we update the paint's text size. If we don't do this, ellipsis might be measured // incorrecly (but drawn correctly, which almost feels like an Android bug, because width of the