Skip to content

Commit

Permalink
Fix NPE in StringSwitchJavacFilter (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed May 19, 2021
1 parent 9b24fb5 commit a9ce74e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Expand Up @@ -115,6 +115,57 @@ public void should_filter_when_javac_generates_lookupswitch() {
assertIgnored(new Range(expectedFromInclusive, expectedToInclusive));
}

/**
* <code><pre>
* int c = -1;
* switch (s.hashCode()) {
* case 0:
* if (s.equals(""))
* c = 0;
* return;
* default:
* }
* switch (c)
* // ...
* </pre></code>
*/
@Test
public void should_not_filter_when_no_expected_goto() {
m.visitInsn(Opcodes.ICONST_M1);
m.visitVarInsn(Opcodes.ISTORE, 2);

m.visitVarInsn(Opcodes.ALOAD, 1);
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode",
"()I", false);

final Label secondSwitch = new Label();
final Label h1 = new Label();
m.visitTableSwitchInsn(0, 0, secondSwitch, h1);

m.visitLabel(h1);
m.visitVarInsn(Opcodes.ALOAD, 1);
m.visitLdcInsn("");
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals",
"(Ljava/lang/Object;)Z", false);
m.visitJumpInsn(Opcodes.IFEQ, secondSwitch);
m.visitInsn(Opcodes.ICONST_0);
m.visitVarInsn(Opcodes.ISTORE, 2);

// Something different from the expected by filter
// secondSwitch label or GOTO:
m.visitInsn(Opcodes.RETURN);

m.visitLabel(secondSwitch);
m.visitVarInsn(Opcodes.ILOAD, 2);
final Label defaultCase = new Label();
m.visitLookupSwitchInsn(defaultCase, new int[] {}, new Label[] {});
m.visitLabel(defaultCase);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_code_generated_by_ECJ() {
final Label h1 = new Label();
Expand Down
Expand Up @@ -87,6 +87,9 @@ boolean match(final AbstractInsnNode start,
break;
}
nextIs(Opcodes.GOTO);
if (cursor == null) {
return false;
}
if (((JumpInsnNode) cursor).label != secondSwitchLabel) {
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -20,6 +20,12 @@ <h1>Change History</h1>

<h2>Snapshot Build @qualified.bundle.version@ (@build.date@)</h2>

<h3>Fixed bugs</h3>
<ul>
<li>Fixed <code>NullPointerException</code> during filtering
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1189">#1189</a>).</li>
</ul>

<h2>Release 0.8.7 (2021/05/04)</h2>

<h3>New Features</h3>
Expand Down

0 comments on commit a9ce74e

Please sign in to comment.