commit 8097911a9fcfe4a405ff628a3fe1824bfad7b0bc
Author: Georg Baum <[email protected]>
Date:   Fri Feb 28 22:42:38 2014 +0100

    Fix crash if magic_file() returns an error
    
    Thanks to Benjamin Piwowarski who reported the problem and provided a fix.
    magic_file() returns a NULL pointer if an error occurs, and due to a bug
    this happens frequently on OS X: https://trac.macports.org/ticket/38771
    I did not use the original patch, since I did not want to put a platform
    specific workaround in (this needs further discussion), but the crash can
    occur on all platforms and needs to be fixed.

diff --git a/src/Format.cpp b/src/Format.cpp
index ff03ab5..2bc7ac5 100644
--- a/src/Format.cpp
+++ b/src/Format.cpp
@@ -402,11 +402,19 @@ string Formats::getFormatFromFile(FileName const & 
filename) const
                                        << "\tCouldn't load magic database - "
                                        << magic_error(magic_cookie));
                        } else {
-                               string mime = magic_file(magic_cookie,
+                               char const * result = magic_file(magic_cookie,
                                        
filename.toFilesystemEncoding().c_str());
-                               mime = token(mime, ';', 0);
+                               string mime;
+                               if (result)
+                                       mime = token(result, ';', 0);
+                               else {
+                                       LYXERR(Debug::GRAPHICS, 
"Formats::getFormatFromFile\n"
+                                               << "\tCouldn't query magic 
database - "
+                                               << magic_error(magic_cookie));
+                               }
                                // we need our own ps/eps detection
-                               if ((mime != "application/postscript") && (mime 
!= "text/plain")) {
+                               if (!mime.empty() && mime != 
"application/postscript" &&
+                                   mime != "text/plain") {
                                        Formats::const_iterator cit =
                                                find_if(formatlist.begin(), 
formatlist.end(),
                                                        FormatMimeEqual(mime));

Reply via email to