界: Code Quality
代码质量不佳会导致不可预测的行为。对于用户来说,通常表现为可用性差。对于攻击者来说,提供了以意外方式对系统施加压力的机会。
Code Correctness: String Comparison of Float
Abstract
将浮点值与
String
对象进行对比非常不可靠,不应该这样做。Explanation
如果要将浮点值与
示例 1:以下示例将浮点值与
String
对象进行比较,则必须先将该值更改为 String
对象,通常是通过如 Double.toString()
等函数来实现。在将浮点变量转换为 String
对象后,其可能为 "NaN"、"Infinity" 或 "-Infinity",或者带有几位小数点(其中包含 0),或者可能包含指数字段,具体取决于浮点变量的类型和数值。如果转换为十六进制字符串,则其形式也会有很大差异。示例 1:以下示例将浮点值与
String
进行了比较。
...
int initialNum = 1;
...
String resultString = Double.valueOf(initialNum/10000.0).toString();
if (s.equals("0.0001")){
//do something
...
}
...
References
[1] NUM11-J. Do not compare or inspect the string representation of floating-point values CERT
[2] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.dataflow.java.code_correctness_string_comparison_of_float