C++学习之Numeric overflow due to incorrect type casting

由于不正确的类型转换导致的数字溢出,可能会导致错误的结果。这种错误通过debug并不好追踪。

示例1:


typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
uint64_t foo(uint32_t x, uint32_t y){
    uint64_t z;
    z = (uint64_t)( x * y );
    z = (uint64_t)( y * 100000U);
    return z;
}

上面的代码中,开发者希望两数相乘的结果占 64 字节,尽管 x 和 y 都是32位的。但如果 x * y 所得结果大于 32 位,再进行强制类型转换就会出现溢出问题。上面的 5/6 两行 code 都会产生 klockwork issue。


fixed code example 1


typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
uint64_t foo(uint32_t x, uint32_t y){
    uint64_t z;
    z = (uint64_t)( x ) * y;
    z = (uint64_t)( y ) * 100000U;
    return z;
}

第 5 行中先将 x 强制转换为 64 位,然后在与 y 相乘时,y 会先自动转换为 64 位的数字,之后两者相乘即可得一个 64 位数字 z 。第 6 行同理。

示例 2:


typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
uint32_t get_val();
uint64_t foo(uint32_t x, uint32_t y){
    uint64_t z;
    z = (uint64_t)( y * get_val() );
    return z;
}

fixed code example 2:


typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
uint32_t get_val();
uint64_t foo(uint32_t x, uint32_t y){
    uint64_t z;
    z = (uint64_t)( y ) * get_val();
    return z;
}
### Reasons In Qt, when comparing values of data types with limited ranges, such as integers (e.g., `int`, `short`, `char`), overflow or underflow can occur. For example, if you are comparing two `int` values, and the calculation or assignment causes the value to exceed the maximum or minimum representable value of the `int` type, the result will wrap around. Suppose you have two `int` variables `a` and `b`, and you are comparing them. If an operation on `a` causes it to overflow and become a value that seems to satisfy the comparison condition with `b`, the comparison result will always be `true`. ```cpp #include <QDebug> int main() { int a = std::numeric_limits<int>::max(); int b = 10; a = a + 1; // Overflow occurs if (a > b) { qDebug() << "The comparison is always true due to overflow."; } return 0; } ``` ### Solutions 1. **Use Larger Data Types**: If you anticipate that the values might exceed the range of a particular data type, use a larger data type. For example, if `int` is not sufficient, you can use `long long` in C++. ```cpp #include <QDebug> int main() { long long a = std::numeric_limits<long long>::max(); long long b = 10; if (a + 1 > b) { qDebug() << "Using a larger data type to avoid overflow."; } return 0; } ``` 2. **Check for Overflow**: Before performing operations that might cause overflow, check if the operation is valid. You can use conditional statements to ensure that the result of an operation will not exceed the range of the data type. ```cpp #include <QDebug> #include <limits> int main() { int a = std::numeric_limits<int>::max() - 10; int b = 10; if (a <= std::numeric_limits<int>::max() - b) { a = a + b; if (a > b) { qDebug() << "Checked for overflow before operation."; } } return 0; } ``` 3. **Use Qt's Safe Integer Functions**: Qt provides some functions to perform safe integer arithmetic operations. These functions will throw an exception if an overflow occurs. ```cpp #include <QDebug> #include <QtCore/QSafeInteger> int main() { int a = std::numeric_limits<int>::max() - 10; int b = 10; try { int result = qSafeSum(a, b); if (result > b) { qDebug() << "Using Qt's safe integer function."; } } catch (const QSafeIntegerException& e) { qDebug() << "Overflow detected: " << e.what(); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值