Fix NPE when account has no preferredEmail
The NPE is thrown by AutoValue when constructing Address, as email is
not marked as Nullable. We could add a null guard in sendImpl, but
instead we make sure that any account in rcptTo has a valid associated
email. Otherwise there is no point for them being in the set of
recipients.
Google-Bug-Id: b/285080850
Release-Notes: skip
Change-Id: I4ba30c1027f06e365918a5ebb14e928df122f81b
diff --git a/java/com/google/gerrit/server/mail/send/OutgoingEmail.java b/java/com/google/gerrit/server/mail/send/OutgoingEmail.java
index afdcbad..8500388 100644
--- a/java/com/google/gerrit/server/mail/send/OutgoingEmail.java
+++ b/java/com/google/gerrit/server/mail/send/OutgoingEmail.java
@@ -663,10 +663,16 @@
*/
public void addByAccountId(RecipientType rt, Account.Id to, boolean override) {
try {
- if (!rcptTo.contains(to) && isRecipientAllowed(to)) {
- rcptTo.add(to);
- add(rt, toAddress(to), override);
+ if (rcptTo.contains(to) || !isRecipientAllowed(to)) {
+ return;
}
+ Address addr = toAddress(to);
+ if (addr == null) {
+ logger.atFine().log("Not emailing account %s because user has no preferred email", to);
+ return;
+ }
+ rcptTo.add(to);
+ add(rt, addr, override);
} catch (PermissionBackendException e) {
logger.atSevere().withCause(e).log("Error checking permissions for account: %s", to);
}