From b94001fe463a081116aada2e09ae32413622b0de Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Tue, 13 Oct 2026 07:37:32 +0200 Subject: [PATCH] Fix packbits for unsigned-char platforms --- src/imageformats/packbits_p.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/imageformats/packbits_p.h b/src/imageformats/packbits_p.h index 63a1b55..6dba9f2 100644 --- a/src/imageformats/packbits_p.h +++ b/src/imageformats/packbits_p.h @@ -66,28 +66,31 @@ inline qint64 packbitsDecompress(QIODevice *input, char *output, qint64 olen, bo { qint64 j = 0; for (qint64 rr = 0, available = olen; j < olen; available = olen - j) { - char n; + char cn; + signed char n; // check the output buffer space for the next run if (available < 129) { - if (input->peek(&n, 1) != 1) { // end of data (or error) + if (input->peek(&cn, 1) != 1) { // end of data (or error) break; } - if (static_cast(n) != -128 || allowN128) - if ((static_cast(n) >= 0 ? qint64(n) + 1 : qint64(1 - n)) > available) + n = static_cast(cn); + if (n != -128 || allowN128) + if ((n >= 0 ? qint64(n) + 1 : qint64(1 - n)) > available) break; } // decompress - if (input->read(&n, 1) != 1) { // end of data (or error) + if (input->read(&cn, 1) != 1) { // end of data (or error) break; } + n = static_cast(cn); - if (static_cast(n) == -128 && !allowN128) { + if (n == -128 && !allowN128) { continue; } - if (static_cast(n) >= 0) { + if (n >= 0) { rr = input->read(output + j, qint64(n) + 1); if (rr == -1) { return -1; @@ -98,7 +101,7 @@ inline qint64 packbitsDecompress(QIODevice *input, char *output, qint64 olen, bo if (input->read(&b, 1) != 1) { break; } - rr = qint64(1 - static_cast(n)); + rr = qint64(1 - n); std::memset(output + j, b, size_t(rr)); }