From 824675d072f94888ebc91d0abdbc386e3703d4dc Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 16 Apr 2013 06:49:33 -0700 Subject: [PATCH] Guarding ExtensionStack.toString() from NPE --- .../common/extensions/ExtensionStack.java | 38 +++++++++++++------ .../common/extensions/ExtensionStackTest.java | 8 ++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStack.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStack.java index c3e266e6b1c..29450cf82a9 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStack.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStack.java @@ -251,19 +251,35 @@ public class ExtensionStack extends ContainerLifeCycle implements IncomingFrames { StringBuilder s = new StringBuilder(); s.append("ExtensionStack["); - s.append("extensions=["); - boolean delim = false; - for (Extension ext : extensions) + s.append("extensions="); + if (extensions == null) { - if (delim) - { - s.append(','); - } - s.append(ext.getName()); - delim = true; + s.append(""); } - s.append("],incoming=").append(this.nextIncoming.getClass().getName()); - s.append(",outgoing=").append(this.nextOutgoing.getClass().getName()); + else + { + s.append('['); + boolean delim = false; + for (Extension ext : extensions) + { + if (delim) + { + s.append(','); + } + if (ext == null) + { + s.append(""); + } + else + { + s.append(ext.getName()); + } + delim = true; + } + s.append(']'); + } + s.append(",incoming=").append((this.nextIncoming == null)?"":this.nextIncoming.getClass().getName()); + s.append(",outgoing=").append((this.nextOutgoing == null)?"":this.nextOutgoing.getClass().getName()); s.append("]"); return s.toString(); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java index 8b2825a97ee..096dc82d2d2 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java @@ -160,4 +160,12 @@ public class ExtensionStackTest stack.stop(); } } + + @Test + public void testToString() + { + ExtensionStack stack = createExtensionStack(); + // Shouldn't cause a NPE. + LOG.debug("Shouldn't cause a NPE: {}",stack.toString()); + } }