Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bhaveshptl opened this issue May 17, 2023 · 1 comment

Comments

@bhaveshptl
Copy link

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)

@dev-aman
Copy link

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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@dev-aman @bhaveshptl and others