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

NPE in KotlinWhenStringFilter #940

Closed
marchof opened this issue Sep 20, 2019 · 5 comments · Fixed by #942
Closed

NPE in KotlinWhenStringFilter #940

marchof opened this issue Sep 20, 2019 · 5 comments · Fixed by #942
Assignees
Labels
Milestone

Comments

@marchof
Copy link
Member

marchof commented Sep 20, 2019

Steps to reproduce

JaCoCo version: master (commit 8b586a9)
Operating system: OSX
Tool integration: CLI

Run

java -jar jacococli.jar report --classfiles NPE.zip

on the attached class file.

Expected behaviour

Command terminates without exception.

Actual behaviour

Exception in thread "main" java.io.IOException: Error while analyzing NPE.zip@$value$ReadableByteChannel.class.
	at org.jacoco.core.analysis.Analyzer.analyzerError(Analyzer.java:161)
	at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:133)
	at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:156)
	at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:192)
	at org.jacoco.core.analysis.Analyzer.analyzeZip(Analyzer.java:264)
	at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:195)
	at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:225)
	at org.jacoco.cli.internal.commands.Report.analyze(Report.java:109)
	at org.jacoco.cli.internal.commands.Report.execute(Report.java:83)
	at org.jacoco.cli.internal.Main.execute(Main.java:89)
	at org.jacoco.cli.internal.Main.main(Main.java:104)
Caused by: java.lang.NullPointerException
	at org.jacoco.core.internal.analysis.MethodCoverageCalculator.ignore(MethodCoverageCalculator.java:159)
	at org.jacoco.core.internal.analysis.filter.KotlinWhenStringFilter$Matcher.match(KotlinWhenStringFilter.java:98)
	at org.jacoco.core.internal.analysis.filter.KotlinWhenStringFilter.filter(KotlinWhenStringFilter.java:37)
	at org.jacoco.core.internal.analysis.filter.Filters.filter(Filters.java:57)
	at org.jacoco.core.internal.analysis.ClassAnalyzer.addMethodCoverage(ClassAnalyzer.java:118)
	at org.jacoco.core.internal.analysis.ClassAnalyzer.access$2(ClassAnalyzer.java:113)
	at org.jacoco.core.internal.analysis.ClassAnalyzer$1.accept(ClassAnalyzer.java:107)
	at org.jacoco.core.internal.flow.ClassProbesAdapter$2.visitEnd(ClassProbesAdapter.java:89)
	at org.objectweb.asm.ClassReader.readMethod(ClassReader.java:1287)
	at org.objectweb.asm.ClassReader.accept(ClassReader.java:688)
	at org.objectweb.asm.ClassReader.accept(ClassReader.java:400)
	at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:115)
	at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:131)
	... 9 more

NPE.zip

@marchof
Copy link
Member Author

marchof commented Sep 20, 2019

I run into this while trying to reproduce an issue reported to our mailing list: https://groups.google.com/forum/?fromgroups=#!topic/jacoco/cx9sTR3KMr4

@Godin Godin self-assigned this Sep 20, 2019
@marchof
Copy link
Member Author

marchof commented Sep 20, 2019

The NPE happens because KotlinWhenStringFilter calls

IFilterOutput.ignore(AbstractInsnNode fromInclusive, AbstractInsnNode toInclusive)

where toInclusive is before fromInclusive. In this case MethodCoverageCalculator iterates over the end of the method.

@Godin Godin added this to Candidates in Current work items via automation Sep 20, 2019
@Godin
Copy link
Member

Godin commented Sep 20, 2019

KotlinWhenStringFilter calls IFilter.output where toInclusive is before fromInclusive

this happens for the following weird bytecode

         0: aload_1
         1: invokevirtual #121                // Method java/lang/String.hashCode:()I
         4: lookupswitch  { // 0
                 default: 16
            }
        16:

because following loop is not executed

and hence cursor doesn't move

final AbstractInsnNode s = cursor;
...
for (int i = 0; i < hashCodes; i++) {
  ...
}
output.ignore(s.getNext(), cursor);

@Godin
Copy link
Member

Godin commented Sep 20, 2019

Analysis of class file compiled by javac 11.0.3 from the following weird Java code causes the same NPE:

class Example {
	private static void example(String s) {
		switch (s.hashCode()) {
			default:
				break;
		}
	}
}

@Godin Godin changed the title NPE in KotlinWhenStringFilter NPE caused by KotlinWhenStringFilter Sep 21, 2019
@Godin Godin changed the title NPE caused by KotlinWhenStringFilter NPE in KotlinWhenStringFilter Sep 21, 2019
@Godin
Copy link
Member

Godin commented Sep 21, 2019

And we can have similar NPE in StringSwitchEcjFilter - #944

Current work items automation moved this from Candidates to Done Sep 22, 2019
@Godin Godin added this to the 0.8.5 milestone Oct 11, 2019
@jacoco jacoco locked as resolved and limited conversation to collaborators Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Development

Successfully merging a pull request may close this issue.

2 participants