flutter-sdk icon indicating copy to clipboard operation
flutter-sdk copied to clipboard

[ANR] com.truecaller.android.sdk.ShareProfileHelper.hasValidAccountState

Open bhaveshptl opened this issue 2 years ago • 1 comments

Observing ANR with the following call stack in firebase.

com.truecaller.android.sdk.ShareProfileHelper.hasValidAccountState (ShareProfileHelper.java:96) com.truecaller.android.sdk.ShareProfileHelper.isValidTcClientAvailable (ShareProfileHelper.java:88) com.truecaller.android.sdk.ClientManager.<init> (ClientManager.java:87) com.truecaller.android.sdk.ClientManager.createInstance (ClientManager.java:67) com.truecaller.android.sdk.TruecallerSDK.init (TruecallerSDK.java:91) com.truecallersdk.TruecallerSdkPlugin.onMethodCall (TruecallerSdkPlugin.java:117) io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage (MethodChannel.java:262) io.flutter.embedding.engine.dart.DartMessenger.invokeHandler (DartMessenger.java:178) io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0 (DartMessenger.java:206)

bhaveshptl avatar May 17 '23 12:05 bhaveshptl

Hi @bhaveshptl @shubhral,

I was checking the implementation of the ShareProfileHelper.java and found that the following method hasValidAccountState will have the cursor open in very rare scenarios when cursor.moveToFirst() returns false or null (not sure of the implementation of this method). Hence I think we should have a safe check to close the cursor in the end even if the condition are not met.

Present Implementation of hasValidAccountState in ShareProfileHelper.java

private static boolean hasValidAccountState(@NonNull Context context, @NonNull String packageName) {
    try {
        Cursor cursor = context.getContentResolver().query(Uri.parse("content://" + packageName + ".TcInfoContentProvider/tcAccountState"), null, null, null, null);
        if (cursor == null) {
            cursor = context.getContentResolver().query(Uri.parse("content://" + packageName + ".TcAccountStateProvider/tcAccountState"), null, null, null, null);
        }
        if (cursor != null && cursor.moveToFirst()) {
            boolean hasValidAccountState = cursor.getInt(0) == 1;
            cursor.close();
            return hasValidAccountState;
        }
        return true;
    } catch (Exception e) {
        return true;
    }
}

Proposed Implementation of hasValidAccountState in ShareProfileHelper.java

private static boolean hasValidAccountState(@NonNull Context context, @NonNull String packageName) {
    try {
        Cursor cursor = context.getContentResolver().query(Uri.parse("content://" + packageName + ".TcInfoContentProvider/tcAccountState"), null, null, null, null);
        if (cursor == null) {
            cursor = context.getContentResolver().query(Uri.parse("content://" + packageName + ".TcAccountStateProvider/tcAccountState"), null, null, null, null);
        }
        if (cursor != null && cursor.moveToFirst()) {
            boolean hasValidAccountState = cursor.getInt(0) == 1;
            cursor.close();
            return hasValidAccountState;
        }
        if (cursor != null) {
            cursor.close();
        }
        return true;
    } catch (Exception e) {
        return true;
    }
}

dev-aman avatar Dec 26 '24 05:12 dev-aman