Skip to content

Commit 18f571a

Browse files
Godinmarchof
authored andcommitted
Fix NPE in StringSwitchEcjFilter (bazel-contrib#944)
1 parent 19ef126 commit 18f571a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/StringSwitchEcjFilterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,22 @@ public void should_filter_when_default_is_first() {
155155
assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
156156
}
157157

158+
@Test
159+
public void should_not_filter_empty_lookup_switch() {
160+
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
161+
"name", "(Ljava/lang/String;)V", null, null);
162+
m.visitVarInsn(Opcodes.ALOAD, 1);
163+
m.visitVarInsn(Opcodes.ASTORE, 2);
164+
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode",
165+
"()I", false);
166+
final Label defaultCase = new Label();
167+
m.visitLookupSwitchInsn(defaultCase, null, new Label[] {});
168+
m.visitLabel(defaultCase);
169+
m.visitInsn(Opcodes.RETURN);
170+
171+
filter.filter(m, context, output);
172+
173+
assertIgnored();
174+
}
175+
158176
}

org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/StringSwitchEcjFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public void match(final AbstractInsnNode start,
7070
final Set<AbstractInsnNode> replacements = new HashSet<AbstractInsnNode>();
7171
replacements.add(skipNonOpcodes(defaultLabel));
7272

73+
if (hashCodes == 0) {
74+
return;
75+
}
76+
7377
for (int i = 0; i < hashCodes; i++) {
7478
while (true) {
7579
nextIsVar(Opcodes.ALOAD, "s");

org.jacoco.doc/docroot/doc/changes.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ <h3>Fixed bugs</h3>
4848
that source references are actually files
4949
(GitHub <a href="https://github.com/jacoco/jacoco/issues/941">#941</a>).</li>
5050
<li><code>NullPointerException</code> during filtering
51-
(GitHub <a href="https://github.com/jacoco/jacoco/issues/942">#942</a>).</li>
51+
(GitHub <a href="https://github.com/jacoco/jacoco/issues/942">#942</a>,
52+
<a href="https://github.com/jacoco/jacoco/issues/944">#944</a>).</li>
5253
</ul>
5354

5455
<h3>Non-functional Changes</h3>

0 commit comments

Comments
 (0)