From 8e90e0805f2981014d3382d8841617b4c56dfc50 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 26 Sep 2012 12:32:52 +0200 Subject: Prevent an overflow warning in assertions. Functions like QByteArray::at() assert the given index: Q_ASSERT(i >= 0 && i < size(); These functions typically get inlined. Now if the index is e.g. size() - 2, then gcc will emit an ugly warning in client code ("assuming signed overflow does not occur when assuming that (X - c) > X is always false"). This can be easily prevented by casting both sides of the second comparison in the assertion to their unsigned type. The explicit comparison to zero is then no longer necessary, since that condition is tested implicitly by the other comparison due to unsigned arithmetic. Change-Id: Ic7244e1fa5da00a47d1fe0ed56fb81c23d444dfe Reviewed-by: hjk Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira --- src/corelib/tools/qbytearray.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/corelib/tools/qbytearray.h') diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 668d672a6b..50e52a1ca7 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -400,9 +400,9 @@ inline int QByteArray::size() const { return d->size; } inline char QByteArray::at(int i) const -{ Q_ASSERT(i >= 0 && i < size()); return d->data()[i]; } +{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; } inline char QByteArray::operator[](int i) const -{ Q_ASSERT(i >= 0 && i < size()); return d->data()[i]; } +{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; } inline char QByteArray::operator[](uint i) const { Q_ASSERT(i < uint(size())); return d->data()[i]; } -- cgit v1.2.3