[meld] Sanitise regex strings to avoid invalid entries (closes bgo#667165)



commit 691b74feedce118633a003a1a9e157985608c196
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Wed Jan 11 06:21:45 2012 +1000

    Sanitise regex strings to avoid invalid entries (closes bgo#667165)
    
    It was previously possible to create invalid filter entries by
    inserting verbatim newline characters into the filter text entry, most
    likely by copy and paste. This commit adds some recovery for such
    broken situations, and also sanitises the regex strings on entry to
    avoid it happening in the first place.

 meld/meldapp.py     |    5 ++++-
 meld/preferences.py |    8 +++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/meld/meldapp.py b/meld/meldapp.py
index 9d554b9..7c98555 100644
--- a/meld/meldapp.py
+++ b/meld/meldapp.py
@@ -72,6 +72,8 @@ class FilterEntry(object):
     @classmethod
     def parse(cls, string, filter_type):
         elements = string.split("\t")
+        if len(elements) < 3:
+            return None
         name, active = elements[0], bool(int(elements[1]))
         filter_string = " ".join(elements[2:])
         compiled = FilterEntry.compile_filter(filter_string, filter_type)
@@ -128,7 +130,8 @@ class MeldApp(gobject.GObject):
             self.emit('text-filters-changed')
 
     def _parse_filters(self, string, filt_type):
-        return [FilterEntry.parse(l, filt_type) for l in string.split("\n")]
+        filt = [FilterEntry.parse(l, filt_type) for l in string.split("\n")]
+        return [f for f in filt if f is not None]
 
     def diff_files_callback(self, option, opt_str, value, parser):
         """Gather --diff arguments and append to a list"""
diff --git a/meld/preferences.py b/meld/preferences.py
index f27c230..9734cf4 100644
--- a/meld/preferences.py
+++ b/meld/preferences.py
@@ -45,6 +45,8 @@ class FilterList(listwidget.ListWidget):
 
         for filtstring in getattr(self.prefs, self.key).split("\n"):
             filt = meldapp.FilterEntry.parse(filtstring, filter_type)
+            if filt is None:
+                continue
             valid = filt.filter is not None
             self.model.append([filt.label, filt.active,
                                filt.filter_string, valid])
@@ -75,7 +77,11 @@ class FilterList(listwidget.ListWidget):
     def _update_filter_string(self, *args):
         pref = []
         for row in self.model:
-            pref.append("%s\t%s\t%s" % (row[0], 1 if row[1] else 0, row[2]))
+            pattern = row[2]
+            if pattern:
+                pattern = pattern.replace('\r', '')
+                pattern = pattern.replace('\n', '')
+            pref.append("%s\t%s\t%s" % (row[0], 1 if row[1] else 0, pattern))
         setattr(self.prefs, self.key, "\n".join(pref))
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]