obsolete: explode if metadata contains invalid UTF-8 sequence (API)
authorYuya Nishihara <yuya@tcha.org>
Sun, 15 Jul 2018 18:32:17 +0900
changeset 38708 ff1182d166a2
parent 38707 6b5ca1d0aa1e
child 38709 6a032a8f741b
obsolete: explode if metadata contains invalid UTF-8 sequence (API) The current metadata API can be a source of bugs since it forces callers to process encoding conversion by themselves. So let's make it reject bad data as a last ditch. I assume there's no metadata field which is supposed to store arbitrary BLOB like transplant_source.
mercurial/obsolete.py
--- a/mercurial/obsolete.py	Sun Jul 15 18:24:57 2018 +0900
+++ b/mercurial/obsolete.py	Sun Jul 15 18:32:17 2018 +0900
@@ -80,6 +80,7 @@
     obsutil,
     phases,
     policy,
+    pycompat,
     util,
 )
 from .utils import dateutil
@@ -600,6 +601,16 @@
             raise ValueError(_('in-marker cycle with %s') % node.hex(prec))
 
         metadata = tuple(sorted(metadata.iteritems()))
+        for k, v in metadata:
+            try:
+                # might be better to reject non-ASCII keys
+                k.decode('utf-8')
+                v.decode('utf-8')
+            except UnicodeDecodeError:
+                raise error.ProgrammingError(
+                    'obsstore metadata must be valid UTF-8 sequence '
+                    '(key = %r, value = %r)'
+                    % (pycompat.bytestr(k), pycompat.bytestr(v)))
 
         marker = (bytes(prec), tuple(succs), int(flag), metadata, date, parents)
         return bool(self.add(transaction, [marker]))