Skip to content

Commit

Permalink
common: prevent NPE by explict cast of (Double)double in ternary cond…
Browse files Browse the repository at this point in the history
…ition

Motivation:

0a3685f
https://rb.dcache.org/r/13868

Introduced a null check on the value given to Double.isNaN,
but neglected the fact that the second and third values
of the condition did not have the same type; more precisely,
the second value is a primitive and the third an object.
According to https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25.2,
Java infers the type of the expression in this case to be
the primitive, which a null value (third argument) cannot
be unboxed to.

Modification:

Explicitly cast the second argument to (Double), so
the type is inferred as to be Double.

Result:

Another NPE is eliminated:

Target: master
Request: 8.2  (update pull request)
Request: 8.1  (update pull request)
Request: 8.0  (update pull request)
Request: 7.2  (update pull request)
Pull-request:  #6980
Pull-request:  #6981
Pull-request:  #6982
Pull-request:  #6983
Requires-notes: no
Patch: https://rb.dcache.org/r/13868/
Acked-by: Dmitry (this change)
  • Loading branch information
alrossi authored and lemora committed Feb 2, 2023
1 parent 8472767 commit fbf9c31
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions modules/common/src/main/java/org/dcache/util/MathUtils.java
Expand Up @@ -81,7 +81,6 @@ public static long subWithInfinity(long a, long b) {
* @return 0.0 if the value is NaN; otherwise, the value (including null).
*/
public static Double nanToZero(Double value) {
return value != null && Double.isNaN(value) ? 0.0 : value;
return value != null && Double.isNaN(value) ? (Double)0.0 : value;
}

}

0 comments on commit fbf9c31

Please sign in to comment.