Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

NPE check for Password and Account Name field #384

Open
wants to merge 11 commits into
base: MAS-2.1.00
Choose a base branch
from

Conversation

arindamhit
Copy link
Contributor

We are adding a check for Null Pointer Exception for the Password and Account Name field.

@arindamhit arindamhit changed the base branch from develop to MAS-2.1.00 June 17, 2021 10:51
@arindamhit arindamhit changed the base branch from MAS-2.1.00 to develop June 17, 2021 10:52
@arindamhit arindamhit self-assigned this Jun 17, 2021
@arindamhit arindamhit changed the base branch from develop to MAS-2.1.00 June 17, 2021 11:00
@zakirjt zakirjt requested a review from graju256 June 17, 2021 14:03
Comment on lines 64 to 67
new NullPointerException("Password is null");
}
} else {
new NullPointerException("Account Name does not exist");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to throw the exception.
Use throw statement.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, it is not always meant to throw NullPointerException.
Go for throwing IllegalArgumentException with an informative message.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the below Ln:69, it seemed that you don't want to throw the exception if the password is missing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I'm thinking:

if accountName is null, you can throw illegal argument exception
but if password is null, I think, you can continue without throwing the exception.

Because, mAccount is set to null, it will be initialized at the Ln:70.

mAccount = account;
}else {
// - case migration from old AccountManagerStoreDataSource
mAccount = null;
identifier = new SharedStorageIdentifier();
}
} else {
throw new IllegalArgumentException("Invalid parameters, Account name cannot be null");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional], "Account name cannot be null" is sufficient here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graju256 @zakirjt Added some logs to capture some details inside the catch block. Can you please review that.

}
}

//Create the account if it wasn't retrieved,
if (mAccount == null) {
sb.append("Account Name:: "+accountName);
sb.append("Account Type:: "+accountName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Account Type:: "+accountName , this should be accountType.

for (Account account : accounts) {
if (accountName.equals(account.name)) {
if (accountName != null &&accountName.equals(account.name)) {
String password = mAccountManager.getPassword(account);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to put some logging in this block also.
As later if password does not match, we make mAccount to null, and which eventually will go to the if(mAccount==null) block.

mAccount = account;
}else {
// - case migration from old AccountManagerStoreDataSource
mAccount = null;
identifier = new SharedStorageIdentifier();
}
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be throwing the exception for name mismatch. It's a mistake.

We need to rewrite the logic like below:

accountName is invariant to the loop. Hence, a null check for the accountName should be done outside the for-loop.

if (acoountName == null) {
throw new IllegalArgumentException("Invalid parameters, Account name cannot be null");
}

for (Account account : accounts) {
     if (accountName.equals(account.name)) {
     }
}

@@ -51,27 +53,35 @@ public AccountManagerUtil(Context context, String accountName, boolean sharedSto
mAccountManager = AccountManager.get(MAS.getContext());
//Attempt to retrieve the account
Account[] accounts = mAccountManager.getAccountsByType(accountType);
sb.append("No of accounts:: "+accounts.length);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you simply log the details wherever you want!

@@ -51,27 +58,42 @@ public AccountManagerUtil(Context context, String accountName, boolean sharedSto
mAccountManager = AccountManager.get(MAS.getContext());
//Attempt to retrieve the account
Account[] accounts = mAccountManager.getAccountsByType(accountType);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply synchronization to the block that consists of the below logic:
synchronized {

  • get-accounts
  • iterate
  • add-account-explicitly
    }

mAccount = null;
identifier = new SharedStorageIdentifier();
messageBuilder.append(" trying account:" + account.name);
synchronized (this) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use class-object for synchronization.
synchronization (AccountManagerUtil.class) {
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I use the object already created (mutex)?

Copy link

@graju256 graju256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me.

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

Successfully merging this pull request may close these issues.

None yet

4 participants