From 5d7e221bbf9e27e90448243909abc76d81733381 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 29 Mar 2019 12:49:47 -0700 Subject: QRandomGenerator: assert that bounded() calls have correct arguments Otherwise, the math will fail badly. Documentation improved to reflect reality. Change-Id: I9e3d261ad9bf41cfb2b6fffd159085cd38e3c388 Reviewed-by: Thiago Macieira --- src/corelib/global/qrandom.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/corelib/global/qrandom.h') diff --git a/src/corelib/global/qrandom.h b/src/corelib/global/qrandom.h index 46d3e0e152..260fc1501a 100644 --- a/src/corelib/global/qrandom.h +++ b/src/corelib/global/qrandom.h @@ -122,14 +122,16 @@ public: return quint32(value); } - int bounded(int highest) + quint32 bounded(quint32 lowest, quint32 highest) { - return int(bounded(quint32(highest))); + Q_ASSERT(highest > lowest); + return bounded(highest - lowest) + lowest; } - quint32 bounded(quint32 lowest, quint32 highest) + int bounded(int highest) { - return bounded(highest - lowest) + lowest; + Q_ASSERT(highest > 0); + return int(bounded(0U, quint32(highest))); } int bounded(int lowest, int highest) -- cgit v1.2.3