From 01c3051fc39127302943846f15047da305c07df0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 14 Aug 2013 00:00:25 -0700 Subject: [PATCH] * 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. --- src/ChangeLog | 6 +++++ src/image.c | 69 ++++++++++++++++++++++----------------------------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 88df9f1376..30a3dc3462 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2013-08-14 Paul Eggert + + * 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 * xdisp.c (adjust_window_ends): Move duplicated code to new function. diff --git a/src/image.c b/src/image.c index 99bb9d8c13..d93d1b517b 100644 --- a/src/image.c +++ b/src/image.c @@ -7845,35 +7845,27 @@ imagemagick_error (MagickWand *wand) } /* 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; + Lisp_Object symbol = intern ("image-format-suffixes"); + Lisp_Object val = find_symbol_value (symbol); + Lisp_Object format; - 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); + 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 @@ imagemagick_load_image (struct frame *f, struct image *img, 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 @@ imagemagick_load_image (struct frame *f, struct image *img, 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 @@ imagemagick_load_image (struct frame *f, struct image *img, 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 @@ imagemagick_load_image (struct frame *f, struct image *img, /* `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. */ -- 2.20.1