From 8f5aaf13b2f67f84d8e189b175d9a830a5ab446c Mon Sep 17 00:00:00 2001 From: Gijs Weterings Date: Thu, 3 Apr 2025 08:01:59 -0700 Subject: [PATCH] Fix Nullsafe FIXMEs for FileReaderModule.java and mark nullsafe (#50352) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50352 Gone trough all the FIXMEs added in the previous diff by the nullsafe tool, marked the class as nullsafe and ensured no remaining violations. Changelog: [Android][Fixed] Made FileReaderModule.java nullsafe Reviewed By: cortinico Differential Revision: D71979585 fbshipit-source-id: 3bef5ff48d1d27838d2668367785a85b2b863f05 --- .../react/modules/blob/FileReaderModule.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/FileReaderModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/FileReaderModule.java index 97e8fc469b2..9f89c35f9b9 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/FileReaderModule.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/blob/FileReaderModule.java @@ -8,12 +8,15 @@ package com.facebook.react.modules.blob; import android.util.Base64; +import androidx.annotation.Nullable; import com.facebook.fbreact.specs.NativeFileReaderModuleSpec; +import com.facebook.infer.annotation.Nullsafe; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.module.annotations.ReactModule; +@Nullsafe(Nullsafe.Mode.LOCAL) @ReactModule(name = NativeFileReaderModuleSpec.NAME) public class FileReaderModule extends NativeFileReaderModuleSpec { @@ -23,15 +26,13 @@ public class FileReaderModule extends NativeFileReaderModuleSpec { super(reactContext); } - private BlobModule getBlobModule(String reason) { + private @Nullable BlobModule getBlobModule(String reason) { ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn(); if (reactApplicationContext != null) { - // NULLSAFE_FIXME[Return Not Nullable] return reactApplicationContext.getNativeModule(BlobModule.class); } - // NULLSAFE_FIXME[Return Not Nullable] return null; } @@ -48,9 +49,13 @@ public class FileReaderModule extends NativeFileReaderModuleSpec { return; } - byte[] bytes = - // NULLSAFE_FIXME[Parameter Not Nullable] - blobModule.resolve(blob.getString("blobId"), blob.getInt("offset"), blob.getInt("size")); + String blobId = blob.getString("blobId"); + if (blobId == null) { + promise.reject(ERROR_INVALID_BLOB, "The specified blob does not contain a blobId"); + return; + } + + byte[] bytes = blobModule.resolve(blobId, blob.getInt("offset"), blob.getInt("size")); if (bytes == null) { promise.reject(ERROR_INVALID_BLOB, "The specified blob is invalid"); @@ -77,9 +82,13 @@ public class FileReaderModule extends NativeFileReaderModuleSpec { return; } - byte[] bytes = - // NULLSAFE_FIXME[Parameter Not Nullable] - blobModule.resolve(blob.getString("blobId"), blob.getInt("offset"), blob.getInt("size")); + String blobId = blob.getString("blobId"); + if (blobId == null) { + promise.reject(ERROR_INVALID_BLOB, "The specified blob does not contain a blobId"); + return; + } + + byte[] bytes = blobModule.resolve(blobId, blob.getInt("offset"), blob.getInt("size")); if (bytes == null) { promise.reject(ERROR_INVALID_BLOB, "The specified blob is invalid"); @@ -90,8 +99,9 @@ public class FileReaderModule extends NativeFileReaderModuleSpec { StringBuilder sb = new StringBuilder(); sb.append("data:"); - // NULLSAFE_FIXME[Nullable Dereference] - if (blob.hasKey("type") && !blob.getString("type").isEmpty()) { + if (blob.hasKey("type") + && blob.getString("type") != null + && !blob.getString("type").isEmpty()) { sb.append(blob.getString("type")); } else { sb.append("application/octet-stream");