eliminate_memory_leak_when_handling_EXIFOrientation
authorLaszlo Boszormenyi (GCS) <gcs@debian.org>
Mon, 17 Apr 2023 17:17:10 +0000 (18:17 +0100)
committerLaszlo Boszormenyi (GCS) <gcs@debian.org>
Mon, 17 Apr 2023 17:17:10 +0000 (18:17 +0100)
# HG changeset patch
# User Bob Friesenhahn <bfriesen@GraphicsMagick.org>
# Date 1681598921 18000
# Node ID 3ce01217413bb5b476460bbc8ab11020205eeda0
# Parent  8bec800dbaef2d72da0e7e997ad45bece0e95893
SetImageAttribute(): Eliminate memory leak when handling attribute with key "EXIF:Orientation"

Gbp-Pq: Name eliminate_memory_leak_when_handling_EXIFOrientation.patch

ChangeLog
coders/miff.c
magick/attribute.c
www/Changelog.html

index 03aa5ae8e6ee55a0c6814ba70a005470ecf907ee..12664224397736f1e2a9f3da1cc40c3934901a9a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-04-15  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
+
+       * magick/attribute.c (SetImageAttribute): Eliminate memory leak
+       when handling attribute with key "EXIF:Orientation".  (SourceForge
+       issue #707 "memory leaks in gm").
+
 2023-04-08  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
 
        * coders/mpc.c (ReadMPCImage): If an attribute appears multiple
index f26b810c6bea952c2360051c2b0de63d2840f551..f1925598b537a76d0288bc2b26982644acb82407 100644 (file)
@@ -761,6 +761,8 @@ SetNewImageAttribute(Image *image,const char *key,const char *value)
   MagickPassFail
     status;
 
+  status = SetImageAttribute(image,key,value);
+
   if (GetImageAttribute(image,key) == (const ImageAttribute *) NULL)
     status = SetImageAttribute(image,key,value);
   else
index d1768fb5423e64d342fbe7d8598779f17ad12d1e..9308227d950b8e2e786a25c862b774b90eedafe0 100644 (file)
@@ -3178,9 +3178,6 @@ SetImageAttribute(Image *image,const char *key,const char *value)
   register ImageAttribute
     *p;
 
-  int
-    orientation;
-
   /*
     Initialize new attribute.
   */
@@ -3271,6 +3268,9 @@ SetImageAttribute(Image *image,const char *key,const char *value)
 
           if (LocaleCompare(attribute->key,"EXIF:Orientation") == 0)
             {
+              int
+                orientation = 0;
+
               /*
                 Special handling for EXIF orientation tag.
                 If new value differs from existing value,
@@ -3278,17 +3278,19 @@ SetImageAttribute(Image *image,const char *key,const char *value)
                 is valid. Don't append new value to existing value,
                 replace it instead.
               */
-              orientation = MagickAtoI(value);
-              if (orientation > 0 || orientation <= (int)LeftBottomOrientation)
-                SetEXIFOrientation(image, orientation);
-
-              /* Replace current attribute with new one */
-              attribute->next = p->next;
-              if (p->previous == (ImageAttribute *) NULL)
-                image->attributes=attribute;
-              else
-                p->previous->next = attribute;
-              DestroyImageAttribute(p);
+              if ((MagickAtoIChk(value, &orientation) == MagickPass) &&
+                  (orientation > 0 || orientation <= (int)LeftBottomOrientation))
+                {
+                  SetEXIFOrientation(image, orientation);
+                }
+              /* Assign changed value to attribute in list */
+              if (LocaleCompare(p->value, attribute->value) != 0)
+                {
+                  MagickFreeMemory(p->value);
+                  p->value=attribute->value;
+                  attribute->value = (char *) NULL;
+                }
+              DestroyImageAttribute(attribute);
               return(MagickPass);
             }
           else
@@ -3296,6 +3298,9 @@ SetImageAttribute(Image *image,const char *key,const char *value)
               /*
                 Extend existing text string.  This functionality is deprecated!
               */
+              fprintf(stderr,
+                      "SetImageAttribute: Extending attribute value text is deprecated! (key=\"%s\")\n",
+                      attribute->key);
               min_l=p->length+attribute->length+1;
               for (realloc_l=2; realloc_l <= min_l; realloc_l *= 2)
                     { /* nada */};
index e6abafff78bde3d73fccdd9b654d59985669412c..9abae9ce61861b2ac33be9911ea254a71b1c8867 100644 (file)
 </div>
 
 <div class="document">
+<p>2023-04-15  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
+<blockquote>
+<ul class="simple">
+<li><p>magick/attribute.c (SetImageAttribute): Eliminate memory leak
+when handling attribute with key &quot;EXIF:Orientation&quot;.  (SourceForge
+issue #707 &quot;memory leaks in gm&quot;).</p></li>
+</ul>
+</blockquote>
 <p>2023-04-08  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <ul class="simple">