Skip to content

Commit 9afa81e

Browse files
committed
INT31-C: Improve error message to include range
1 parent 7f672bf commit 9afa81e

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ predicate withinIntegralRange(IntegralType typ, float value) {
6262

6363
from
6464
IntegerConversion c, Expr preConversionExpr, Type castedToType, Type castedFromType,
65-
IntegralType unspecifiedCastedFromType, string typeFromMessage
65+
IntegralType unspecifiedCastedFromType, string typeFromMessage, float preConversionLowerBound,
66+
float preConversionUpperBound, float typeLowerBound, float typeUpperBound
6667
where
6768
not isExcluded(c, IntegerOverflowPackage::integerConversionCausesDataLossQuery()) and
6869
preConversionExpr = c.getPreConversionExpr() and
@@ -71,10 +72,16 @@ where
7172
unspecifiedCastedFromType = castedFromType.getUnspecifiedType() and
7273
// Casting to an integral type
7374
castedToType = c.getCastedToType() and
75+
// Get the upper/lower bound of the pre-conversion expression
76+
preConversionLowerBound = lowerBound(preConversionExpr) and
77+
preConversionUpperBound = upperBound(preConversionExpr) and
78+
// Get the upper/lower bound of the target type
79+
typeLowerBound = typeLowerBound(castedToType) and
80+
typeUpperBound = typeUpperBound(castedToType) and
7481
// Where the result is not within the range of the target type
7582
(
76-
not withinIntegralRange(castedToType, lowerBound(preConversionExpr)) or
77-
not withinIntegralRange(castedToType, upperBound(preConversionExpr))
83+
not withinIntegralRange(castedToType, preConversionLowerBound) or
84+
not withinIntegralRange(castedToType, preConversionUpperBound)
7885
) and
7986
// A conversion of `-1` to `time_t` is permitted by the standard
8087
not (
@@ -93,4 +100,7 @@ where
93100
if castedFromType = unspecifiedCastedFromType
94101
then typeFromMessage = castedFromType.toString()
95102
else typeFromMessage = castedFromType + " (" + unspecifiedCastedFromType + ")"
96-
select c, "Conversion from " + typeFromMessage + " to " + castedToType + " may cause data loss."
103+
select c,
104+
"Conversion from " + typeFromMessage + " to " + castedToType +
105+
" may cause data loss (casting from range " + preConversionLowerBound + "..." +
106+
preConversionUpperBound + " to range " + typeLowerBound + "..." + typeUpperBound + ")."
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
| test.c:7:3:7:15 | (signed int)... | Conversion from unsigned int to signed int may cause data loss. |
2-
| test.c:17:3:17:17 | (unsigned int)... | Conversion from signed int to unsigned int may cause data loss. |
3-
| test.c:34:3:34:17 | (signed short)... | Conversion from signed int to signed short may cause data loss. |
4-
| test.c:51:3:51:19 | (unsigned short)... | Conversion from unsigned int to unsigned short may cause data loss. |
5-
| test.c:89:3:89:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss. |
6-
| test.c:92:3:92:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss. |
7-
| test.c:93:3:93:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss. |
8-
| test.c:97:9:97:12 | 4096 | Conversion from int to unsigned char may cause data loss. |
9-
| test.c:99:10:99:13 | 4096 | Conversion from int to unsigned char may cause data loss. |
10-
| test.c:101:13:101:16 | 4096 | Conversion from int to unsigned char may cause data loss. |
11-
| test.c:103:13:103:16 | 4096 | Conversion from int to unsigned char may cause data loss. |
1+
| test.c:7:3:7:15 | (signed int)... | Conversion from unsigned int to signed int may cause data loss (casting from range 0...4294967295 to range -2147483648...2147483647). |
2+
| test.c:17:3:17:17 | (unsigned int)... | Conversion from signed int to unsigned int may cause data loss (casting from range -2147483648...2147483647 to range 0...4294967295). |
3+
| test.c:34:3:34:17 | (signed short)... | Conversion from signed int to signed short may cause data loss (casting from range -2147483648...2147483647 to range -32768...32767). |
4+
| test.c:51:3:51:19 | (unsigned short)... | Conversion from unsigned int to unsigned short may cause data loss (casting from range 0...4294967295 to range 0...65535). |
5+
| test.c:89:3:89:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss (casting from range 100000...100000 to range 0...255). |
6+
| test.c:92:3:92:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss (casting from range -129...-129 to range 0...255). |
7+
| test.c:93:3:93:19 | (unsigned char)... | Conversion from signed int to unsigned char may cause data loss (casting from range 256...256 to range 0...255). |
8+
| test.c:97:9:97:12 | 4096 | Conversion from int to unsigned char may cause data loss (casting from range 4096...4096 to range 0...255). |
9+
| test.c:99:10:99:13 | 4096 | Conversion from int to unsigned char may cause data loss (casting from range 4096...4096 to range 0...255). |
10+
| test.c:101:13:101:16 | 4096 | Conversion from int to unsigned char may cause data loss (casting from range 4096...4096 to range 0...255). |
11+
| test.c:103:13:103:16 | 4096 | Conversion from int to unsigned char may cause data loss (casting from range 4096...4096 to range 0...255). |

0 commit comments

Comments
 (0)