changeset 26172:a7be718a9dd3

validate editor file codec derived from name (bug #55139) * file-editor-tab.cc (load_file): check codec from name against nullptr and take codec from locale as fall back * resource-manager.cc (do_combo_encoding): do not use system encoding as default encoding in the preferences dialogs combo box.
author Torsten <mttl@mailbox.org>
date Wed, 05 Dec 2018 21:15:40 +0100
parents 00b3e20eb3bc
children 0a145a1600e3
files libgui/src/m-editor/file-editor-tab.cc libgui/src/resource-manager.cc
diffstat 2 files changed, 17 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Wed Dec 05 10:39:45 2018 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Wed Dec 05 21:15:40 2018 +0100
@@ -1753,6 +1753,9 @@
     // decode
     QTextCodec::ConverterState st;
     QTextCodec *codec = QTextCodec::codecForName (_encoding.toLatin1 ());
+    if (codec == nullptr)
+      codec = QTextCodec::codecForLocale ();
+
     const QString text = codec->toUnicode(text_data.constData(),
                                           text_data.size(), &st);
 
--- a/libgui/src/resource-manager.cc	Wed Dec 05 10:39:45 2018 -0800
+++ b/libgui/src/resource-manager.cc	Wed Dec 05 21:15:40 2018 +0100
@@ -403,22 +403,27 @@
         enc = m_settings->value (ed_default_enc.key, ed_default_enc.def).toString ();
         if (enc.isEmpty ())  // still empty?
           enc = ed_default_enc.def.toString ();     // take default
+
+        if (! QTextCodec::codecForName (enc.toLatin1 ())) // does it exist?
+          enc = "";   // no, so clear it
       }
 
     // fill the combo box
     foreach (QString c, all_codecs)
       combo->addItem (c);
 
-    // prepend the default item
-    combo->insertSeparator (0);
-    combo->insertItem (0, ed_default_enc.def.toString ());
+    // prepend the current/default item and select it
+    if (! enc.isEmpty ())
+      {
+        combo->insertSeparator (0);
+        combo->insertItem (0, ed_default_enc.def.toString ());
 
-    // select the current/default item
-    int idx = combo->findText (enc);
-    if (idx >= 0)
-      combo->setCurrentIndex (idx);
-    else
-      combo->setCurrentIndex (0);
+        int idx = combo->findText (enc);
+        if (idx >= 0)
+          combo->setCurrentIndex (idx);
+        else
+          combo->setCurrentIndex (0);
+      }
 
     combo->setMaxVisibleItems (12);
   }