Menu

#61 false positive in MRC_METHOD_RETURNS_CONSTANT

closed-fixed
Detector (52)
5
2013-02-10
2013-02-07
Tobias Baum
No

Hi,

I'm having some false positives with the MRC_METHOD_RETURNS_CONSTANT bug.

Example Java code:
private int findComponent(Component component) {
int index = 0;
while (this.components[index] != component) {
index++;
if (index >= this.components.length) {
return 0;
}
}
return index;
}

Corresponding bytecode:

private int findComponent(java.awt.Component);
Code:
0: iconst_0
1: istore_2
2: goto 19
5: iinc 2, 1
8: iload_2
9: aload_0
10: getfield #30; //Field components:[Ljavax/swing/JComponent;
13: arraylength
14: if_icmplt 19
17: iconst_0
18: ireturn
19: aload_0
20: getfield #30; //Field components:[Ljavax/swing/JComponent;
23: iload_2
24: aaload
25: aload_1
26: if_acmpne 5
29: iload_2
30: ireturn

The detector complains that zero is returned always, but that would only be the case if the loop/jump would never be taken, which is definitely not the case.

Another example (seems to be the same/similar problem):

Java code:
private int determineFilterHeight() {
int maxHeight = 0;
for (final SETTableColumn factory : this.availableColumns) {
maxHeight = Math.max(maxHeight, factory.getFilterHeight());
}
return maxHeight;
}

And again the corresponding bytecode:

private int determineFilterHeight();
Code:
0: iconst_0
1: istore_1
2: aload_0
3: getfield #81; //Field availableColumns:Ljava/util/List;
6: invokeinterface #178, 1; //InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
11: astore_3
12: goto 34
15: aload_3
16: invokeinterface #182, 1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
21: checkcast #139; //class de/setsoftware/gwtlib/web/table/SETTableColumn
24: astore_2
25: iload_1
26: aload_2
27: invokevirtual #188; //Method de/setsoftware/gwtlib/web/table/SETTableColumn.getFilterHeight:()I
30: invokestatic #191; //Method java/lang/Math.max:(II)I
33: istore_1
34: aload_3
35: invokeinterface #197, 1; //InterfaceMethod java/util/Iterator.hasNext:()Z
40: ifne 15
43: iload_1
44: ireturn

Same false complaint: The detector says constant zero is returned, but it's not.

I'm using fb-contrib 4.8.2 with FindBugs 2.0.2. I think I didn't have this problem with an older version (fb-contrib 4.4.0 with FindBugs 1.3.9).

Thanks in advance for having a look at this (and thanks for providing this project).

Discussion

  • Dave Brosius

    Dave Brosius - 2013-02-09

    yes, a change was made in findbugs proper as to how constants were handled... I think i've fixed a bunch of these cases in trunk already, but i'll look at your particular examples. thanks.

     
  • Dave Brosius

    Dave Brosius - 2013-02-10
    • assigned_to: nobody --> dbrosius
    • status: open --> closed-fixed
     
  • Dave Brosius

    Dave Brosius - 2013-02-10

    fixed in trunk, thanks for the report

     

Log in to post a comment.

MongoDB Logo MongoDB