ClassCastException in MinMaxRangeCheck

SonarQube EE Version 7.5 (build 20543)
SonarQube Gradle plugin version 2.6

The min/max range checker implemented in SONARJAVA-2725 throws a ClassCastException when used on floats:

java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
	at java.lang.Integer.compareTo(Integer.java:52)
	at org.sonar.java.se.checks.MinMaxRangeCheck.checkRangeInconsistencies(MinMaxRangeCheck.java:203)
	at org.sonar.java.se.checks.MinMaxRangeCheck.handleMinMaxInvocation(MinMaxRangeCheck.java:187)
	at org.sonar.java.se.checks.MinMaxRangeCheck.checkPostStatement(MinMaxRangeCheck.java:155)
	at org.sonar.java.se.CheckerDispatcher.executePost(CheckerDispatcher.java:106)
        (...)

So somehow lowerBound is an Integer and upperBound is a Float here (MinMaxRangeCheck.checkRangeInconsistencies):

      // all the used values are going to be Numbers
      @SuppressWarnings("unchecked")
      int comparedValue = ((Comparable<Number>) lowerBound).compareTo(upperBound);

This is the code being scanned:

  private static final float SCALE_FACTOR_THRESHOLD = 2;
  private float mScaleFactor;
  (...)
  mScaleFactor = Math.max(1, Math.min(mScaleFactor, SCALE_FACTOR_THRESHOLD));

Obviously the 1 there will be automatically widened to a float when calling Math.max, but changing the last bit there to

  mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, SCALE_FACTOR_THRESHOLD));

made the problem go away, so it looks like the 1 is being parsed as an integer.

Hello @e.lislebo, thanks for your feedback.

I managed to reproduce the exception thanks to your reproducer.

I created a ticket to track this issue.

Quentin

Hey,

Glad the reproduce steps were sufficient. Thanks, I’ll follow that issue then.

Cheers,
Even