[Development] Fixing QRect::width() / height()

Marc Mutz marc.mutz at kdab.com
Tue Mar 15 14:07:29 CET 2016


Hi,

No, this is not about the +1.

QRect is internally represented as QPoint topLeft, bottomRight, which means it 
can hold rectangles for which width() and height(), returning ints, may 
overflow, e.g.

   QRect{{INT_MIN, INT_MIN}, QPoint{0, 0}}
   QRect{{INT_MIN, INT_MIN}, QPoint{INT_MAX, INT_MAX}}

While these may seem like pathological cases that never occur in practice, the 
auto-test checks such rectangles, and if it didn't the next attacker would.

It is therefore important to provide a fix width() and height().

There are two options I can see:

   qint64 width64() const;
   unsigned int width??() const;

The first returns the result as a 64-bit quantitiy, preserving the negative 
widths of unnormalised rectangles, and never overflowing, but possibly 
penalises 32-bit platforms without 64-bit register support.

The second still overflows for left() == INT_MIN, right() == INT_MAX, because 
of the +1 (don't highjack this thread, you have been warned! :), would only 
work on normalized rects, but doesn't require a 64-bit quantitiy.

Opinions on which one to choose? One? The other? Both?

Thanks,
Marc

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt Experts



More information about the Development mailing list