dcapwell commented on code in PR #2104:
URL: https://github.com/apache/cassandra/pull/2104#discussion_r1134554042


##########
src/java/org/apache/cassandra/streaming/StreamSession.java:
##########
@@ -1342,37 +1342,37 @@ public static StringBuilder boundStackTrace(Throwable 
e, int limit)
     {
         Set<Throwable> visited = Collections.newSetFromMap(new 
IdentityHashMap<>());
         StringBuilder out = new StringBuilder();
-        return boundStackTrace(e, limit, visited, out);
+        int counter = limit;
+        return boundStackTrace(e, limit, counter, visited, out);
     }
 
-    public static StringBuilder boundStackTrace(Throwable e, int limit, 
Set<Throwable> visited, StringBuilder out)
+    public static StringBuilder boundStackTrace(Throwable e, int limit, int 
counter, Set<Throwable> visited, StringBuilder out)
     {
         if (e == null)
-            return null;
+            return out;
 
         if (!visited.add(e))
             return out.append("[CIRCULAR REFERENCE: 
").append(e.getClass().getName()).append(": 
").append(e.getMessage()).append("]");
         visited.add(e);
 
-        if (e.getStackTrace().length == 0 || e.getStackTrace().length < limit)
-        {
-            out.append(e.getClass().getName()).append(": 
").append(e.getMessage()).append('\n');
-            boundStackTrace(e.getCause(), limit, visited, out);
-            return out;
-        }
+        int stackTraceSize = e.getStackTrace().length;
 
         StackTraceElement[] stackTrace = e.getStackTrace();
-        StringBuilder stackTraceBuilder = new StringBuilder();
-        stackTraceBuilder.append(e.getClass().getName() + ": " + 
e.getMessage()).append('\n');
+        out.append(e.getClass().getName() + ": " + 
e.getMessage()).append('\n');
 
-        for (int i = 0; i < limit; i++)
+        int i = 0;
+        while (i < Math.min(stackTraceSize, limit) && counter > 0)

Review Comment:
   Should replace with
   
   ```
   for (int i = 0, size = Math.min(e.getStackTrace().length, limit); i < size 
&& counter > 0)
   ```
   
   this lets you avoid `I` leaking outside the loop and lets you remove 
variable `stackTraceSize`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to