Skip to content

Commit

Permalink
Fix NullPointerException Extracting Class symbols (#7934)
Browse files Browse the repository at this point in the history
In rare cases, class super name is null (normally only for
java.lag.Object). Handle nicely this case.
  • Loading branch information
jpbempel authored Nov 13, 2024
1 parent 8d7866c commit 9246b9f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.objectweb.asm.Type.getObjectType;

import com.datadog.debugger.agent.Generated;
import datadog.trace.util.Strings;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand All @@ -17,6 +18,7 @@
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.InsnList;
Expand Down Expand Up @@ -331,6 +333,13 @@ private static int widenIntType(int sort) {
return sort;
}

public static String extractSuperClass(ClassNode classNode) {
if (classNode.superName == null) {
return Object.class.getTypeName();
}
return Strings.getClassName(classNode.superName);
}

/** Wraps ASM's {@link org.objectweb.asm.Type} with associated generic types */
public static class Type {
private final org.objectweb.asm.Type mainType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datadog.debugger.instrumentation;

import static com.datadog.debugger.instrumentation.ASMHelper.extractSuperClass;
import static com.datadog.debugger.instrumentation.ASMHelper.getStatic;
import static com.datadog.debugger.instrumentation.ASMHelper.invokeConstructor;
import static com.datadog.debugger.instrumentation.ASMHelper.invokeStatic;
Expand Down Expand Up @@ -37,7 +38,6 @@
import datadog.trace.bootstrap.debugger.MethodLocation;
import datadog.trace.bootstrap.debugger.ProbeId;
import datadog.trace.bootstrap.debugger.util.Redaction;
import datadog.trace.util.Strings;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -1161,7 +1161,7 @@ private static void addInheritedFields(
Limits limits,
List<FieldNode> results,
int fieldCount) {
String superClassName = Strings.getClassName(classNode.superName);
String superClassName = extractSuperClass(classNode);
while (!superClassName.equals(Object.class.getTypeName())) {
Class<?> clazz;
try {
Expand Down Expand Up @@ -1215,7 +1215,7 @@ private static void addInheritedStaticFields(
Limits limits,
List<FieldNode> results,
int fieldCount) {
String superClassName = Strings.getClassName(classNode.superName);
String superClassName = extractSuperClass(classNode);
while (!superClassName.equals(Object.class.getTypeName())) {
Class<?> clazz;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static Scope extractScopes(ClassNode classNode, String jarName) {
.addModifiers(extractClassModifiers(classNode.access))
.addInterfaces(extractInterfaces(classNode))
.addAnnotations(extractAnnotations(classNode.visibleAnnotations))
.superClass(Strings.getClassName(classNode.superName))
.superClass(ASMHelper.extractSuperClass(classNode))
.build();
Scope classScope =
Scope.builder(ScopeType.CLASS, sourceFile, classStartLine, classEndLine)
Expand Down

0 comments on commit 9246b9f

Please sign in to comment.