Commit 8533deb2f for imagemagick.org

commit 8533deb2fcba356bf884aff5864b77ab34f6d5b4
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 17:45:39 2026 -0500

    if no tiles are decoded, the coder reports an exception

diff --git a/coders/jnx.c b/coders/jnx.c
index b9a8f72e0..9633b23d5 100644
--- a/coders/jnx.c
+++ b/coders/jnx.c
@@ -175,7 +175,7 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   jnx_info.southwest.x=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
   jnx_info.southwest.y=180.0*ReadBlobLSBSignedLong(image)/0x7fffffff;
   jnx_info.levels=ReadBlobLSBSignedLong(image);
-  if (jnx_info.levels > JNXMaxLevels)
+  if ((jnx_info.levels <= 0) || (jnx_info.levels > JNXMaxLevels))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   jnx_info.expire=ReadBlobLSBSignedLong(image);
   jnx_info.id=ReadBlobLSBSignedLong(image);
@@ -185,8 +185,7 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if (jnx_info.version > 3)
     jnx_info.order=ReadBlobLSBSignedLong(image);
   else
-    if (jnx_info.version == 3)
-      jnx_info.order=30;
+    jnx_info.order=30;
   if (EOFBlob(image) != MagickFalse)
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   /*
@@ -197,16 +196,19 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   for (i=0; i < (ssize_t) jnx_info.levels; i++)
   {
     jnx_level_info[i].count=ReadBlobLSBSignedLong(image);
-    if (jnx_level_info[i].count > 50000)
+    if ((jnx_level_info[i].count < 0) || (jnx_level_info[i].count > 50000))
       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     jnx_level_info[i].offset=ReadBlobLSBSignedLong(image);
+    if (jnx_level_info[i].offset < 0)
+      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     /* scale */
     (void) ReadBlobLSBLong(image);
     if (jnx_info.version > 3)
       {
         /* copyright */
         (void) ReadBlobLSBLong(image);
-        while (ReadBlobLSBShort(image) != 0);
+        while ((ReadBlobLSBShort(image) != 0) && (EOFBlob(image) == MagickFalse))
+          ;
       }
     if (EOFBlob(image) != MagickFalse)
       ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
@@ -218,7 +220,7 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     Read JNX tiles.
   */
   images=NewImageList();
-  for (i=0; i < (ssize_t) jnx_info.levels; i++)
+  for (i=0; (i < (ssize_t) jnx_info.levels) && (status != MagickFalse); i++)
   {
     MagickOffsetType
       offset;
@@ -269,27 +271,47 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
         }
       tile_length=ReadBlobLSBLong(image);
       tile_offset=ReadBlobLSBSignedLong(image);
-      if (tile_offset == -1)
-        continue;
-      restore_offset=TellBlob(image);
-      if (restore_offset < 0)
-        continue;
-      offset=SeekBlob(image,(MagickOffsetType) tile_offset,SEEK_SET);
-      if (offset != (MagickOffsetType) tile_offset)
-        continue;
+      if (EOFBlob(image) != MagickFalse)
+        {
+          images=DestroyImageList(images);
+          ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+        }
+      if ((tile_offset < 0) || (tile_length == 0))
+        continue;  /* -1 marks an absent tile */
       /*
-        Read a tile.
+        Validate the tile extent against the file before seeking.
       */
       if (HeapOverflowCheckAdd(tile_length,2) != MagickFalse)
         {
           images=DestroyImageList(images);
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         }
-      if (((MagickSizeType) tile_length) > GetBlobSize(image))
+      if (((MagickSizeType) tile_offset > GetBlobSize(image)) ||
+          ((MagickSizeType) tile_length >
+           (GetBlobSize(image)-(MagickSizeType) tile_offset)))
         {
           images=DestroyImageList(images);
           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
         }
+      restore_offset=TellBlob(image);
+      if (restore_offset < 0)
+        {
+          images=DestroyImageList(images);
+          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+        }
+      offset=SeekBlob(image,(MagickOffsetType) tile_offset,SEEK_SET);
+      if (offset != (MagickOffsetType) tile_offset)
+        {
+          if (SeekBlob(image,restore_offset,SEEK_SET) != restore_offset)
+            {
+              images=DestroyImageList(images);
+              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+            }
+          continue;
+        }
+      /*
+        Read a tile.
+      */
       blob=(unsigned char *) AcquireQuantumMemory((size_t) tile_length+2,
         sizeof(*blob));
       if (blob == (unsigned char *) NULL)
@@ -312,6 +334,13 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       read_info=DestroyImageInfo(read_info);
       blob=(unsigned char *) RelinquishMagickMemory(blob);
       offset=SeekBlob(image,restore_offset,SEEK_SET);
+      if (offset != restore_offset)
+        {
+          if (tile_image != (Image *) NULL)
+            tile_image=DestroyImageList(tile_image);
+          images=DestroyImageList(images);
+          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+        }
       if (tile_image == (Image *) NULL)
         continue;
       tile_image->depth=8;
@@ -334,6 +363,9 @@ static Image *ReadJNXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           status=MagickFalse;
       }
   }
+  if (images == (Image *) NULL)
+    ThrowReaderException(CorruptImageError,
+      "ImageFileDoesNotContainAnyImageData");
   (void) CloseBlob(image);
   image=DestroyImage(image);
   return(GetFirstImageInList(images));