From 3929a71cd0362cea6ca4ea0f7135ad524cfc8309 Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Tue, 28 Feb 2012 15:21:46 +0000 Subject: [PATCH] Avoid crash in call to magic_file() if the checked file does not exist git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40803 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Format.cpp | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Format.cpp b/src/Format.cpp index c20ac9ed43..4b6e134941 100644 --- a/src/Format.cpp +++ b/src/Format.cpp @@ -375,32 +375,34 @@ string Formats::getFormatFromFile(FileName const & filename) const return string(); #ifdef HAVE_MAGIC_H - magic_t magic_cookie = magic_open(MAGIC_MIME); - if (magic_cookie) { - string format; - if (magic_load(magic_cookie, NULL) != 0) { - LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n" - << "\tCouldn't load magic database - " - << magic_error(magic_cookie)); - } else { - string mime = magic_file(magic_cookie, - filename.toFilesystemEncoding().c_str()); - mime = token(mime, ';', 0); - // we need our own ps/eps detection - if (mime != "application/postscript") { - Formats::const_iterator cit = - find_if(formatlist.begin(), formatlist.end(), - FormatMimeEqual(mime)); - if (cit != formats.end()) { - LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: " - << mime << " -> " << cit->name()); - format = cit->name(); + if (filename.exists()) { + magic_t magic_cookie = magic_open(MAGIC_MIME); + if (magic_cookie) { + string format; + if (magic_load(magic_cookie, NULL) != 0) { + LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n" + << "\tCouldn't load magic database - " + << magic_error(magic_cookie)); + } else { + string mime = magic_file(magic_cookie, + filename.toFilesystemEncoding().c_str()); + mime = token(mime, ';', 0); + // we need our own ps/eps detection + if (mime != "application/postscript") { + Formats::const_iterator cit = + find_if(formatlist.begin(), formatlist.end(), + FormatMimeEqual(mime)); + if (cit != formats.end()) { + LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: " + << mime << " -> " << cit->name()); + format = cit->name(); + } } } + magic_close(magic_cookie); + if (!format.empty()) + return format; } - magic_close(magic_cookie); - if (!format.empty()) - return format; } #endif