changelog: handle decoding of NULs in extra more carefully (issue3156) stable
authorMatt Mackall <mpm@selenic.com>
Fri, 16 Dec 2011 18:23:15 -0600
branchstable
changeset 15661 20ae902c43ec
parent 15658 971c55ce03b8
child 15662 06671371e634
changelog: handle decoding of NULs in extra more carefully (issue3156) Escaped NULs adjacent to [0-7] could be decoded as octal. This hits about 0.24% of changesets with transplant, which stores binary nodes.
mercurial/changelog.py
--- a/mercurial/changelog.py	Thu Dec 15 13:19:43 2011 -0500
+++ b/mercurial/changelog.py	Fri Dec 16 18:23:15 2011 -0600
@@ -24,9 +24,20 @@
     return text.replace('\0', '\\0')
 
 def decodeextra(text):
+    """
+    >>> decodeextra(encodeextra({'foo': 'bar', 'baz': chr(0) + '2'}))
+    {'foo': 'bar', 'baz': '\\x002'}
+    >>> decodeextra(encodeextra({'foo': 'bar', 'baz': chr(92) + chr(0) + '2'}))
+    {'foo': 'bar', 'baz': '\\\\\\x002'}
+    """
     extra = {}
     for l in text.split('\0'):
         if l:
+            if '\\0' in l:
+                # fix up \0 without getting into trouble with \\0
+                l = l.replace('\\\\', '\\\\\n')
+                l = l.replace('\\0', '\0')
+                l = l.replace('\n', '')
             k, v = l.decode('string_escape').split(':', 1)
             extra[k] = v
     return extra