You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lc...@apache.org on 2016/04/21 01:23:53 UTC

[2/3] incubator-beam git commit: Protect against empty stack traces

Protect against empty stack traces


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/6334f72c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/6334f72c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/6334f72c

Branch: refs/heads/master
Commit: 6334f72c7a89bc0c4a229e4389a82b5f86530257
Parents: 3e0eaf5
Author: Mark Shields <ma...@google.com>
Authored: Wed Apr 20 15:18:17 2016 -0700
Committer: Luke Cwik <lc...@google.com>
Committed: Wed Apr 20 16:06:35 2016 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/beam/sdk/util/UserCodeException.java | 6 +++++-
 .../java/org/apache/beam/sdk/util/UserCodeExceptionTest.java  | 7 +++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/6334f72c/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UserCodeException.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UserCodeException.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UserCodeException.java
index 14443a4..ba06961 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UserCodeException.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/UserCodeException.java
@@ -62,13 +62,17 @@ public class UserCodeException extends RuntimeException {
    * of the current thread.
    */
   private void truncateStackTrace(Throwable t) {
-
     StackTraceElement[] currentStack = Thread.currentThread().getStackTrace();
     StackTraceElement[] throwableStack = t.getStackTrace();
 
     int currentStackSize = currentStack.length;
     int throwableStackSize = throwableStack.length;
 
+    if (throwableStack.length == 0) {
+      // Nothing to truncate.
+      return;
+    }
+
     int commonFrames = 0;
     while (framesEqual(currentStack[currentStackSize - commonFrames - 1],
         throwableStack[throwableStackSize - commonFrames - 1])) {

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/6334f72c/sdks/java/core/src/test/java/org/apache/beam/sdk/util/UserCodeExceptionTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/UserCodeExceptionTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/UserCodeExceptionTest.java
index 614c72f..2c66d5b 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/UserCodeExceptionTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/UserCodeExceptionTest.java
@@ -96,6 +96,13 @@ public class UserCodeExceptionTest {
     assertEquals(runtimeException, wrapped);
   }
 
+  @Test
+  public void robustAgainstEmptyStackTrace() {
+    RuntimeException runtimeException = new RuntimeException("empty stack");
+    runtimeException.setStackTrace(new StackTraceElement[0]);
+    RuntimeException wrapped = UserCodeException.wrapIf(true, runtimeException);
+    assertEquals(runtimeException, wrapped.getCause());
+  }
 
   private void throwUserCodeException() {
     try {