|
| 1 | +/** |
| 2 | + * @id c/cert/integer-conversion-causes-data-loss |
| 3 | + * @name INT31-C: Ensure that integer conversions do not result in lost or misinterpreted data |
| 4 | + * @description Converting an integer value to another integer type with a different sign or size |
| 5 | + * can lead to data loss or misinterpretation of the value. |
| 6 | + * @kind problem |
| 7 | + * @precision medium |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/cert/id/int31-c |
| 10 | + * correctness |
| 11 | + * external/cert/obligation/rule |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import codingstandards.c.cert |
| 16 | + |
| 17 | +class IntegerConversion extends Expr { |
| 18 | + private IntegralType castedToType; |
| 19 | + private Expr preConversionExpr; |
| 20 | + |
| 21 | + IntegerConversion() { |
| 22 | + // This is an explicit cast |
| 23 | + castedToType = this.(Cast).getType().getUnspecifiedType() and |
| 24 | + preConversionExpr = this.(Cast).getExpr() |
| 25 | + or |
| 26 | + // Functions that internally cast an argument to unsigned char |
| 27 | + castedToType instanceof UnsignedCharType and |
| 28 | + this = preConversionExpr and |
| 29 | + exists(FunctionCall call, string name | call.getTarget().hasGlobalOrStdName(name) | |
| 30 | + name = ["ungetc", "fputc"] and |
| 31 | + this = call.getArgument(0) |
| 32 | + or |
| 33 | + name = ["memset", "memchr"] and |
| 34 | + this = call.getArgument(1) |
| 35 | + or |
| 36 | + name = "memset_s" and |
| 37 | + this = call.getArgument(2) |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + Expr getPreConversionExpr() { result = preConversionExpr } |
| 42 | + |
| 43 | + Type getCastedToType() { result = castedToType } |
| 44 | +} |
| 45 | + |
| 46 | +bindingset[value] |
| 47 | +predicate withinIntegralRange(IntegralType typ, float value) { |
| 48 | + exists(float lb, float ub, float limit | |
| 49 | + limit = 2.pow(8 * typ.getSize()) and |
| 50 | + ( |
| 51 | + if typ.isUnsigned() |
| 52 | + then ( |
| 53 | + lb = 0 and ub = limit - 1 |
| 54 | + ) else ( |
| 55 | + lb = -limit / 2 and |
| 56 | + ub = (limit / 2) - 1 |
| 57 | + ) |
| 58 | + ) and |
| 59 | + value >= lb and |
| 60 | + value <= ub |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +from |
| 65 | + IntegerConversion c, Expr preConversionExpr, Type castedToType, Type castedFromType, |
| 66 | + IntegralType unspecifiedCastedFromType, string typeFromMessage, float preConversionLowerBound, |
| 67 | + float preConversionUpperBound, float typeLowerBound, float typeUpperBound |
| 68 | +where |
| 69 | + not isExcluded(c, IntegerOverflowPackage::integerConversionCausesDataLossQuery()) and |
| 70 | + preConversionExpr = c.getPreConversionExpr() and |
| 71 | + castedFromType = preConversionExpr.getType() and |
| 72 | + // Casting from an integral type |
| 73 | + unspecifiedCastedFromType = castedFromType.getUnspecifiedType() and |
| 74 | + // Casting to an integral type |
| 75 | + castedToType = c.getCastedToType() and |
| 76 | + // Get the upper/lower bound of the pre-conversion expression |
| 77 | + preConversionLowerBound = lowerBound(preConversionExpr) and |
| 78 | + preConversionUpperBound = upperBound(preConversionExpr) and |
| 79 | + // Get the upper/lower bound of the target type |
| 80 | + typeLowerBound = typeLowerBound(castedToType) and |
| 81 | + typeUpperBound = typeUpperBound(castedToType) and |
| 82 | + // Where the result is not within the range of the target type |
| 83 | + ( |
| 84 | + not withinIntegralRange(castedToType, preConversionLowerBound) or |
| 85 | + not withinIntegralRange(castedToType, preConversionUpperBound) |
| 86 | + ) and |
| 87 | + // A conversion of `-1` to `time_t` is permitted by the standard |
| 88 | + not ( |
| 89 | + c.getType().getUnspecifiedType().hasName("time_t") and |
| 90 | + preConversionExpr.getValue() = "-1" |
| 91 | + ) and |
| 92 | + // Conversion to unsigned char is permitted from the range [SCHAR_MIN..UCHAR_MAX], as those can |
| 93 | + // legitimately represent characters |
| 94 | + not ( |
| 95 | + c.getType().getUnspecifiedType() instanceof UnsignedCharType and |
| 96 | + lowerBound(preConversionExpr) >= typeLowerBound(any(SignedCharType s)) and |
| 97 | + upperBound(preConversionExpr) <= typeUpperBound(any(UnsignedCharType s)) |
| 98 | + ) and |
| 99 | + not castedToType instanceof BoolType and |
| 100 | + // Create a helpful message |
| 101 | + if castedFromType = unspecifiedCastedFromType |
| 102 | + then typeFromMessage = castedFromType.toString() |
| 103 | + else typeFromMessage = castedFromType + " (" + unspecifiedCastedFromType + ")" |
| 104 | +select c, |
| 105 | + "Conversion from " + typeFromMessage + " to " + castedToType + |
| 106 | + " may cause data loss (casting from range " + preConversionLowerBound + "..." + |
| 107 | + preConversionUpperBound + " to range " + typeLowerBound + "..." + typeUpperBound + ")." |
0 commit comments