From a99cadfda0665f3586a1f873172dec80e67b32df Mon Sep 17 00:00:00 2001
From: dirk <dirk@aa41f4f7-0bf4-0310-aa73-e5a19afd5a74>
Date: Thu, 6 Nov 2014 21:09:54 +0000
Subject: [PATCH] Added missing calls to RelinquishUniqueFileResource.

Avoid to leak fd in case of error.

git-svn-id: https://subversion.imagemagick.org/subversion/ImageMagick/branches/ImageMagick-6@16971 aa41f4f7-0bf4-0310-aa73-e5a19afd5a74
origin: http://trac.imagemagick.org/changeset/16971
---
 coders/dcm.c  | 22 +++++++++++++---------
 coders/dot.c  |  5 ++++-
 coders/exr.c  | 10 +++++++++-
 coders/pict.c |  4 ++++
 coders/pwp.c  |  6 +++++-
 5 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/coders/dcm.c b/coders/dcm.c
index 5c1cde7..d826a96 100644
--- a/coders/dcm.c
+++ b/coders/dcm.c
@@ -3469,28 +3469,32 @@ static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         unsigned int
           tag;
 
+        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
+        length=(size_t) ReadBlobLSBLong(image);
+        if (tag == 0xFFFEE0DD)
+          break; /* sequence delimiter tag */
+        if (tag != 0xFFFEE000)
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
         file=(FILE *) NULL;
         unique_file=AcquireUniqueFileResource(filename);
         if (unique_file != -1)
           file=fdopen(unique_file,"wb");
-        if ((unique_file == -1) || (file == (FILE *) NULL))
+        if (file == (FILE *) NULL)
           {
+            (void) RelinquishUniqueFileResource(filename);
             ThrowFileException(exception,FileOpenError,
               "UnableToCreateTemporaryFile",filename);
             break;
           }
-        tag=(ReadBlobLSBShort(image) << 16) | ReadBlobLSBShort(image);
-        length=(size_t) ReadBlobLSBLong(image);
-        if (tag == 0xFFFEE0DD)
-          break; /* sequence delimiter tag */
-        if (tag != 0xFFFEE000)
-          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
         for ( ; length != 0; length--)
         {
           c=ReadBlobByte(image);
           if (c == EOF)
-            ThrowFileException(exception,CorruptImageError,
-              "UnexpectedEndOfFile",image->filename);
+            {
+              ThrowFileException(exception,CorruptImageError,
+                "UnexpectedEndOfFile",image->filename);
+              break;
+            }
           (void) fputc(c,file);
         }
         (void) fclose(file);
diff --git a/coders/dot.c b/coders/dot.c
index c92d7c1..844ffc8 100644
--- a/coders/dot.c
+++ b/coders/dot.c
@@ -136,7 +136,10 @@ static Image *ReadDOTImage(const ImageInfo *image_info,ExceptionInfo *exception)
     read_info->filename,image_info->filename);
   graph=agread(GetBlobFileHandle(image));
   if (graph == (graph_t *) NULL)
-    return ((Image *) NULL);
+    {
+      (void) RelinquishUniqueFileResource(read_info->filename);
+      return ((Image *) NULL);
+    }
   option=GetImageOption(image_info,"dot:layout-engine");
   if (option == (const char *) NULL)
     gvLayout(graphic_context,graph,(char *) "dot");
diff --git a/coders/exr.c b/coders/exr.c
index 0726020..f9b6470 100644
--- a/coders/exr.c
+++ b/coders/exr.c
@@ -191,6 +191,8 @@ static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
     {
       ThrowFileException(exception,BlobError,"UnableToOpenBlob",
         ImfErrorMessage());
+      if (LocaleCompare(image_info->filename,read_info->filename) != 0)
+        (void) RelinquishUniqueFileResource(read_info->filename);
       read_info=DestroyImageInfo(read_info);
       return((Image *) NULL);
     }
@@ -212,6 +214,9 @@ static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if (scanline == (ImfRgba *) NULL)
     {
       (void) ImfCloseInputFile(file);
+      if (LocaleCompare(image_info->filename,read_info->filename) != 0)
+        (void) RelinquishUniqueFileResource(read_info->filename);
+      read_info=DestroyImageInfo(read_info);
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
     }
   for (y=0; y < (long) image->rows; y++)
@@ -413,15 +418,18 @@ static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image)
   ImfDeleteHeader(hdr_info);
   if (file == (ImfOutputFile *) NULL)
     {
+      (void) RelinquishUniqueFileResource(write_info->filename);
+      write_info=DestroyImageInfo(write_info);
       ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
         ImfErrorMessage());
-      write_info=DestroyImageInfo(write_info);
       return(MagickFalse);
     }
   scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
   if (scanline == (ImfRgba *) NULL)
     {
       (void) ImfCloseOutputFile(file);
+      (void) RelinquishUniqueFileResource(write_info->filename);
+      write_info=DestroyImageInfo(write_info);
       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
     }
   for (y=0; y < (long) image->rows; y++)
diff --git a/coders/pict.c b/coders/pict.c
index f1985e0..fa47b06 100644
--- a/coders/pict.c
+++ b/coders/pict.c
@@ -1364,6 +1364,9 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
           file=fdopen(unique_file,"wb");
         if ((unique_file == -1) || (file == (FILE *) NULL))
           {
+            if (file != (FILE *) NULL)
+              (void) fclose(file);
+            (void) RelinquishUniqueFileResource(read_info->filename);
             (void) CopyMagickString(image->filename,read_info->filename,
               MaxTextExtent);
             ThrowFileException(exception,FileOpenError,
@@ -1377,6 +1380,7 @@ static Image *ReadPICTImage(const ImageInfo *image_info,
         if (ReadRectangle(image,&frame) == MagickFalse)
           {
             (void) fclose(file);
+            (void) RelinquishUniqueFileResource(read_info->filename);
             ThrowReaderException(CorruptImageError,"ImproperImageHeader");
           }
         for (i=0; i < 122; i++)
diff --git a/coders/pwp.c b/coders/pwp.c
index 0fe9d3e..fd21b45 100644
--- a/coders/pwp.c
+++ b/coders/pwp.c
@@ -193,7 +193,10 @@ static Image *ReadPWPImage(const ImageInfo *image_info,ExceptionInfo *exception)
     if (c == EOF)
       break;
     if (LocaleNCompare((char *) (magick+12),"SFW94A",6) != 0)
-      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      {
+        (void) RelinquishUniqueFileResource(read_info->filename);
+        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      }
     /*
       Dump SFW image to a temporary file.
     */
@@ -202,6 +205,7 @@ static Image *ReadPWPImage(const ImageInfo *image_info,ExceptionInfo *exception)
       file=fdopen(unique_file,"wb");
     if ((unique_file == -1) || (file == (FILE *) NULL))
       {
+        (void) RelinquishUniqueFileResource(read_info->filename);
         ThrowFileException(exception,FileOpenError,"UnableToWriteFile",
           image->filename);
         image=DestroyImageList(image);
-- 
2.1.4

