Rating: Disallow putting Float.NaN as star/percent values

When creating a Rating instance, invalid values shouldn't be allowed.
However, Float.NaN passes the checks, which leads to creating invalid
ones. This CL fixes the code.

Bug: 173680971
Test: Passed CTS
Change-Id: I629f1568a2efb0c324cc11e94969356c59298db8
This commit is contained in:
Hyundo Moon
2020-11-19 19:56:33 +09:00
parent 6fa73c8e20
commit 76da1da533

View File

@@ -206,11 +206,12 @@ public final class Rating implements Parcelable {
Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating");
return null;
}
if ((starRating < 0.0f) || (starRating > maxRating)) {
if (starRating >= 0.0f && starRating <= maxRating) {
return new Rating(starRatingStyle, starRating);
} else {
Log.e(TAG, "Trying to set out of range star-based rating");
return null;
}
return new Rating(starRatingStyle, starRating);
}
/**
@@ -221,11 +222,11 @@ public final class Rating implements Parcelable {
* @return null if the rating is out of range, a new Rating instance otherwise.
*/
public static Rating newPercentageRating(float percent) {
if ((percent < 0.0f) || (percent > 100.0f)) {
if (percent >= 0.0f && percent <= 100.0f) {
return new Rating(RATING_PERCENTAGE, percent);
} else {
Log.e(TAG, "Invalid percentage-based rating value");
return null;
} else {
return new Rating(RATING_PERCENTAGE, percent);
}
}