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.
This commit is contained in:
Georg Baum 2014-02-28 22:42:38 +01:00
parent 2a8de1b23d
commit 8097911a9f

View File

@ -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));