From 1e2ead0a126b0034ee400f1c440e6d6226cd8b83 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 31 Aug 2017 10:02:37 +0900 Subject: [PATCH] StreamEvents: Fix NPE when invoking the command with --help When executing the command: ssh user@host gerrit stream-events --help the onExit and destroy methods attempt to remove the event listener registration which has not been initialized, resulting in NPE and the command hanging. Add null checks to prevent this. Change-Id: I1b9ccd41d017e62f0a4206114ab15faa6ed8e000 --- .../google/gerrit/sshd/commands/StreamEvents.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java index 1c0a4247ac..76d15f7c1e 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java @@ -169,9 +169,15 @@ final class StreamEvents extends BaseCommand { .create(); } + private void removeEventListenerRegistration() { + if (eventListenerRegistration != null) { + eventListenerRegistration.remove(); + } + } + @Override protected void onExit(final int rc) { - eventListenerRegistration.remove(); + removeEventListenerRegistration(); synchronized (taskLock) { done = true; @@ -182,7 +188,7 @@ final class StreamEvents extends BaseCommand { @Override public void destroy() { - eventListenerRegistration.remove(); + removeEventListenerRegistration(); final boolean exit; synchronized (taskLock) { @@ -230,7 +236,7 @@ final class StreamEvents extends BaseCommand { // destroy() above, or it closed the stream and is no longer // accepting output. Either way terminate this instance. // - eventListenerRegistration.remove(); + removeEventListenerRegistration(); flush(); onExit(0); return;