Skip to content

Commit 92779f6

Browse files
authored
Fix TOCTOU race causing NPE in Hibernate closeScope (#11955)
Fix TOCTOU race causing NPE in Hibernate closeScope Co-authored-by: andrea.marziali <andrea.marziali@datadoghq.com>
1 parent ddfe2e8 commit 92779f6

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

  • dd-java-agent/instrumentation/hibernate/hibernate-common/src/main/java/datadog/trace/instrumentation/hibernate

dd-java-agent/instrumentation/hibernate/hibernate-common/src/main/java/datadog/trace/instrumentation/hibernate/SessionMethodUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ public static void closeScope(
6262
final Object entity,
6363
final boolean closeSpan) {
6464

65-
if (sessionState == null || sessionState.getMethodScope() == null) {
65+
final AgentScope scope = sessionState == null ? null : sessionState.getMethodScope();
66+
if (scope == null) {
6667
// This method call was re-entrant. Do nothing, since it is being traced by the parent/first
6768
// call.
6869
return;
6970
}
7071

7172
CallDepthThreadLocalMap.reset(SessionMethodUtils.class);
72-
final AgentScope scope = sessionState.getMethodScope();
73+
sessionState.setMethodScope(null);
7374
final AgentSpan span = scope.span();
7475
if (span != null && (sessionState.hasChildSpan() || closeSpan)) {
7576
DECORATOR.onError(span, throwable);
@@ -81,7 +82,6 @@ public static void closeScope(
8182
}
8283

8384
scope.close();
84-
sessionState.setMethodScope(null);
8585
}
8686

8787
/**

0 commit comments

Comments
 (0)