From 836e0a53bb96e29c557a1f20a9d71195303633a9 Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Wed, 4 Mar 2026 08:15:48 +0100 Subject: [PATCH] JP2: fix possible Undefined-shift --- src/imageformats/jp2.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/imageformats/jp2.cpp b/src/imageformats/jp2.cpp index 1f7ccfa..05e6524 100644 --- a/src/imageformats/jp2.cpp +++ b/src/imageformats/jp2.cpp @@ -269,10 +269,11 @@ public: // discriminate between int and float (avoid complicating things by creating classes with template specializations) if (std::numeric_limits::is_integer) { - auto divisor = 1; - if (jc.prec > sizeof(T) * 8) { + auto divisor = 1ull; + auto prec = std::min(size_t(jc.prec), sizeof(*jc.data) * 8); + if (prec > sizeof(T) * 8 && prec < 64) { // convert to the wanted precision (e.g. 16-bit -> 8-bit: divisor = 65535 / 255 = 257) - divisor = std::max(1, int(((1ll << jc.prec) - 1) / ((1ll << (sizeof(T) * 8)) - 1))); + divisor = std::max(1ull, (((1ull << prec) - 1) / ((1ull << (sizeof(T) * 8)) - 1))); } for (qint32 y = 0, h = img->height(); y < h; ++y) { auto ptr = reinterpret_cast(img->scanLine(y));