handle data escaping properly in cnf vars
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 7 Feb 2019 13:45:40 +0000 (14:45 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 7 Feb 2019 15:11:32 +0000 (16:11 +0100)
libcnffile has an additional step that strips escaping from
var data so this:

    42 MY_VAR,0: "foo\"bar\"baz"

is actually a valid CNF_VAR with a data member equals ``foo"bar"baz''.
Till now, cnfvar.py would reject such a line because of the
stricter matching. Since escaped strings are ubiquitous in JSON
encoded data, this must be handled properly. (Ironically this is
not an issue with the JSON interface due to the builtin string
escaping rules of the format.)

Imitate the behavior of libcnffile so properly escaped double
quotes don't terminate the string matching. Lucky for us we can
get away with extending the line regex with an alternating
pattern in a non-matching group ``(?:...''.

src/cnfvar.py

index 833a606..1b6b5bb 100644 (file)
@@ -409,7 +409,10 @@ base_line_pattern = re.compile(b"""
                                     \s*                  # optional spaces
                                     :                    # delimiter
                                     \s*                  # optional spaces
-                                    \"([^\"]*)\"         # quoted string (data)
+                                    \"(                  # quoted string (data)
+                                       (?:  \\\"         #  (of escaped dquote
+                                           |[^\"])*      #   or anything not a
+                                      )\"                #   literal quote)
                                     \s*                  # optional spaces
                                     (                    # bgroup
                                      \#                  # comment leader