📄 dumbmethods.java
字号:
if (stackLoc >= 0) { OpcodeStack.Item tos = stack.getStackItem(stackLoc); switch (tos.getSpecialKind()) { case OpcodeStack.Item.HASHCODE_INT_REMAINDER: bugReporter.reportBug(new BugInstance(this, "RV_REM_OF_HASHCODE", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); break; case OpcodeStack.Item.RANDOM_INT: case OpcodeStack.Item.RANDOM_INT_REMAINDER: bugReporter.reportBug(new BugInstance(this, "RV_REM_OF_RANDOM_INT", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); break; } } if (seen == IREM) { OpcodeStack.Item item0 = stack.getStackItem(0); Object constant0 = item0.getConstant(); OpcodeStack.Item item1 = stack.getStackItem(1); int special = item1.getSpecialKind(); if (constant0 instanceof Integer && ((Integer)constant0).intValue() == 1) bugReporter.reportBug(new BugInstance(this, "INT_BAD_REM_BY_1", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); } if (stack.getStackDepth() >= 1 && (seen == LOOKUPSWITCH || seen == TABLESWITCH)) { OpcodeStack.Item item0 = stack.getStackItem(0); if (item0.getSpecialKind() == OpcodeStack.Item.SIGNED_BYTE) { int[] switchLabels = getSwitchLabels(); int [] switchOffsets = getSwitchOffsets(); for(int i = 0; i < switchLabels.length; i++) { int v = switchLabels[i]; if (v <= -129 || v >= 128) bugReporter.reportBug(new BugInstance(this, "INT_BAD_COMPARISON_WITH_SIGNED_BYTE", HIGH_PRIORITY) .addClassAndMethod(this) .addInt(v).describe(IntAnnotation.INT_VALUE) .addSourceLine(this, getPC() + switchOffsets[i])); } } } // check for use of signed byte where is it assumed it can be out of the -128...127 range if (stack.getStackDepth() >= 2) switch (seen) { case IF_ICMPEQ: case IF_ICMPNE: case IF_ICMPLT: case IF_ICMPLE: case IF_ICMPGE: case IF_ICMPGT: OpcodeStack.Item item0 = stack.getStackItem(0); OpcodeStack.Item item1 = stack.getStackItem(1); int seen2 = seen; if (item0.getConstant() != null) { OpcodeStack.Item tmp = item0; item0 = item1; item1 = tmp; switch(seen) { case IF_ICMPLT: seen2 = IF_ICMPGT; break; case IF_ICMPGE: seen2 = IF_ICMPLE; break; case IF_ICMPGT: seen2 = IF_ICMPLT; break; case IF_ICMPLE: seen2 = IF_ICMPGE; break; } } Object constant1 = item1.getConstant(); if (item0.getSpecialKind() == OpcodeStack.Item.SIGNED_BYTE && constant1 instanceof Number) { int v1 = ((Number)constant1).intValue(); if (v1 <= -129 || v1 >= 128 || v1 == 127 && !(seen2 == IF_ICMPEQ || seen2 == IF_ICMPNE )) { int priority = HIGH_PRIORITY; if (v1 == 127) { switch(seen2) { case IF_ICMPGT: // 127 > x priority = LOW_PRIORITY; break; case IF_ICMPGE: // 127 >= x : always true priority = HIGH_PRIORITY; break; case IF_ICMPLT: // 127 < x : never true priority = HIGH_PRIORITY; break; case IF_ICMPLE: // 127 <= x priority = LOW_PRIORITY; break; } } else if (v1 == 128) { switch(seen2) { case IF_ICMPGT: // 128 > x priority = NORMAL_PRIORITY; break; case IF_ICMPGE: // 128 >= x priority = HIGH_PRIORITY; break; case IF_ICMPLT: // 128 < x priority = HIGH_PRIORITY; break; case IF_ICMPLE: // 128 <= x priority = HIGH_PRIORITY; break; } } else if (v1 <= -129) priority = NORMAL_PRIORITY; bugReporter.reportBug(new BugInstance(this, "INT_BAD_COMPARISON_WITH_SIGNED_BYTE", priority) .addClassAndMethod(this) .addInt(v1).describe(IntAnnotation.INT_VALUE) .addSourceLine(this)); } } else if (item0.getSpecialKind() == OpcodeStack.Item.MASKED_NON_NEGATIVE && constant1 instanceof Number) { int v1 = ((Number)constant1).intValue(); if (v1 < 0) bugReporter.reportBug(new BugInstance(this, "INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE", HIGH_PRIORITY) .addClassAndMethod(this) .addInt(v1).describe(IntAnnotation.INT_VALUE) .addSourceLine(this)); } } if (checkForBitIorofSignedByte && seen != I2B) { bugReporter.reportBug(new BugInstance(this, "BIT_IOR_OF_SIGNED_BYTE", prevOpcode == LOR ? HIGH_PRIORITY : NORMAL_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); checkForBitIorofSignedByte = false; } else if ((seen == IOR || seen == LOR) && stack.getStackDepth() >= 2) { OpcodeStack.Item item0 = stack.getStackItem(0); OpcodeStack.Item item1 = stack.getStackItem(1); int special0 = item0.getSpecialKind(); int special1 = item1.getSpecialKind(); if (special0 == OpcodeStack.Item.SIGNED_BYTE && special1 == OpcodeStack.Item.LOW_8_BITS_CLEAR || special0 == OpcodeStack.Item.LOW_8_BITS_CLEAR && special1 == OpcodeStack.Item.SIGNED_BYTE ) checkForBitIorofSignedByte = true; else checkForBitIorofSignedByte = false; } else checkForBitIorofSignedByte = false; if (prevOpcodeWasReadLine && sinceBufferedInputStreamReady >= 100 && seen == INVOKEVIRTUAL && getClassConstantOperand().equals("java/lang/String") && getSigConstantOperand().startsWith("()")) { String method = getNameConstantOperand(); String sig = getSigConstantOperand(); bugReporter.reportBug(new BugInstance(this, "NP_IMMEDIATE_DEREFERENCE_OF_READLINE", NORMAL_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); } if (seen == INVOKEVIRTUAL && getClassConstantOperand().equals("java/io/BufferedReader") && getNameConstantOperand().equals("ready") && getSigConstantOperand().equals("()Z")) sinceBufferedInputStreamReady = 0; else sinceBufferedInputStreamReady++; prevOpcodeWasReadLine = (seen == INVOKEVIRTUAL||seen == INVOKEINTERFACE) && getNameConstantOperand().equals("readLine") && getSigConstantOperand().equals("()Ljava/lang/String;"); // System.out.println(randomNextIntState + " " + OPCODE_NAMES[seen] + " " + getMethodName()); switch(randomNextIntState) { case 0: if (seen == INVOKEVIRTUAL && getClassConstantOperand().equals("java/util/Random") && getNameConstantOperand().equals("nextDouble") || seen == INVOKESTATIC && getClassConstantOperand().equals("java/lang/Math") && getNameConstantOperand().equals("random")) randomNextIntState = 1; break; case 1: if (seen == D2I) { bugReporter.reportBug(new BugInstance(this, "RV_01_TO_INT", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); randomNextIntState = 0; } else if (seen == DMUL) randomNextIntState = 4; else randomNextIntState = 2; break; case 2: if (seen == I2D) randomNextIntState = 3; else if (seen == DMUL) randomNextIntState = 4; else randomNextIntState = 0; break; case 3: if (seen == DMUL) randomNextIntState = 4; else randomNextIntState = 0; break; case 4: if (seen == D2I) bugReporter.reportBug(new BugInstance(this, "DM_NEXTINT_VIA_NEXTDOUBLE", NORMAL_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); randomNextIntState = 0; break; default: throw new IllegalStateException(); } if (isPublicStaticVoidMain && seen == INVOKEVIRTUAL && getClassConstantOperand().startsWith("javax/swing/") && (getNameConstantOperand().equals("show") && getSigConstantOperand().equals("()V") || getNameConstantOperand().equals("pack") && getSigConstantOperand().equals("()V") || getNameConstantOperand().equals("setVisible") && getSigConstantOperand().equals("(Z)V"))) bugReporter.reportBug(new BugInstance(this, "SW_SWING_METHODS_INVOKED_IN_SWING_THREAD", LOW_PRIORITY) .addClassAndMethod(this) .addSourceLine(this));// if ((seen == INVOKEVIRTUAL)// && getClassConstantOperand().equals("java/lang/String")// && getNameConstantOperand().equals("substring")// && getSigConstantOperand().equals("(I)Ljava/lang/String;")// && stack.getStackDepth() > 1) {// OpcodeStack.Item item = stack.getStackItem(0);// Object o = item.getConstant();// if (o != null && o instanceof Integer) {// int v = ((Integer) o).intValue();// if (v == 0)// bugReporter.reportBug(new BugInstance(this, "DMI_USELESS_SUBSTRING", NORMAL_PRIORITY)// .addClassAndMethod(this)// .addSourceLine(this));// }// } if ((seen == INVOKEVIRTUAL) && getNameConstantOperand().equals("isAnnotationPresent") && getSigConstantOperand().equals("(Ljava/lang/Class;)Z") && stack.getStackDepth() > 0) { OpcodeStack.Item item = stack.getStackItem(0); Object value = item.getConstant(); if (value instanceof String) { String annotationClassName = (String) value; boolean lacksClassfileRetention = AnalysisContext.currentAnalysisContext().getAnnotationRetentionDatabase().lacksClassfileRetention( annotationClassName.replace('/','.')); if (lacksClassfileRetention) bugReporter.reportBug(new BugInstance(this, "DMI_ANNOTATION_IS_NOT_VISIBLE_TO_REFLECTION", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this) .addCalledMethod(this)); } } if ((seen == INVOKEVIRTUAL) && getNameConstantOperand().equals("next") && getSigConstantOperand().equals("()Ljava/lang/Object;") && getMethodName().equals("hasNext") && getMethodSig().equals("()Z") && stack.getStackDepth() > 0) { OpcodeStack.Item item = stack.getStackItem(0); bugReporter.reportBug(new BugInstance(this, "DMI_CALLING_NEXT_FROM_HASNEXT", item.isInitialParameter() && item.getRegisterNumber() == 0 ? NORMAL_PRIORITY : LOW_PRIORITY) .addClassAndMethod(this) .addSourceLine(this) .addCalledMethod(this)); } if ((seen == INVOKESPECIAL) && getClassConstantOperand().equals("java/lang/String") && getNameConstantOperand().equals("<init>") && getSigConstantOperand().equals("(Ljava/lang/String;)V")) if (alreadyReported.add(getRefConstantOperand())) bugReporter.reportBug(new BugInstance(this, "DM_STRING_CTOR", NORMAL_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); if (seen == INVOKESTATIC && getClassConstantOperand().equals("java/lang/System") && getNameConstantOperand().equals("runFinalizersOnExit") || seen == INVOKEVIRTUAL && getClassConstantOperand().equals("java/lang/Runtime") && getNameConstantOperand().equals("runFinalizersOnExit")) bugReporter.reportBug(new BugInstance(this, "DM_RUN_FINALIZERS_ON_EXIT", HIGH_PRIORITY) .addClassAndMethod(this) .addSourceLine(this)); if ((seen == INVOKESPECIAL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -