Commit 6cc08834b for imagemagick.org
commit 6cc08834bfea60d8d607bdca7f1e999377230db7
Author: Cristy <urban-warrior@imagemagick.org>
Date: Mon Nov 16 19:14:30 2026 -0500
add range guard to ScaleQuantumToLong()
diff --git a/MagickCore/quantum-private.h b/MagickCore/quantum-private.h
index 20154d19b..662ef82ab 100644
--- a/MagickCore/quantum-private.h
+++ b/MagickCore/quantum-private.h
@@ -29,8 +29,33 @@
extern "C" {
#endif
+#if !defined(ExponentBias)
+#define ExponentBias (127-15)
+#endif
+#if !defined(ExponentMask)
+#define ExponentMask (0x7c00U)
+#endif
+#if !defined(ExponentShift)
+#define ExponentShift 23
+#endif
+#if !defined(MagickMax)
#define MagickMax(x,y) (((x) > (y)) ? (x) : (y))
+#endif
+#if !defined(MagickMin)
#define MagickMin(x,y) (((x) < (y)) ? (x) : (y))
+#endif
+#if !defined(SignBitShift)
+#define SignBitShift 31
+#endif
+#if !defined(SignificandShift)
+#define SignificandShift 13
+#endif
+#if !defined(SignificandMask)
+#define SignificandMask (0x00000400U)
+#endif
+#if !defined(HalfExponentMax)
+#define HalfExponentMax (0x0000001fU)
+#endif
typedef struct _QuantumState
{
@@ -129,13 +154,6 @@ static inline EndianType GetHostEndian(void)
static inline float HalfToSinglePrecision(const unsigned short half)
{
-#define ExponentBias (127-15)
-#define ExponentMask (0x7c00U)
-#define ExponentShift 23
-#define SignBitShift 31
-#define SignificandShift 13
-#define SignificandMask (0x00000400U)
-
typedef union _SinglePrecision
{
unsigned int
@@ -170,6 +188,10 @@ static inline float HalfToSinglePrecision(const unsigned short half)
value=sign_bit << SignBitShift;
else
{
+ /*
+ Subnormal half: normalize. Unsigned wrap-around of exponent is
+ intentional and is corrected by the exponent++ / bias below.
+ */
while ((significand & SignificandMask) == 0)
{
significand<<=1;
@@ -183,8 +205,11 @@ static inline float HalfToSinglePrecision(const unsigned short half)
}
}
else
- if (exponent == SignBitShift)
+ if (exponent == HalfExponentMax)
{
+ /*
+ Infinity or NaN.
+ */
value=(sign_bit << SignBitShift) | 0x7f800000;
if (significand != 0)
value|=(significand << SignificandShift);
@@ -323,11 +348,11 @@ static inline const unsigned char *PushShortPixel(const EndianType endian,
if (endian == LSBEndian)
{
quantum=(unsigned int) *pixels++;
- quantum|=(unsigned int) (*pixels++ << 8);
+ quantum|=(unsigned int) ((unsigned int) *pixels++ << 8);
*pixel=(unsigned short) (quantum & 0xffff);
return(pixels);
}
- quantum=(unsigned int) (*pixels++ << 8);
+ quantum=(unsigned int) ((unsigned int) *pixels++ << 8);
quantum|=(unsigned int) *pixels++;
*pixel=(unsigned short) (quantum & 0xffff);
return(pixels);
@@ -369,11 +394,9 @@ static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
if (quantum > range)
return(QuantumRange);
#if !defined(MAGICKCORE_HDRI_SUPPORT)
- return((Quantum) ((double) QuantumRange*(quantum*
- MagickSafeReciprocal((double) range))+0.5));
+ return((Quantum) ((double) QuantumRange*((double) quantum*MagickSafeReciprocal((double) range))+0.5));
#else
- return((Quantum) ((double) QuantumRange*((double) quantum*
- MagickSafeReciprocal((double) range))));
+ return((Quantum) ((double) QuantumRange*((double) quantum*MagickSafeReciprocal((double) range))));
#endif
}
@@ -718,6 +741,10 @@ static inline Quantum ScaleMapToQuantum(const MagickRealType value)
static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
{
+ if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+ return(0U);
+ if ((quantum/4294967297.0) >= 4294967295.0)
+ return(4294967295U);
return((unsigned int) (quantum/4294967297.0+0.5));
}
@@ -827,18 +854,19 @@ static inline unsigned short SinglePrecisionToHalf(const double value)
}
if (exponent > 30)
{
- float
- alpha;
-
int
i;
+ volatile float
+ alpha;
+
/*
Float overflow.
*/
- alpha=1.0e10;
+ alpha=1.0e10f;
for (i=0; i < 10; i++)
alpha*=alpha;
+ (void) alpha;
return((unsigned short) (sign_bit | ExponentMask));
}
half=(unsigned short) (sign_bit | ((unsigned int) exponent << 10) |