mirror of
https://github.com/github/codeql.git
synced 2026-02-05 17:51:06 +01:00
Merge pull request #12635 from MathiasVP/bool-to-int-is-safe
C++: `bool` -> `int` are safe conversions
This commit is contained in:
@@ -250,16 +250,26 @@ SemType getSemanticType(Specific::Type type) {
|
||||
Specific::unknownType(type) and result = TSemUnknownType()
|
||||
}
|
||||
|
||||
private class SemNumericOrBooleanType extends SemSizedType {
|
||||
SemNumericOrBooleanType() {
|
||||
this instanceof SemNumericType
|
||||
or
|
||||
this instanceof SemBooleanType
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if the conversion from `fromType` to `toType` can never overflow or underflow.
|
||||
*/
|
||||
predicate conversionCannotOverflow(SemNumericType fromType, SemNumericType toType) {
|
||||
predicate conversionCannotOverflow(SemNumericOrBooleanType fromType, SemNumericOrBooleanType toType) {
|
||||
// Identity cast
|
||||
fromType = toType
|
||||
or
|
||||
// Treat any cast to an FP type as safe. It can lose precision, but not overflow.
|
||||
toType instanceof SemFloatingPointType and fromType = any(SemNumericType n)
|
||||
or
|
||||
fromType instanceof SemBooleanType and toType instanceof SemIntegerType
|
||||
or
|
||||
exists(SemIntegerType fromInteger, SemIntegerType toInteger, int fromSize, int toSize |
|
||||
fromInteger = fromType and
|
||||
toInteger = toType and
|
||||
|
||||
Reference in New Issue
Block a user