Subject: bug#35725: 26.2; Fatal error 11: Segmentation fault; Crash on: (find-file "any.svg")

From: Paul Eggert <[email-masked]>
Date: Sat, 18 May 2019 10:12:31 -0700

Glenn Morris wrote:
> Hey, thanks for putting my name in a commit that will make people angry. :)

You're welcome. But really, they should direct any ire at me.

> Putting the priority of libmagick below libpng etc is probably less
> contentious, but of course more work.

Could you explain what you mean by that? Emacs should already prefer 
more-specialized code (if available) to libmagick.

I just now checked with splash.svg, and found that Emacs used libmagick instead 
of librsvg when both were available. I tracked this down to a bug in image.el, 
which I fixed by installing the attached patch into master. Is this the problem 
you were thinking of, or was it something else?
>From db9af103944959be640a53fcf0f0b696f25d553f Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 18 May 2019 10:00:26 -0700
Subject: [PATCH] For SVG, 8192 is the new 256
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Prefer librsvg for display of splash.svg
When both librsvg and Imagemagick are available, Emacs should
prefer librsvg to render SVG images.  However, Emacs was using
Imagemagick to render its own splash.svg file because
image-type-from-file-header returned nil for that file.
* lisp/image.el (image-type-from-buffer)
(image-type-from-file-header): Look at the first 8192 bytes of
the image, not just the first 256.  For Emacs’s own splash.svg
file, image-type-header-regexps needs to look at 939 bytes.
8192 bytes is a reasonable number nowadays given typical file
system design.
* test/lisp/image-tests.el (image-tests--emacs-images-directory):
New contant.
(image-type-from-file-header-test): New test.
---
 lisp/image.el            |  4 ++--
 test/lisp/image-tests.el | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/lisp/image.el b/lisp/image.el
index ba87d7f785..db11302086 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -315,7 +315,7 @@ image-type-from-buffer
 					(buffer-substring
 					 (point-min)
 					 (min (point-max)
-					      (+ (point-min) 256))))))
+					      (+ (point-min) 8192))))))
 		     (setq image-type (cdr image-type))))
 	    (setq type image-type
 		  types nil)
@@ -339,7 +339,7 @@ image-type-from-file-header
        (file-readable-p file)
        (with-temp-buffer
 	 (set-buffer-multibyte nil)
-	 (insert-file-contents-literally file nil 0 256)
+	 (insert-file-contents-literally file nil 0 8192)
 	 (image-type-from-buffer))))

diff --git a/test/lisp/image-tests.el b/test/lisp/image-tests.el
index 89b926e629..621646e575 100644
--- a/test/lisp/image-tests.el
+++ b/test/lisp/image-tests.el
@@ -22,6 +22,10 @@
 (require 'ert)
 (require 'image)
 
+(defconst image-tests--emacs-images-directory
+  (expand-file-name "../etc/images" (getenv "EMACS_TEST_DIRECTORY"))
+  "Directory containing Emacs images.")
+
 (ert-deftest image--set-property ()
   "Test `image--set-property' behavior."
   (let ((image (list 'image)))
@@ -42,4 +46,11 @@
     (setf (image-property image :width) nil)
     (should (equal image '(image)))))
 
+(ert-deftest image-type-from-file-header-test ()
+  "Test image-type-from-file-header."
+  (should (eq 'svg
+	      (image-type-from-file-header
+	       (expand-file-name "splash.svg"
+				 image-tests--emacs-images-directory)))))
+
 ;;; image-tests.el ends here
-- 
2.17.1