Skip to content

Commit 7f672bf

Browse files
committed
INT31-C: Add better message for typedefs
1 parent 9dfc200 commit 7f672bf

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

c/cert/src/rules/INT31-C/IntegerConversionCausesDataLoss.ql

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6466
where
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 (
@@ -83,7 +88,9 @@ where
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."

c/cert/test/rules/INT31-C/IntegerConversionCausesDataLoss.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
WARNING: Unused predicate test (/home/luke/git/codeql-coding-standards/c/cert/src/rules/INT31-C/IntegerConversionCausesDataLoss.ql:63,11-15)
2-
WARNING: Unused predicate withinIntegralRange (/home/luke/git/codeql-coding-standards/c/cert/src/rules/INT31-C/IntegerConversionCausesDataLoss.ql:65,11-30)
31
| test.c:7:3:7:15 | (signed int)... | Conversion from unsigned int to signed int may cause data loss. |
42
| test.c:17:3:17:17 | (unsigned int)... | Conversion from signed int to unsigned int may cause data loss. |
53
| test.c:34:3:34:17 | (signed short)... | Conversion from signed int to signed short may cause data loss. |

0 commit comments

Comments
 (0)