From 4e5e3b8e7eefc35cd1b46ad96040c8baa94cac01 Mon Sep 17 00:00:00 2001 From: Mark Shields Date: Wed, 20 Apr 2016 15:18:17 -0700 Subject: [PATCH 1/2] Protect against empty stack traces --- .../java/org/apache/beam/sdk/util/UserCodeException.java | 6 +++++- .../org/apache/beam/sdk/util/UserCodeExceptionTest.java | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 14443a43f9da..ba0696123628 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 @@ private UserCodeException(Throwable t) { * 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])) { 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 614c72f913a8..2c66d5b0af19 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 void testWrapIfReturnsSourceRuntimeExceptionWhenFalse() { 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 { From 3c6240c27d88e0673947560d08a804b0087cf3c1 Mon Sep 17 00:00:00 2001 From: Mark Shields Date: Wed, 20 Apr 2016 15:59:31 -0700 Subject: [PATCH 2/2] Thomas' comments --- .../main/java/org/apache/beam/sdk/util/UserCodeException.java | 2 +- .../java/org/apache/beam/sdk/util/UserCodeExceptionTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 ba0696123628..ad1cd8bd85b7 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 @@ -68,7 +68,7 @@ private void truncateStackTrace(Throwable t) { int currentStackSize = currentStack.length; int throwableStackSize = throwableStack.length; - if (throwableStack.length == 0) { + if (throwableStackSize == 0) { // Nothing to truncate. return; } 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 2c66d5b0af19..3be114605928 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 @@ -100,7 +100,7 @@ public void testWrapIfReturnsSourceRuntimeExceptionWhenFalse() { public void robustAgainstEmptyStackTrace() { RuntimeException runtimeException = new RuntimeException("empty stack"); runtimeException.setStackTrace(new StackTraceElement[0]); - RuntimeException wrapped = UserCodeException.wrapIf(true, runtimeException); + RuntimeException wrapped = UserCodeException.wrap(runtimeException); assertEquals(runtimeException, wrapped.getCause()); }