Issue
I am getting an error called "Avoid numerical data corruption during incompatible mutation" according to CISQ standard and CAST scan tools. Below is the sample code. Any idea how to avoid this??
public int getage() {
return (int) ChronoUnit.YEARS.between(getdob(), LocalDate.now());
}
I tried using JAVA 8 toIntExact of java.Math.toIntExact but I am not sure how different is this.
return toIntExact(ChronoUnit.YEARS.between(LocalDate.of(2014,1, 1), LocalDate.now()));
Solution
As expressed by @AndyTurner in the comment, the problem is in the cast from long to int, which could result in an erroneous calculation.
Possible workarounds are changing the return type to long, which could lead to just moving the cast in a different place, or using toIntExact.
The different with a plain old cast is that, if the long does not actually fit into an int, toIntExact raises a ArithmeticException
Answered By - bracco23
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.