Skip to content

Commit

Permalink
Fixed FindDeadLocalStores to handle LOOKUPSWITCH and TABLESWITCH (#2937)
Browse files Browse the repository at this point in the history
* test: added more tests for false positives

* fix: a switch instruction is a BCEL Select

LOOKUPSWITCH and TABLESWITCH both extend Select, use the base class to
detect the start of a switch in FindDeadLocalStores
Updated the line numbers of expected bugs

* fix: reverted .* import

---------

Co-authored-by: Jeremy Landis <jeremylandis@hotmail.com>
  • Loading branch information
gtoison and hazendaz committed Apr 21, 2024
1 parent 1342c0f commit a197132
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
## Unreleased - 2024-??-??
### Fixed
- Fix FP `SING_SINGLETON_GETTER_NOT_SYNCHRONIZED` with eager instances ([#2932]https://github.com/spotbugs/spotbugs/issues/2932)
- Do not report DLS_DEAD_LOCAL_STORE for Java 21's type switches when switch instruction is TABLESWITCH([#2736](https://github.com/spotbugs/spotbugs/issues/2736))
- Fix FP `SE_BAD_FIELD` for record fields ([#2935]https://github.com/spotbugs/spotbugs/issues/2935)

## 4.8.4 - 2024-04-07
Expand Down
Expand Up @@ -25,13 +25,13 @@ void testIssue() {
assertBugCount("BC_UNCONFIRMED_CAST", 1);
assertBugCount("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", 1);

assertBugAtLine("BC_UNCONFIRMED_CAST", 50);
assertBugAtLine("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", 49);
assertBugAtLine("BC_UNCONFIRMED_CAST", 52);
assertBugAtLine("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", 51);

// Checks for issue 2736
assertBugCount("DLS_DEAD_LOCAL_STORE", 1);

assertBugAtLine("DLS_DEAD_LOCAL_STORE", 59);
assertBugAtLine("DLS_DEAD_LOCAL_STORE", 61);
}

private void assertBugCount(String type, int expectedCount) {
Expand Down
Expand Up @@ -52,11 +52,11 @@
import org.apache.bcel.generic.LRETURN;
import org.apache.bcel.generic.LSTORE;
import org.apache.bcel.generic.LoadInstruction;
import org.apache.bcel.generic.LOOKUPSWITCH;
import org.apache.bcel.generic.MULTIANEWARRAY;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.NEWARRAY;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.Select;
import org.apache.bcel.generic.StoreInstruction;
import org.apache.bcel.generic.Type;

Expand Down Expand Up @@ -276,8 +276,8 @@ private void analyzeMethod(ClassContext classContext, Method method) throws Data
switchHandler.sawInvokeDynamic(pc, invokeMethodName);

continue;
} else if (handle.getInstruction() instanceof LOOKUPSWITCH) {
LOOKUPSWITCH switchInstruction = (LOOKUPSWITCH) handle.getInstruction();
} else if (handle.getInstruction() instanceof Select) {
Select switchInstruction = (Select) handle.getInstruction();
int[] indices = switchInstruction.getIndices();

switchHandler.enterSwitch(switchInstruction.getOpcode(),
Expand Down
22 changes: 22 additions & 0 deletions spotbugsTestCases/src/java21/Issue2782.java
@@ -1,3 +1,5 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -62,4 +64,24 @@ public static int deadStore(int value) {
return 42;
}
}

public static int collectionSize(Object value) {
return switch (value) {
case Map<?, ?> map -> map.size();
case Set<?> set -> set.size();
case List<?> list -> list.size();
case Collection<?> collection ->
throw new UnsupportedOperationException("Error " + value.getClass());
case null, default -> 0;
};
}

public static String getValueType(Object value) {
return switch (value) {
case List<?> list -> "list";
case Map<?, ?> map -> "map";
case Number number -> "number";
case null, default -> "other";
};
}
}

0 comments on commit a197132

Please sign in to comment.