Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-139635

Unexpected Result When QLCDNumber::display is Set to INT_MIN

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 6.0.0
    • None
    • Linux/X11

      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

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            xionglinlin1 xiong linlin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change