Skip to content

Commit 103f8b3

Browse files
joevilchesfacebook-github-bot
authored andcommittedFeb 15, 2025
Fix bug where we can crash with small blurRadius
Summary: An internal crash report notified us that we were getting an `IllegalArgumentException` in Android's `BlurMaskFilter` used in Box Shadows. The only requirement for the arguments there are that the blur radius is positive. We ensure that, but pass to `sigmaToRadius` afterwards, which does NOT ensure that. This updates that conversion so that * We will never return negative numbers, regardless of what `PixelUtil.toDIPFromPixel` is doing * Small enough numbers ( < 0.5) will just go through as 0.0, which is what Android does in https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/libs/hwui/utils/Blur.cpp;l=34 * Ensure shadows check that we have a POSITIVE blur radius AFTER conversion. sigmaToRadius can still return 0 which with also throw, so gotta make sure we change our checks. Filter blurs and dropShadow should be ok, renderEffect does not mention anything about being > 0 in the docs. Changelog: [Android] [Fixed] - Fix issue where boxShadow crashes with small blur radius Facebook Post alerting us of this: https://fb.workplace.com/groups/rn.panelapps/permalink/1136446121310146/ Reviewed By: NickGerleman Differential Revision: D69683031 fbshipit-source-id: 92f0938fea65e1af280bdb12b6c3d2b9014e89c0
1 parent cf81abc commit 103f8b3

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed
 

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FilterHelper.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,9 @@ internal object FilterHelper {
394394
internal fun sigmaToRadius(sigma: Float): Float {
395395
// Android takes blur amount as a radius while web takes a sigma. This value
396396
// is used under the hood to convert between them on Android
397-
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/libs/hwui/jni/RenderEffect.cpp
397+
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/libs/hwui/utils/Blur.cpp;l=34
398398
val sigmaToRadiusRatio = 0.57_735f
399-
return (PixelUtil.toPixelFromDIP(sigma) - 0.5f) / sigmaToRadiusRatio
399+
val pxSigma = PixelUtil.toPixelFromDIP(sigma)
400+
return if (pxSigma > 0.5f) (pxSigma - 0.5f) / sigmaToRadiusRatio else 0f
400401
}
401402
}

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/InsetBoxShadowDrawable.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ internal class InsetBoxShadowDrawable(
5555
private val shadowPaint =
5656
Paint().apply {
5757
color = shadowColor
58-
if (blurRadius > 0) {
59-
maskFilter =
60-
BlurMaskFilter(
61-
FilterHelper.sigmaToRadius(blurRadius * BLUR_RADIUS_SIGMA_SCALE),
62-
BlurMaskFilter.Blur.NORMAL)
58+
val convertedBlurRadius = FilterHelper.sigmaToRadius(blurRadius * BLUR_RADIUS_SIGMA_SCALE)
59+
if (convertedBlurRadius > 0) {
60+
maskFilter = BlurMaskFilter(convertedBlurRadius, BlurMaskFilter.Blur.NORMAL)
6361
}
6462
}
6563

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/OutsetBoxShadowDrawable.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ internal class OutsetBoxShadowDrawable(
5151
private val shadowPaint =
5252
Paint().apply {
5353
color = shadowColor
54-
if (blurRadius > 0) {
55-
maskFilter =
56-
BlurMaskFilter(
57-
FilterHelper.sigmaToRadius(blurRadius * BLUR_RADIUS_SIGMA_SCALE),
58-
BlurMaskFilter.Blur.NORMAL)
54+
val convertedBlurRadius = FilterHelper.sigmaToRadius(blurRadius * BLUR_RADIUS_SIGMA_SCALE)
55+
if (convertedBlurRadius > 0) {
56+
maskFilter = BlurMaskFilter(convertedBlurRadius, BlurMaskFilter.Blur.NORMAL)
5957
}
6058
}
6159

0 commit comments

Comments
 (0)