Unable to run string to primitive conversion check (S2130) due to ArrayIndexOutOfBoundsException

java

(Philippe Cadé) #1

Dear all,

We’re running into an error while analyzing one of our files, see stacktrace below.

I can privately share the file in question if needed, though it has many dependencies.

  • SonarQube 6.7.1 (build 35068)
  • SonarJava 5.12 (build 17701)

Thanks for your help
Philippe

/jenkins/xxx/job_amos_sonar.xml:110: org.sonar.java.AnalysisException: SonarQube is unable to analyze file : 'xxx/PartRequestHome.java'
	at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:99)
	at org.sonar.java.ast.JavaAstScanner.scan(JavaAstScanner.java:67)
	at org.sonar.java.JavaSquid.scanSources(JavaSquid.java:114)
	at org.sonar.java.JavaSquid.scan(JavaSquid.java:108)
	at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:88)
	at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
	at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:88)
	at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:82)
	at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:68)
	at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:88)
	at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:180)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:288)
	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:283)
	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:281)
	at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:261)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
	at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
	at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
	at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:111)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:62)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
	at com.sun.proxy.$Proxy0.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:233)
	at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
	at org.sonarsource.scanner.ant.SonarQubeTask.launchAnalysis(SonarQubeTask.java:99)
	at org.sonarsource.scanner.ant.SonarQubeTask.execute(SonarQubeTask.java:81)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
	at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
	at org.apache.tools.ant.Task.perform(Task.java:348)
	at org.apache.tools.ant.Target.execute(Target.java:435)
	at org.apache.tools.ant.Target.performTasks(Target.java:456)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
	at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
	at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
	at org.apache.tools.ant.Main.runBuild(Main.java:857)
	at org.apache.tools.ant.Main.startAnt(Main.java:236)
	at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287)
	at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
	at com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:60)
	at org.sonar.java.ast.parser.ListTreeImpl.get(ListTreeImpl.java:186)
	at org.sonar.java.ast.parser.ListTreeImpl.get(ListTreeImpl.java:38)
	at org.sonar.java.checks.StringToPrimitiveConversionCheck$PrimitiveCheck.isStringBasedConstructor(StringToPrimitiveConversionCheck.java:160)
	at org.sonar.java.checks.StringToPrimitiveConversionCheck$PrimitiveCheck.isBadlyInstantiated(StringToPrimitiveConversionCheck.java:133)
	at org.sonar.java.checks.StringToPrimitiveConversionCheck$PrimitiveCheck.checkInstantiation(StringToPrimitiveConversionCheck.java:125)
	at org.sonar.java.checks.StringToPrimitiveConversionCheck$PrimitiveCheck.access$000(StringToPrimitiveConversionCheck.java:94)
	at org.sonar.java.checks.StringToPrimitiveConversionCheck.visitNode(StringToPrimitiveConversionCheck.java:60)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.lambda$visit$7(VisitorsBridge.java:295)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:298)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302)
	at org.sonar.java.model.VisitorsBridge$ScannerRunner.run(VisitorsBridge.java:271)
	at org.sonar.java.model.VisitorsBridge.visitFile(VisitorsBridge.java:141)
	at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:90)
	... 52 more

(Michael Gumowski) #2

Hey Phillippe,

Thanks for the feedback, it looks like we forgot to cover a case in the rule. However, I’m not able to reproduce it locally based only on the log.

Could you provide a small code snippet reproducing the issue and making the analysis fail (ideally self-contained)? You should be able to find it in the class file making the fail. Probably a call to a primitive wrapper constructor, or an invocation of valueOf(). If not, you can maybe send the file privately to me.

Cheers,
Michael


(Philippe Cadé) #3

Hi Michael,

The following code can reproduce the issue. Note: Java 11 required.

import java.util.ArrayList;
import java.util.HashSet;

public class S2130Fail {

	void foo() {
		var pn = new HashSet<>();
		bar(new ArrayList<>(pn));
	}
	
	void bar(Object o) {
		
	}
}

Kind regards,
Philippe


(Michael Gumowski) #4

Okay, thanks a lot!

After investigation, what I initially expected to be a missing case in the rule is in fact a bit more complicated… And finally related to type inference badly reacting to diamond operator (<>). Ticket created to handle it: SONARJAVA-3090

Once fixed on semantic side, it should fix the issue with the rule.

Thanks again,
Michael