emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r113866: * image.c (imagemagick_filename_hint): New


From: Paul Eggert
Subject: [Emacs-diffs] trunk r113866: * image.c (imagemagick_filename_hint): New arg HINT_BUFFER.
Date: Wed, 14 Aug 2013 07:00:43 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113866
revision-id: address@hidden
parent: address@hidden
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Wed 2013-08-14 00:00:25 -0700
message:
  * image.c (imagemagick_filename_hint): New arg HINT_BUFFER.
  
  Use changed.  This avoids the need to call xmalloc and for the
  caller to call xfree, and avoids memory leaks in some situations.
modified:
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/image.c                    image.c-20091113204419-o5vbwnq5f7feedwu-2969
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2013-08-14 06:06:56 +0000
+++ b/src/ChangeLog     2013-08-14 07:00:25 +0000
@@ -1,3 +1,9 @@
+2013-08-14  Paul Eggert  <address@hidden>
+
+       * image.c (imagemagick_filename_hint): New arg HINT_BUFFER.
+       Use changed.  This avoids the need to call xmalloc and for the
+       caller to call xfree, and avoids memory leaks in some situations.
+
 2013-08-14  Dmitry Antipov  <address@hidden>
 
        * xdisp.c (adjust_window_ends): Move duplicated code to new function.

=== modified file 'src/image.c'
--- a/src/image.c       2013-08-14 04:27:32 +0000
+++ b/src/image.c       2013-08-14 07:00:25 +0000
@@ -7845,35 +7845,27 @@
 }
 
 /* Possibly give ImageMagick some extra help to determine the image
-   type by supplying a "dummy" filename based on the Content-Type. */
+   type by supplying a "dummy" filename based on the Content-Type.  */
 
-static char*
-imagemagick_filename_hint (Lisp_Object spec)
+static char *
+imagemagick_filename_hint (Lisp_Object spec, char hint_buffer[MaxTextExtent])
 {
-  Lisp_Object format = image_spec_value (spec, intern (":format"), NULL);
-  Lisp_Object val, symbol = intern ("image-format-suffixes");
-  const char *prefix = "/tmp/foo.";
-  char *name;
-
-  if (NILP (Fboundp (symbol)))
-    return NULL;
-
-  val = Fassq (format, Fsymbol_value (symbol));
-  if (! CONSP (val))
-    return NULL;
-
-  val = Fcdr (val);
-  if (! CONSP (val))
-    return NULL;
-
-  val = Fcar (val);
+  Lisp_Object symbol = intern ("image-format-suffixes");
+  Lisp_Object val = find_symbol_value (symbol);
+  Lisp_Object format;
+
+  if (! CONSP (val))
+    return NULL;
+
+  format = image_spec_value (spec, intern (":format"), NULL);
+  val = Fcar_safe (Fcdr_safe (Fassq (format, val)));
   if (! STRINGP (val))
     return NULL;
 
-  name = xmalloc (strlen (prefix) + SBYTES (val) + 1);
-  strcpy (name, prefix);
-  strcat (name, SSDATA (val));
-  return name;
+  /* It's OK to truncate the hint if it has MaxTextExtent or more bytes,
+     as ImageMagick would ignore the extra bytes anyway.  */
+  snprintf (hint_buffer, MaxTextExtent, "/tmp/foo.%s", SSDATA (val));
+  return hint_buffer;
 }
 
 /* Helper function for imagemagick_load, which does the actual loading
@@ -7909,6 +7901,7 @@
   int desired_width, desired_height;
   double rotation;
   int pixelwidth;
+  char hint_buffer[MaxTextExtent];
   char *filename_hint = NULL;
 
   /* Handle image index for image types who can contain more than one image.
@@ -7923,15 +7916,14 @@
   ping_wand = NewMagickWand ();
   /* MagickSetResolution (ping_wand, 2, 2);   (Bug#10112)  */
 
-  if (! filename)
-    filename_hint = imagemagick_filename_hint (img->spec);
-
-  if (filename_hint)
-    MagickSetFilename (ping_wand, filename_hint);
-
-  status = filename
-    ? MagickPingImage (ping_wand, filename)
-    : MagickPingImageBlob (ping_wand, contents, size);
+  if (filename)
+    status = MagickPingImage (ping_wand, filename);
+  else
+    {
+      filename_hint = imagemagick_filename_hint (img->spec, hint_buffer);
+      MagickSetFilename (ping_wand, filename_hint);
+      status = MagickPingImageBlob (ping_wand, contents, size);
+    }
 
   if (status == MagickFalse)
     {
@@ -7961,13 +7953,15 @@
 
   image_wand = NewMagickWand ();
 
-  if (filename_hint)
-    MagickSetFilename (image_wand, filename_hint);
+  if (filename)
+    status = MagickReadImage (image_wand, filename);
+  else
+    {
+      MagickSetFilename (image_wand, filename_hint);
+      status = MagickReadImageBlob (image_wand, contents, size);
+    }
 
-  if ((filename
-       ? MagickReadImage (image_wand, filename)
-       : MagickReadImageBlob (image_wand, contents, size))
-      == MagickFalse)
+  if (status == MagickFalse)
     {
       imagemagick_error (image_wand);
       goto imagemagick_error;
@@ -8207,16 +8201,11 @@
   /* `MagickWandTerminus' terminates the imagemagick environment.  */
   MagickWandTerminus ();
 
-  if (filename_hint)
-    free (filename_hint);
-
   return 1;
 
  imagemagick_error:
   DestroyMagickWand (image_wand);
   if (bg_wand) DestroyPixelWand (bg_wand);
-  if (filename_hint)
-    free (filename_hint);
 
   MagickWandTerminus ();
   /* TODO more cleanup.  */


reply via email to

[Prev in Thread] Current Thread [Next in Thread]