Skip to content

Commit 8c298a2

Browse files
authored
Fix NullPointerException in ExceptionProbe (#11051)
Fix NullPointerException in ExceptionProbe getInnerMostThrowable can return null if there is cycle in causes need to check for function return fix test Co-authored-by: jean-philippe.bempel <jean-philippe.bempel@datadoghq.com>
1 parent e16815c commit 8c298a2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/ExceptionProbe.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public void evaluate(
9595
return;
9696
}
9797
Throwable innerMostThrowable = getInnerMostThrowable(throwable);
98+
if (innerMostThrowable == null) {
99+
LOGGER.debug("Cannot get inner most throwable (cycle?)");
100+
return;
101+
}
98102
String fingerprint =
99103
Fingerprinter.fingerprint(innerMostThrowable, exceptionProbeManager.getClassNameFilter());
100104
if (exceptionProbeManager.shouldCaptureException(fingerprint)) {

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/util/ExceptionHelperTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
67
import static org.mockito.ArgumentMatchers.anyString;
78
import static org.mockito.Mockito.doAnswer;
@@ -74,7 +75,7 @@ public void foldExceptionStackTrace() {
7475
String strStackTrace = ExceptionHelper.foldExceptionStackTrace(new Exception());
7576
assertTrue(
7677
strStackTrace.startsWith(
77-
"java.lang.Exception at com.datadog.debugger.util.ExceptionHelperTest.foldExceptionStackTrace(ExceptionHelperTest.java:74) at "),
78+
"java.lang.Exception at com.datadog.debugger.util.ExceptionHelperTest.foldExceptionStackTrace(ExceptionHelperTest.java:75) at "),
7879
strStackTrace);
7980
assertFalse(strStackTrace.contains("\n"));
8081
assertFalse(strStackTrace.contains("\t"));
@@ -96,6 +97,10 @@ public void innerMostException() {
9697
assertEquals(nested, exceptions.pollLast());
9798
assertEquals(nested.getCause(), exceptions.pollLast());
9899
assertEquals(nested.getCause().getCause(), exceptions.pollLast());
100+
// create a cycle and make sure we handle it gracefully
101+
Exception ex2 = new RuntimeException("test2", ex);
102+
ex.initCause(ex2);
103+
assertNull(ExceptionHelper.getInnerMostThrowable(ex2));
99104
}
100105

101106
@Test

0 commit comments

Comments
 (0)