GraphicsMagick: SetImageAttribute(): Eliminate memory leak when ...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.7046.1681598934.6776.graphicsmagick-commit@lists.sourceforge.net>
changeset 3ce01217413b in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=3ce01217413b
summary: SetImageAttribute(): Eliminate memory leak when handling attribute with key "EXIF:Orientation"

diffstat:

 ChangeLog                              |   6 ++++++
 VisualMagick/installer/inc/version.isx |   4 ++--
 coders/miff.c                          |   4 ++++
 magick/attribute.c                     |  33 +++++++++++++++++++--------------
 magick/version.h                       |   4 ++--
 www/Changelog.html                     |   8 ++++++++
 6 files changed, 41 insertions(+), 18 deletions(-)

diffs (138 lines):

diff -r 8bec800dbaef -r 3ce01217413b ChangeLog
--- a/ChangeLog	Sat Apr 08 18:31:31 2023 -0500
+++ b/ChangeLog	Sat Apr 15 17:48:41 2023 -0500
@@ -1,3 +1,9 @@
+2023-04-15  Bob Friesenhahn  <[email protected]>
+
+	* 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  <[email protected]>
 
 	* coders/jpeg.c (WriteJPEGImage): Do not set image resolution if
diff -r 8bec800dbaef -r 3ce01217413b VisualMagick/installer/inc/version.isx
--- a/VisualMagick/installer/inc/version.isx	Sat Apr 08 18:31:31 2023 -0500
+++ b/VisualMagick/installer/inc/version.isx	Sat Apr 15 17:48:41 2023 -0500
@@ -10,5 +10,5 @@
 
 #define public MagickPackageName "GraphicsMagick"
 #define public MagickPackageVersion "1.4"
-#define public MagickPackageVersionAddendum ".020230408"
-#define public MagickPackageReleaseDate "snapshot-20230408"
+#define public MagickPackageVersionAddendum ".020230415"
+#define public MagickPackageReleaseDate "snapshot-20230415"
diff -r 8bec800dbaef -r 3ce01217413b coders/miff.c
--- a/coders/miff.c	Sat Apr 08 18:31:31 2023 -0500
+++ b/coders/miff.c	Sat Apr 15 17:48:41 2023 -0500
@@ -761,10 +761,14 @@
   MagickPassFail
     status;
 
+  status = SetImageAttribute(image,key,value);
+
+#if 0
   if (GetImageAttribute(image,key) == (const ImageAttribute *) NULL)
     status = SetImageAttribute(image,key,value);
   else
     status = MagickFail;
+#endif
 
   return status;
 };
diff -r 8bec800dbaef -r 3ce01217413b magick/attribute.c
--- a/magick/attribute.c	Sat Apr 08 18:31:31 2023 -0500
+++ b/magick/attribute.c	Sat Apr 15 17:48:41 2023 -0500
@@ -3178,9 +3178,6 @@
   register ImageAttribute
     *p;
 
-  int
-    orientation;
-
   /*
     Initialize new attribute.
   */
@@ -3271,6 +3268,9 @@
 
           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 @@
                 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 @@
               /*
                 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 */};
diff -r 8bec800dbaef -r 3ce01217413b magick/version.h
--- a/magick/version.h	Sat Apr 08 18:31:31 2023 -0500
+++ b/magick/version.h	Sat Apr 15 17:48:41 2023 -0500
@@ -38,8 +38,8 @@
 #define MagickLibVersion  0x272402
 #define MagickLibVersionText  "1.4"
 #define MagickLibVersionNumber 27,24,2
-#define MagickChangeDate   "20230408"
-#define MagickReleaseDate  "snapshot-20230408"
+#define MagickChangeDate   "20230415"
+#define MagickReleaseDate  "snapshot-20230415"
 
 /*
   The MagickLibInterfaceNewest and MagickLibInterfaceOldest defines
diff -r 8bec800dbaef -r 3ce01217413b www/Changelog.html
--- a/www/Changelog.html	Sat Apr 08 18:31:31 2023 -0500
+++ b/www/Changelog.html	Sat Apr 15 17:48:41 2023 -0500
@@ -37,6 +37,14 @@
 </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">
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.