@@ -19,7 +19,7 @@ class IntegerConversion extends Expr {
1919
2020 IntegerConversion ( ) {
2121 // This is an explicit cast
22- castedToType = this .( Cast ) .getActualType ( ) and
22+ castedToType = this .( Cast ) .getType ( ) . getUnspecifiedType ( ) and
2323 preConversionExpr = this .( Cast ) .getExpr ( )
2424 or
2525 // Functions that internally cast an argument to unsigned char
@@ -60,16 +60,21 @@ predicate withinIntegralRange(IntegralType typ, float value) {
6060 )
6161}
6262
63- from IntegerConversion c , Expr preConversionExpr
63+ from
64+ IntegerConversion c , Expr preConversionExpr , Type castedToType , Type castedFromType ,
65+ IntegralType unspecifiedCastedFromType , string typeFromMessage
6466where
6567 not isExcluded ( c , IntegerOverflowPackage:: integerConversionCausesDataLossQuery ( ) ) and
6668 preConversionExpr = c .getPreConversionExpr ( ) and
69+ castedFromType = preConversionExpr .getType ( ) and
6770 // 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
6974 // Where the result is not within the range of the target type
7075 (
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 ) )
7378 ) and
7479 // A conversion of `-1` to `time_t` is permitted by the standard
7580 not (
8388 lowerBound ( preConversionExpr ) >= typeLowerBound ( any ( SignedCharType s ) ) and
8489 upperBound ( preConversionExpr ) <= typeUpperBound ( any ( UnsignedCharType s ) )
8590 ) 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