Skip to content

Commit 9dfc200

Browse files
committed
INT31-C: Exclude stdbool.h
Conversions to bool should be permitted because they are not "lossy".
1 parent b51dc68 commit 9dfc200

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ where
8282
c.getType().getUnspecifiedType() instanceof UnsignedCharType and
8383
lowerBound(preConversionExpr) >= typeLowerBound(any(SignedCharType s)) and
8484
upperBound(preConversionExpr) <= typeUpperBound(any(UnsignedCharType s))
85-
)
85+
) and
86+
not c.getCastedToType() instanceof BoolType
8687
select c,
8788
"Conversion from " + c.getPreConversionExpr().getType() + " to " + c.getCastedToType() +
8889
" may cause data loss."

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <limits.h>
2+
#include <stdbool.h>
23
#include <stddef.h>
34
#include <stdio.h>
45
#include <string.h>
5-
66
void test_unsigned_to_signed(unsigned int x) {
77
(signed int)x; // NON_COMPLIANT - not larger enough to represent all
88
}
@@ -105,4 +105,8 @@ void test_funcs(int *a, size_t n) {
105105
// not supported in our stdlib, or in any of the compilers
106106
// memset_s(a, rn, 4096, n); // NON_COMPLIANT
107107
// memset_s(a, rn, 0, n); // COMPLIANT
108+
}
109+
110+
void test_bool(signed int s) {
111+
(bool)s; // COMPLIANT
108112
}

0 commit comments

Comments
 (0)