[PATCH STABLE] filelog: don't crash on invalid copy metadata (issue5748)

Gregory Szorc gregory.szorc at gmail.com
Thu Nov 30 21:22:10 EST 2017

# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1512094786 18000
#      Thu Nov 30 21:19:46 2017 -0500
# Branch stable
# Node ID 5f86d95b1bd8aae2c555d2480aa4ac0d2893d35f
# Parent  27196b7fc1acfc78711bc4b721fcc68754354c2c
filelog: don't crash on invalid copy metadata (issue5748)

"copy" and "copyrev" are both supposed to appear next to each other.
However, a user report demonstrated a crash that indicates that
something in the wild is producing "copy" without "copyrev"
(probably `hg convert`).

While we should definitely fix the source of the bad metadata,
the bad code causing the crash is already in the wild and who knows
how many repositories are impacted. So let's be more defensive
when accessing the file revision metadata.

diff --git a/mercurial/filelog.py b/mercurial/filelog.py
--- a/mercurial/filelog.py
+++ b/mercurial/filelog.py
@@ -60,10 +60,17 @@ class filelog(revlog.revlog):
         if self.parents(node)[0] != revlog.nullid:
             return False
         t = self.revision(node)
         m = parsemeta(t)[0]
-        if m and "copy" in m:
-            return (m["copy"], revlog.bin(m["copyrev"]))
+        if m:
+            # copy and copyrev occur in pairs. In rare cases due to bugs,
+            # one can occur without the other.
+            try:
+                copy, copyrev = m['copy'], m['copyrev']
+                return copy, revlog.bin(copyrev)
+            except KeyError:
+                pass
         return False
     def size(self, rev):
         """return the size of a given revision"""

More information about the Mercurial-devel mailing list