Skip to content

Commit 59f2c75

Browse files
committed
C++: bool -> int conversions are safe conversions.
1 parent 0f267e0 commit 59f2c75

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticType.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,26 @@ SemType getSemanticType(Specific::Type type) {
250250
Specific::unknownType(type) and result = TSemUnknownType()
251251
}
252252

253+
private class SemNumericOrBooleanType extends SemSizedType {
254+
SemNumericOrBooleanType() {
255+
this instanceof SemNumericType
256+
or
257+
this instanceof SemBooleanType
258+
}
259+
}
260+
253261
/**
254262
* Holds if the conversion from `fromType` to `toType` can never overflow or underflow.
255263
*/
256-
predicate conversionCannotOverflow(SemNumericType fromType, SemNumericType toType) {
264+
predicate conversionCannotOverflow(SemNumericOrBooleanType fromType, SemNumericOrBooleanType toType) {
257265
// Identity cast
258266
fromType = toType
259267
or
260268
// Treat any cast to an FP type as safe. It can lose precision, but not overflow.
261269
toType instanceof SemFloatingPointType and fromType = any(SemNumericType n)
262270
or
271+
fromType instanceof SemBooleanType and toType instanceof SemIntegerType
272+
or
263273
exists(SemIntegerType fromInteger, SemIntegerType toInteger, int fromSize, int toSize |
264274
fromInteger = fromType and
265275
toInteger = toType and

0 commit comments

Comments
 (0)