From 4830673b523fe35af4ad86f4ebe2dcb3408a20ae Mon Sep 17 00:00:00 2001
From: Ryan Izard <rizard@g.clemson.edu>
Date: Fri, 20 Feb 2015 12:46:58 -0500
Subject: [PATCH] Account for cases where we might try to remove a CounterNode
 from an empty hierarchy.

---
 .../debugcounter/CounterNode.java                    | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/main/java/net/floodlightcontroller/debugcounter/CounterNode.java b/src/main/java/net/floodlightcontroller/debugcounter/CounterNode.java
index 1f3c87aae..99f32f371 100644
--- a/src/main/java/net/floodlightcontroller/debugcounter/CounterNode.java
+++ b/src/main/java/net/floodlightcontroller/debugcounter/CounterNode.java
@@ -11,6 +11,9 @@ import java.util.regex.Pattern;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 
@@ -27,6 +30,7 @@ import com.google.common.collect.ImmutableList;
  */
 class CounterNode implements Iterable<DebugCounterImpl> {
     private static final String QUOTED_SEP = Pattern.quote("/");
+    private static final Logger log = LoggerFactory.getLogger(CounterNode.class);
 
     /** path/hierarchy of this counter without leading /. An empty string
      * represents the root. A string without a / is a module name
@@ -168,6 +172,14 @@ class CounterNode implements Iterable<DebugCounterImpl> {
          * We're directly reusing the shrunken hierarchyElements List and
          * keyToRemove String, which IMHO is pretty cool how it works out.
          */
+        
+        /*
+         * Make sure it's possible to remove something.
+         */
+        if (hierarchyElements.isEmpty()) {
+        	log.error("Cannot remove a CounterNode from an empty list of hierarchy elements. Returning null.");
+        	return null;
+        } 
         String keyToRemove = hierarchyElements.remove(hierarchyElements.size() - 1); 
         for (String element: hierarchyElements) {
             cur = cur.children.get(element);
-- 
GitLab