Commit cb9c9190b for imagemagick.org

commit cb9c9190bc2f6e0cf56448bee4609ddab8f3d940
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Nov 28 15:09:55 2026 -0500

    several failure paths set an exception but left the status at success

diff --git a/coders/jxl.c b/coders/jxl.c
index 066f994f6..8d87ebe56 100644
--- a/coders/jxl.c
+++ b/coders/jxl.c
@@ -278,7 +278,11 @@ static inline void JXLSetFormat(Image *image,JxlPixelFormat *pixel_format,
       if ((format == FloatingPointQuantumFormat) &&
           (pixel_format->data_type == JXL_TYPE_UINT16))
         {
-          pixel_format->data_type=JXL_TYPE_FLOAT16;
+          /*
+            Request 32-bit floats: the pixels are imported as FloatPixel, a
+            half-float buffer would be under-sized and misinterpreted.
+          */
+          pixel_format->data_type=JXL_TYPE_FLOAT;
           (void) SetImageProperty(image,"quantum:format","floating-point",
             exception);
         }
@@ -456,6 +460,7 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
   size_t
     extent = 0,
     image_count = 0,
+    input_length = 0,
     input_size;

   StringInfo
@@ -555,16 +560,26 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
           count;

         remaining=JxlDecoderReleaseInput(jxl_info);
+        if (remaining > input_length)
+          remaining=input_length;
         if (remaining > 0)
-          (void) memmove(pixels,pixels+input_size-remaining,remaining);
+          (void) memmove(pixels,pixels+input_length-remaining,remaining);
         count=ReadBlob(image,input_size-remaining,pixels+remaining);
         if (count <= 0)
           {
+            /*
+              End of file: hand back any unconsumed bytes, then close input.
+            */
+            if (remaining > 0)
+              (void) JxlDecoderSetInput(jxl_info,(const uint8_t *) pixels,
+                remaining);
             JxlDecoderCloseInput(jxl_info);
+            input_length=remaining;
             break;
           }
+        input_length=remaining+(size_t) count;
         jxl_status=JxlDecoderSetInput(jxl_info,(const uint8_t *) pixels,
-          (size_t) count);
+          input_length);
         if (jxl_status == JXL_DEC_SUCCESS)
           jxl_status=JXL_DEC_NEED_MORE_INPUT;
         break;
@@ -583,6 +598,7 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
             (void) ThrowMagickException(exception,GetMagickModule(),
               MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
               image->filename);
+            jxl_status=JXL_DEC_ERROR;
             break;
           }
         JXLInitImage(image,&basic_info);
@@ -732,7 +748,10 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
             */
             AcquireNextImage(image_info,image,exception);
             if (GetNextImageInList(image) == (Image *) NULL)
-              break;
+              {
+                jxl_status=JXL_DEC_ERROR;
+                break;
+              }
             image=SyncNextImageInList(image);
             JXLInitImage(image,&basic_info);
             JXLCopyColorInfo(image,image->previous,exception);
@@ -773,6 +792,7 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
               {
                 (void) ThrowMagickException(exception,GetMagickModule(),
                   CoderError,"MemoryAllocationFailed","`%s'",image->filename);
+                jxl_status=JXL_DEC_ERROR;
                 break;
               }
           }
@@ -794,6 +814,7 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
           {
             (void) ThrowMagickException(exception,GetMagickModule(),
               CorruptImageError,"UnableToReadImageData","`%s'",image->filename);
+            jxl_status=JXL_DEC_ERROR;
             break;
           }
         type=JXLDataTypeToStorageType(image,pixel_format.data_type,exception);
@@ -801,6 +822,7 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
           {
             (void) ThrowMagickException(exception,GetMagickModule(),
               CorruptImageError,"Unsupported data type","`%s'",image->filename);
+            jxl_status=JXL_DEC_ERROR;
             break;
           }
         if ((image->alpha_trait & BlendPixelTrait) != 0)
@@ -847,17 +869,17 @@ static Image *ReadJXLImage(const ImageInfo *image_info,
             /*
               Read Exif profile.
             */
-          if (exif_profile == (StringInfo *) NULL)
-            {
-              exif_profile=AcquireProfileStringInfo("exif",(size_t) size,
-                exception);
-              if (exif_profile != (StringInfo *) NULL)
-                {
-                  box_profile=exif_profile;
-                  jxl_status=JxlDecoderSetBoxBuffer(jxl_info,
-                    GetStringInfoDatum(exif_profile),(size_t) size);
-                }
-            }
+            if (exif_profile == (StringInfo *) NULL)
+              {
+                exif_profile=AcquireProfileStringInfo("exif",(size_t) size,
+                  exception);
+                if (exif_profile != (StringInfo *) NULL)
+                  {
+                    box_profile=exif_profile;
+                    jxl_status=JxlDecoderSetBoxBuffer(jxl_info,
+                      GetStringInfoDatum(exif_profile),(size_t) size);
+                  }
+              }
           }
         if (LocaleNCompare(type,"xml ",sizeof(type)) == 0)
           {
@@ -1266,7 +1288,7 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
     {
       JxlThreadParallelRunnerDestroy(runner);
       JxlEncoderDestroy(jxl_info);
-      return(MagickFalse);
+      ThrowWriterException(CoderError,"UnableToWriteImageData");
     }
   JXLSetFormat(image,&pixel_format,exception);
   if (pixel_format.data_type == JXL_TYPE_FLOAT16)
@@ -1335,7 +1357,8 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
     {
       basic_info.have_animation=JXL_TRUE;
       basic_info.animation.num_loops=(uint32_t) image->iterations;
-      basic_info.animation.tps_numerator=(uint32_t) image->ticks_per_second;
+      basic_info.animation.tps_numerator=(image->ticks_per_second > 0) ?
+        (uint32_t) image->ticks_per_second : 100U;
       basic_info.animation.tps_denominator=1;
       JxlEncoderInitFrameHeader(&frame_header);
     }
@@ -1366,18 +1389,26 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
   option=GetImageOption(image_info,"jxl:effort");
   if (option != (const char *) NULL)
     {
-      if (JxlEncoderFrameSettingsSetOption(frame_settings,
-          JXL_ENC_FRAME_SETTING_EFFORT,StringToInteger(option)) !=
-          JXL_ENC_SUCCESS)
+      double
+        effort;
+
+      if ((JXLParseOption(option,0.0,100.0,&effort) == MagickFalse) ||
+          (JxlEncoderFrameSettingsSetOption(frame_settings,
+           JXL_ENC_FRAME_SETTING_EFFORT,(int64_t) effort) !=
+           JXL_ENC_SUCCESS))
         (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
           "InvalidSetting","`%s'",option);
     }
   option=GetImageOption(image_info,"jxl:decoding-speed");
   if (option != (const char *) NULL)
     {
-      if (JxlEncoderFrameSettingsSetOption(frame_settings,
-          JXL_ENC_FRAME_SETTING_DECODING_SPEED,StringToInteger(option)) !=
-          JXL_ENC_SUCCESS)
+      double
+        speed;
+
+      if ((JXLParseOption(option,0.0,100.0,&speed) == MagickFalse) ||
+          (JxlEncoderFrameSettingsSetOption(frame_settings,
+           JXL_ENC_FRAME_SETTING_DECODING_SPEED,(int64_t) speed) !=
+           JXL_ENC_SUCCESS))
         (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
           "InvalidSetting","`%s'",option);
     }
@@ -1391,12 +1422,15 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
           (GetStringInfoLength(exif_profile) > 6))
         {
           /*
-            Add Exif profile.  Assumes "Exif\0\0" JPEG APP1 prefix.
+            Add Exif profile.  The box starts with the offset of the TIFF
+            header, 6 when the profile carries the "Exif\0\0" APP1 prefix.
           */
           StringInfo
             *profile;

-          profile=BlobToStringInfo("\0\0\0\6",4);
+          profile=BlobToStringInfo(
+            (memcmp(GetStringInfoDatum(exif_profile),"Exif\0\0",6) == 0) ?
+            "\0\0\0\6" : "\0\0\0\0",4);
           if (profile != (StringInfo *) NULL)
             {
               ConcatenateStringInfo(profile,exif_profile);
@@ -1445,7 +1479,8 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
     size_t
       bytes_per_row;

-    if (HeapOverflowSanityCheckGetSize(image->columns,channels_size,&bytes_per_row) != MagickFalse)
+    if (HeapOverflowSanityCheckGetSize(image->columns,channels_size,
+          &bytes_per_row) != MagickFalse)
       {
         (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
           "MemoryAllocationFailed","`%s'",image->filename);
@@ -1466,6 +1501,7 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
         JxlBlendInfo
           alpha_blend_info;

+        JxlEncoderInitFrameHeader(&frame_header);
         frame_header.duration=(uint32_t) MagickMin(image->delay,
           (size_t) UINT32_MAX);
         if ((image->previous == (Image *) NULL) ||
@@ -1495,9 +1531,11 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
         if (basic_info.num_extra_channels > 0)
           {
             JxlEncoderInitBlendInfo(&alpha_blend_info);
-            alpha_blend_info.blendmode=frame_header.layer_info.blend_info.blendmode;
+            alpha_blend_info.blendmode=
+              frame_header.layer_info.blend_info.blendmode;
             alpha_blend_info.source=frame_header.layer_info.blend_info.source;
-            (void) JxlEncoderSetExtraChannelBlendInfo(frame_settings,0,&alpha_blend_info);
+            (void) JxlEncoderSetExtraChannelBlendInfo(frame_settings,0,
+              &alpha_blend_info);
           }
       }
     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
@@ -1518,12 +1556,12 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
         status=MagickFalse;
         break;
       }
-    if (jxl_status != JXL_ENC_SUCCESS)
-      break;
     jxl_status=JxlEncoderAddImageFrame(frame_settings,&pixel_format,pixels,
       bytes_per_row*image->rows);
     if (jxl_status != JXL_ENC_SUCCESS)
       break;
+    if (image_info->adjoin == MagickFalse)
+      break;
     next=GetNextImageInList(image);
     if (next == (Image*) NULL)
       break;
@@ -1539,7 +1577,7 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
   } while (image_info->adjoin != MagickFalse);
   if (pixel_info != (MemoryInfo *) NULL)
     pixel_info=RelinquishVirtualMemory(pixel_info);
-  if (jxl_status == JXL_ENC_SUCCESS)
+  if ((status != MagickFalse) && (jxl_status == JXL_ENC_SUCCESS))
     {
       unsigned char
         *output_buffer;
@@ -1582,7 +1620,7 @@ static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
     }
   JxlThreadParallelRunnerDestroy(runner);
   JxlEncoderDestroy(jxl_info);
-  if (jxl_status != JXL_ENC_SUCCESS)
+  if ((status != MagickFalse) && (jxl_status != JXL_ENC_SUCCESS))
     ThrowWriterException(CoderError,"UnableToWriteImageData");
   if (CloseBlob(image) == MagickFalse)
     status=MagickFalse;