@Nonnull public SourceLineAnnotation getPrimarySourceLineAnnotation() { if (annotation instanceof SourceLineAnnotation && SourceLineAnnotation.DEFAULT_ROLE.equals(annotation.getDescription()) && !((SourceLineAnnotation) annotation).isUnknown()) { return (SourceLineAnnotation) annotation; if (annotation instanceof SourceLineAnnotation && !((SourceLineAnnotation) annotation).isUnknown()) { return (SourceLineAnnotation) annotation; if ((srcLine = inspectPackageMemberSourceLines(getPrimaryMethod())) != null) { return srcLine; if ((srcLine = inspectPackageMemberSourceLines(getPrimaryField())) != null) { return srcLine; if ((srcLine = inspectPackageMemberSourceLines(getPrimaryClass())) != null) { return srcLine; throw new IllegalStateException("BugInstance for " + getType() + " must contain at least one class, method, or field annotation");
@Override public BugInstance findBug(String instanceHash, String bugType, int lineNumber) { for (BugInstance bug : bugSet) { if (bug.getInstanceHash().equals(instanceHash) && bug.getBugPattern().getType().equals(bugType) && bug.getPrimarySourceLineAnnotation().getStartLine() == lineNumber) { return bug; } } return null; }
public static BugInstance obfuscate(BugInstance b) { final BugInstance result = new BugInstance(b.getType(), b.getPriority()); BugAnnotationVisitor visitor = new BugAnnotationVisitor() { for (BugAnnotation a : b.getAnnotations()) { a.accept(visitor); result.setOldInstanceHash(hashData(b.getInstanceHash())); result.setHistory(b); return result;
public void setHistory(BugInstance from) { long first = from.getFirstVersion(); long last = from.getLastVersion(); if (first > 0 && last >= 0 && first > last) { throw new IllegalArgumentException("from has version range " + first + "..." + last + " in " + from.getBugPattern() + "\n" + from.getMessage()); } setFirstVersion(first); setLastVersion(last); this.removedByChangeOfPersistingClass = from.removedByChangeOfPersistingClass; this.introducedByChangeOfExistingClass = from.introducedByChangeOfExistingClass; }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if ((access & Opcodes.ACC_STATIC) != 0 && (access & Opcodes.ACC_FINAL) != 0 && (access & Opcodes.ACC_PUBLIC) != 0 && !name.equals(name.toUpperCase())) { bugReporter.reportBug(new BugInstance(this, "NM_FIELD_NAMING_CONVENTION", Priorities.LOW_PRIORITY).addClass(this) .addField(this.name, name, desc, access)); } return null; }
/** * Add a class annotation. If this is the first class annotation added, it * becomes the primary class annotation. * * @param jclass * the JavaClass object for the class * @return this object */ @Nonnull public BugInstance addClass(JavaClass jclass) { addClass(jclass.getClassName()); return this; }
/** * Add a method annotation for the method which is called by given * instruction. * * @param methodGen * the method containing the call * @param inv * the InvokeInstruction * @return this object */ @Nonnull public BugInstance addCalledMethod(MethodGen methodGen, InvokeInstruction inv) { ConstantPoolGen cpg = methodGen.getConstantPool(); return addCalledMethod(cpg, inv); }
@Override public void reportBug(@Nonnull BugInstance bugInstance) { BugPattern bugPattern = bugInstance.getBugPattern(); String category = bugPattern.getCategory(); if (categorySet.contains(category)) { getDelegate().reportBug(bugInstance); } else { if (DEBUG) { System.out.println("CategoryFilteringBugReporter: filtered due to category " + category); } } } }
/** * Get the BugPattern. */ public @Nonnull BugPattern getBugPattern() { BugPattern result = DetectorFactoryCollection.instance().lookupBugPattern(getType()); if (result != null) { return result; } AnalysisContext.logError("Unable to find description of bug pattern " + getType()); result = DetectorFactoryCollection.instance().lookupBugPattern("UNKNOWN"); if (result != null) { return result; } return BugPattern.REALLY_UNKNOWN; }
if (!hasManyPreceedingNullTests(sourceLineAnnotation.getStartBytecode())) { hasManyNullTests = false; BugInstance bugInstance = new BugInstance(this, bugType, priority).addClassAndMethod(classContext.getJavaClass(), method); if (invokedMethod != null) { assert invokedXMethod != null; XMethod i = invokedXMethod.resolveAccessMethodForMethod(); if (i != invokedXMethod) { bugInstance.addMethod(i).describe(MethodAnnotation.METHOD_CALLED); } else { bugInstance.addMethod(invokedMethod).describe(MethodAnnotation.METHOD_CALLED) .addParameterAnnotation(parameterNumber, "INT_MAYBE_NULL_ARG"); bugInstance.addField(storedField).describe("FIELD_STORED"); bugInstance.addOptionalAnnotation(variableAnnotation); if (variableAnnotation instanceof FieldAnnotation) { bugInstance.describe("FIELD_CONTAINS_VALUE"); int pos = loc.getHandle().getPosition(); if (pos != source + 3) { bugInstance.addSourceLine(classContext, method, loc).describe(getDescription(loc, refValue)); bugInstance.add(sourceLineAnnotation).describe("SOURCE_LINE_NULL_VALUE"); bugInstance.add(sourceLineAnnotation).describe("SOURCE_LINE_KNOWN_NULL");
@Override public void visit(JavaClass obj) { String name = obj.getClassName(); String[] parts = name.split("[$+.]"); baseClassName = parts[parts.length - 1]; return; classIsPublicOrProtected = obj.isPublic() || obj.isProtected(); if (Character.isLetter(baseClassName.charAt(0)) && !Character.isUpperCase(baseClassName.charAt(0)) && baseClassName.indexOf('_') == -1) { int priority = classIsPublicOrProtected ? NORMAL_PRIORITY : LOW_PRIORITY; bugReporter.reportBug(new BugInstance(this, "NM_CLASS_NAMING_CONVENTION", priority).addClass(this)); bugReporter.reportBug(new BugInstance(this, "NM_CLASS_NOT_EXCEPTION", NORMAL_PRIORITY).addClass(this)); for (Field f : obj.getFields()) { if (f.getName().length() >= 2 && badFieldName(f)) { badFieldNames++; int badMethodNames = 0; for (Method m : obj.getMethods()) { if (badMethodName(m.getName())) { badMethodNames++;
Code code = obj.getCode(); Method realVoidConstructor = findVoidConstructor(getThisClass()); if (code != null && !markedAsNotUsable(obj)) { for (Method m : this.getThisClass().getMethods()) { if (!m.isStatic() && m != obj && !isVoidConstructor(getThisClass(), m)) { instanceMembers = true; bugReporter.reportBug(new BugInstance(this, "NM_METHOD_CONSTRUCTOR_CONFUSION", priority).addClassAndMethod(this) .lowerPriorityIfDeprecated()); return; bugReporter.reportBug(new BugInstance(this, "NM_METHOD_NAMING_CONVENTION", classIsPublicOrProtected && (obj.isPublic() || obj.isProtected()) && !hasBadMethodNames ? NORMAL_PRIORITY : LOW_PRIORITY) .addClassAndMethod(this)); bugReporter.reportBug(new BugInstance(this, "NM_BAD_EQUAL", HIGH_PRIORITY).addClassAndMethod(this) .lowerPriorityIfDeprecated()); return; bugReporter.reportBug(new BugInstance(this, "NM_LCASE_HASHCODE", HIGH_PRIORITY).addClassAndMethod(this) .lowerPriorityIfDeprecated()); return; bugReporter.reportBug(new BugInstance(this, "NM_LCASE_TOSTRING", HIGH_PRIORITY).addClassAndMethod(this) .lowerPriorityIfDeprecated()); return;
XMethod xmethod = XFactory.createXMethod(clazz, obj); XFactory factory = AnalysisContext.currentXFactory(); String key = obj.getName() + ":" + obj.getSignature(); if (!factory.isCalled(xmethod) && (obj.isStatic() || !definedInSuperClassOrInterface(clazz, key))) { int priority = NORMAL_PRIORITY; JavaClass superClass = clazz.getSuperClass(); String superClassName = superClass.getClassName(); if ("java.lang.Object".equals(superClassName)) { priority = NORMAL_PRIORITY; pendingBug = new BugInstance(this, "UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS", priority).addClassAndMethod(this); potentialSuperCall = null;
@Override public void visitField(Field obj) { if (obj.isProtected()) { bugReporter.reportBug(new BugInstance(this, "CI_CONFUSED_INHERITANCE", LOW_PRIORITY).addClass(cls).addField( new FieldAnnotation(cls.getClassName(), obj.getName(), obj.getSignature(), obj.isStatic()))); } }
classFields.clear(); Field[] fields = obj.getFields(); String fieldName; for (Field field : fields) { if (!field.isStatic() && !field.isPrivate()) { fieldName = field.getName(); classFields.put(fieldName, field); BugInstance bug = new BugInstance(this, "MF_CLASS_MASKS_FIELD", priority).addClass(this).addField(fa) .describe("FIELD_MASKING").addField(maskedFieldAnnotation).describe("FIELD_MASKED"); rememberedBugs.add(new RememberedBug(bug, fa, maskedFieldAnnotation));
if (aField.isPrivate()) { if (!aField.isStatic() && !"java/lang/Enum".equals(superclassName)) { return; if (!aField.isPublic() && !aField.isProtected()) { return; String tBugType = null; int priority = aField.isPublic() && aField.isFinal() && aField.getName().equals(aField.getName().toUpperCase()) && getThisClass().isPublic() ? HIGH_PRIORITY : NORMAL_PRIORITY; if (classOfField != null) { try { pendingBugs.put(getXField(), new BugInstance(this, tBugType, priority).addClass(currentClass).addField(this));
private void analyzeField(Field field, JavaClass javaClass) { for (AnnotationEntry annotation : field.getAnnotationEntries()) { if (ANNOTATION_TYPES.contains(annotation.getAnnotationType()) || annotation.getAnnotationType().contains("JsonTypeInfo")) { for (ElementValuePair elementValuePair : annotation.getElementValuePairs()) { if ("use".equals((elementValuePair.getNameString())) && VULNERABLE_USE_NAMES.contains(elementValuePair.getValue().stringifyValue())) { bugReporter.reportBug(new BugInstance(this, DESERIALIZATION_TYPE, HIGH_PRIORITY) .addClass(javaClass) .addString(javaClass.getClassName() + " on field " + field.getName() + " of type " + field.getType() + " annotated with " + annotation.toShortString()) .addField(FieldAnnotation.fromBCELField(javaClass, field)) .addString("") ); } } } } }