From d75352f042f32e09716180e8ed3950dba532cf8b Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Wed, 20 Jun 2018 10:33:42 +0900 Subject: [PATCH] OutgoingEmail#getNameEmailFor: Protect against null accountId If the accountId is null, it results in a NullPointerException when attempting to get the account from the account cache: at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:782) at com.google.common.cache.LocalCache.get(LocalCache.java:4146) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4151) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:5140) at com.google.gerrit.server.account.AccountCacheImpl.get(AccountCacheImpl.java:85) Check for null, and return the name and email address of the server identity, consistently with what #getNameFor does when the accountId is null. Update the Javadoc accordingly. Also update the Javadoc of #getNameFor to make it consistent. Change-Id: I8145ca79d2122616247b013f83ded50db1f30f62 --- .../gerrit/server/mail/send/OutgoingEmail.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/mail/send/OutgoingEmail.java b/gerrit-server/src/main/java/com/google/gerrit/server/mail/send/OutgoingEmail.java index 56585a4c29..5278ad330e 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/mail/send/OutgoingEmail.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/mail/send/OutgoingEmail.java @@ -341,7 +341,12 @@ public abstract class OutgoingEmail { } } - /** Lookup a human readable name for an account, usually the "full name". */ + /** + * Gets the human readable name for an account, usually the "full name". + * + * @param accountId user to fetch. + * @return name of the account, or the server identity name if null. + */ protected String getNameFor(final Account.Id accountId) { if (accountId == null) { return args.gerritPersonIdent.getName(); @@ -351,13 +356,17 @@ public abstract class OutgoingEmail { } /** - * Gets the human readable name and email for an account; if neither are available, returns the - * Anonymous Coward name. + * Gets the human readable name and email for an account. * * @param accountId user to fetch. - * @return name/email of account, or Anonymous Coward if unset. + * @return name/email of account; Anonymous Coward if unset or the server identity if null. */ public String getNameEmailFor(Account.Id accountId) { + if (accountId == null) { + return String.format( + "%s <%s>", args.gerritPersonIdent.getName(), args.gerritPersonIdent.getEmailAddress()); + } + return args.accountCache.get(accountId).getAccount().getNameEmail(args.anonymousCowardName); }