Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in StringSwitchJavacFilter #1189

Merged
merged 3 commits into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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