-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
6.0.0
-
None
Unexpected Result When QLCDNumber::display is Set to INT_MIN
I have the following example:
// Testing INT_MIN handling QLCDNumber *lcd = new QLCDNumber(12, this); lcd->setMode(QLCDNumber::Dec); lcd->display(INT_MIN); // Should correctly display -2147483648 // Testing other boundary values lcd->display(INT_MAX); // Should correctly display 2147483647 lcd->display(-1); // Should correctly display -1 lcd->display(0); // Should correctly display 0
The result shows that when lcd->display(INT_MIN) is called, the displayed result is not the expected -2147483648. On Linux, I observed the result as --2147483648.
The cause is an integer overflow issue in the QLCDNumber::int2string function, as shown below:
bool negative; if (num < 0) { negative = true; num = -num; // ← Integer overflow bug exists here! } else { negative = false; }
When the value of num is INT_MIN, executing num = -num causes integer overflow because:
1. The absolute value of INT_MIN is 2147483648
2. But the maximum positive value of int type (INT_MAX) is 2147483647
3. Therefore, -INT_MIN cannot be represented by the int type, resulting in overflow
For Gerrit Dashboard: QTBUG-139635 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
671834,9 | QLCDNumber: handle integer overflow for INT_MIN in QLCDNumber | dev | qt/qtbase | Status: NEW | 0 | 0 |