commit b3fe9cb7049daaacbcf357ba5e4ece479f7b1be4
Author: Juergen Spitzmueller <sp...@lyx.org>
Date:   Sat Mar 16 04:52:51 2024 +0100

    Prevent iconv exception in previews (#13042)
    
    The preview loader assumes all content is in the main document encoding.
    
    As soon as content was not encodable, LyX crashed. We now check for that
    and if non-encodable glyphs are found, we do not produce a preview snippet
    and warn.
    
    Ideally, the preview loader should be made aware of encoding changes, or
    we should generally use utf8 for previews.
---
 src/graphics/PreviewLoader.cpp | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp
index a350a4939b..dc944b1929 100644
--- a/src/graphics/PreviewLoader.cpp
+++ b/src/graphics/PreviewLoader.cpp
@@ -849,11 +849,30 @@ void PreviewLoader::Impl::dumpData(odocstream & os,
        BitmapFile::const_iterator it  = vec.begin();
        BitmapFile::const_iterator end = vec.end();
 
+       Encoding const & enc = buffer_.params().encoding();
+
        for (; it != end; ++it) {
+               docstring res;
+               bool uncodable_content = false;
+               // check whether the content is encodable
+               // FIXME: the preview loader should be able
+               //        to handle multiple encodings
+               //        or we should generally use utf8
+               for (char_type n : from_utf8(it->first)) {
+                       if (!enc.encodable(n)) {
+                               LYXERR0("Uncodable character '"
+                                       << docstring(1, n)
+                                       << "' in preview snippet!");
+                               uncodable_content = true;
+                       } else
+                               res += n;
+               }
                // FIXME UNICODE
-               os << "\\begin{preview}\n"
-                  << from_utf8(it->first)
-                  << "\n\\end{preview}\n\n";
+               os << "\\begin{preview}\n";
+               // do not show incomplete preview
+               if (!uncodable_content)
+                       os << res;
+               os << "\n\\end{preview}\n\n";
        }
 }
 
-- 
lyx-cvs mailing list
lyx-cvs@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to