diff --git a/src/plugins/imageformats/icns/qicnshandler.cpp b/src/plugins/imageformats/icns/qicnshandler.cpp
index 02086cbf26c..207f7cd8393 100644
--- a/src/plugins/imageformats/icns/qicnshandler.cpp
+++ b/src/plugins/imageformats/icns/qicnshandler.cpp
@@ -323,8 +323,11 @@ static inline bool isPowOf2OrDividesBy16(quint32 u, qreal r)
 
 static inline bool isBlockHeaderValid(const ICNSBlockHeader &header, quint64 bound = 0)
 {
-    return header.ostype != 0 && (bound == 0
-                || qBound(quint64(ICNSBlockHeaderSize), quint64(header.length), bound) == header.length);
+    return header.ostype != 0 &&
+        (bound == 0 ||
+            // qBound can be used but requires checking the limits first
+            // this requires less operations
+            (ICNSBlockHeaderSize <= header.length && header.length <= bound));
 }
 
 static inline bool isIconCompressed(const ICNSEntry &icon)
@@ -869,7 +872,7 @@ bool QICNSHandler::scanDevice()
             return false;
 
         const qint64 blockDataOffset = device()->pos();
-        if (!isBlockHeaderValid(blockHeader, ICNSBlockHeaderSize + filelength - blockDataOffset)) {
+        if (!isBlockHeaderValid(blockHeader, ICNSBlockHeaderSize - blockDataOffset + filelength)) {
             qWarning("QICNSHandler::scanDevice(): Failed, bad header at pos %s. OSType \"%s\", length %u",
                      QByteArray::number(blockDataOffset).constData(),
                      nameFromOSType(blockHeader.ostype).constData(), blockHeader.length);


