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

qRound does not handle overflow

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2: Important
    • 6.10.0 FF
    • 6.10
    • Core: Other
    • None
    • 1145e1709 (dev), c5608c675 (dev)

    Description

      qRound(double d) is documented to "Rounds d to the nearest integer.". If d is outside the range of integers (overflow), it should therefore clamp the result to INT_MAX (or INT_MIN).

      Instead, it triggers UB, as it just converts the double to integer (after some adjustements), without checking whether it's in bounds or not. This is https://eel.is/c++draft/conv.fpint#1.sentence-3

      This has a cascading effect, for instance the geometry classes use qRound internally, causing for instance a QRectF -> QRect conversion to also trigger UB. There's at least an autotest (tst_QRect::comparison(WithQREAL_MIN)) where this happens.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            peppe Giuseppe D'Angelo
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes