11/**
2- * @name Check for negative length
2+ * @name Redundant check for negative value
33 * @description Checking whether the result of 'len' or 'cap' is negative is pointless,
4- * since these functions always returns a non-negative number.
4+ * since these functions always returns a non-negative number. It is also
5+ * pointless checking if an unsigned integer is negative, as it can only
6+ * hold non-negative values.
57 * @kind problem
68 * @problem.severity warning
79 * @precision very-high
1113
1214import go
1315
14- from ComparisonExpr cmp , BuiltinFunction len , int ub , string r
16+ from ComparisonExpr cmp , DataFlow :: Node op , int ub , string d , string r
1517where
16- ( len = Builtin:: len ( ) or len = Builtin:: cap ( ) ) and
18+ (
19+ exists ( BuiltinFunction bf | bf = Builtin:: len ( ) or bf = Builtin:: cap ( ) |
20+ op = bf .getACall ( ) and d = "'" + bf .getName ( ) + "'"
21+ )
22+ or
23+ op .getType ( ) .getUnderlyingType ( ) instanceof UnsignedIntegerType and
24+ d = "This unsigned value"
25+ ) and
1726 (
1827 exists ( RelationalComparisonExpr rel | rel = cmp |
19- rel .getLesserOperand ( ) = len . getACall ( ) .asExpr ( ) and
28+ rel .getLesserOperand ( ) = op .asExpr ( ) and
2029 rel .getGreaterOperand ( ) .getIntValue ( ) = ub and
2130 (
2231 ub < 0
@@ -27,10 +36,10 @@ where
2736 )
2837 or
2938 exists ( EqualityTestExpr eq | eq = cmp |
30- eq .getAnOperand ( ) = len . getACall ( ) .asExpr ( ) and
39+ eq .getAnOperand ( ) = op .asExpr ( ) and
3140 eq .getAnOperand ( ) .getIntValue ( ) = ub and
3241 ub < 0 and
3342 r = "equal"
3443 )
3544 )
36- select cmp , "'" + len . getName ( ) + "' is always non-negative, and hence cannot " + r + " " + ub + "."
45+ select cmp , d + " is always non-negative, and hence cannot " + r + " " + ub + "."
0 commit comments