diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-12-20 09:14:36 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-12-22 11:16:02 +0000 |
commit | 115b1eab86d0395dc29f90d271bfdd29d425442e (patch) | |
tree | 0cc3e9699879b7b411690d46efcad7134ef71f07 /src/gui/image | |
parent | 85b15b101c70b962fd0030c26159ef7b1b50a1f4 (diff) |
QXBMHandler: use QtMiscUtils, not <ctype.h>
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/gui/image')
-rw-r--r-- | src/gui/image/qxbmhandler.cpp | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index e2248df6d7..5e81370887 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -10,9 +10,9 @@ #include <qiodevice.h> #include <qloggingcategory.h> #include <qvariant.h> +#include <private/qtools_p.h> #include <stdio.h> -#include <ctype.h> 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); |