From 115b1eab86d0395dc29f90d271bfdd29d425442e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 20 Dec 2022 09:14:36 +0100 Subject: [PATCH] QXBMHandler: use QtMiscUtils, not MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The latter are locale-dependent, and while that doesn't produce wrong results here¹, it means they need to be out-of-line, whereas QtMiscUtils functions are inline constexpr. ¹ isdigit could give a false positive on non-US-ASCII-digits, I suppose. As a drive-by, rely on the QtMiscUtils::fromHex() returning a negative value when a non-hex character was encountered, to avoid the extra isxdigit() checks. This property is preserved through the bit manipulations: ORing together negative values yields a negative number iff either or both operands were negative (in two's complement, which Qt requires). The caller also checks the array bounds before calling, so the isxdigit() calls didn't act as an implicit bounds check (returning false if they encounter '\0'), either. Task-number: QTBUG-109235 Pick-to: 6.5 Change-Id: I4cd734468e223f1047a53afd264d077b28cb5f1d Reviewed-by: Thiago Macieira --- src/gui/image/qxbmhandler.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index e2248df6d7d..5e813708876 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -10,9 +10,9 @@ #include #include #include +#include #include -#include QT_BEGIN_NAMESPACE @@ -24,8 +24,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcImageIo) static inline int hex2byte(char *p) { - return ((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) | - (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10); + return QtMiscUtils::fromHex(p[0]) * 16 | QtMiscUtils::fromHex(p[1]); } static bool read_xbm_header(QIODevice *device, int& w, int& h) @@ -129,9 +128,10 @@ static bool read_xbm_body(QIODevice *device, int w, int h, QImage *outImage) while (y < h) { // for all encoded bytes... if (p && p < (buf + readBytes - 3)) { // p = "0x.." - if (!isxdigit(p[2]) || !isxdigit(p[3])) + const int byte = hex2byte(p + 2); + if (byte < 0) // non-hex char encountered return false; - *b++ = hex2byte(p+2); + *b++ = byte; p += 2; if (++x == w && ++y < h) { b = outImage->scanLine(y);