@@ -19,7 +19,7 @@ class IntegerConversion extends Expr {
19
19
20
20
IntegerConversion ( ) {
21
21
// This is an explicit cast
22
- castedToType = this .( Cast ) .getActualType ( ) and
22
+ castedToType = this .( Cast ) .getType ( ) . getUnspecifiedType ( ) and
23
23
preConversionExpr = this .( Cast ) .getExpr ( )
24
24
or
25
25
// Functions that internally cast an argument to unsigned char
@@ -60,16 +60,21 @@ predicate withinIntegralRange(IntegralType typ, float value) {
60
60
)
61
61
}
62
62
63
- from IntegerConversion c , Expr preConversionExpr
63
+ from
64
+ IntegerConversion c , Expr preConversionExpr , Type castedToType , Type castedFromType ,
65
+ IntegralType unspecifiedCastedFromType , string typeFromMessage
64
66
where
65
67
not isExcluded ( c , IntegerOverflowPackage:: integerConversionCausesDataLossQuery ( ) ) and
66
68
preConversionExpr = c .getPreConversionExpr ( ) and
69
+ castedFromType = preConversionExpr .getType ( ) and
67
70
// Casting from an integral type
68
- preConversionExpr .getType ( ) .getUnspecifiedType ( ) instanceof IntegralType and
71
+ unspecifiedCastedFromType = castedFromType .getUnspecifiedType ( ) and
72
+ // Casting to an integral type
73
+ castedToType = c .getCastedToType ( ) and
69
74
// Where the result is not within the range of the target type
70
75
(
71
- not withinIntegralRange ( c . getCastedToType ( ) , lowerBound ( preConversionExpr ) ) or
72
- not withinIntegralRange ( c . getCastedToType ( ) , upperBound ( preConversionExpr ) )
76
+ not withinIntegralRange ( castedToType , lowerBound ( preConversionExpr ) ) or
77
+ not withinIntegralRange ( castedToType , upperBound ( preConversionExpr ) )
73
78
) and
74
79
// A conversion of `-1` to `time_t` is permitted by the standard
75
80
not (
83
88
lowerBound ( preConversionExpr ) >= typeLowerBound ( any ( SignedCharType s ) ) and
84
89
upperBound ( preConversionExpr ) <= typeUpperBound ( any ( UnsignedCharType s ) )
85
90
) and
86
- not c .getCastedToType ( ) instanceof BoolType
87
- select c ,
88
- "Conversion from " + c .getPreConversionExpr ( ) .getType ( ) + " to " + c .getCastedToType ( ) +
89
- " may cause data loss."
91
+ not castedToType instanceof BoolType and
92
+ // Create a helpful message
93
+ if castedFromType = unspecifiedCastedFromType
94
+ then typeFromMessage = castedFromType .toString ( )
95
+ else typeFromMessage = castedFromType + " (" + unspecifiedCastedFromType + ")"
96
+ select c , "Conversion from " + typeFromMessage + " to " + castedToType + " may cause data loss."
0 commit comments