mercurial/pure/mpatch.py
changeset 28782 f736f98e16ca
parent 28589 c4c7be9f0554
child 28861 86db5cb55d46
--- a/mercurial/pure/mpatch.py	Wed Mar 30 22:01:47 2016 +0000
+++ b/mercurial/pure/mpatch.py	Thu Mar 31 02:05:28 2016 +0000
@@ -12,6 +12,10 @@
 
 StringIO = cStringIO.StringIO
 
+class mpatchError(Exception):
+    """error raised when a delta cannot be decoded
+    """
+
 # This attempts to apply a series of patches in time proportional to
 # the total size of the patches, rather than patches * len(text). This
 # means rather than shuffling strings around, we shuffle around
@@ -84,7 +88,10 @@
         last = 0
         while pos < end:
             m.seek(pos)
-            p1, p2, l = struct.unpack(">lll", m.read(12))
+            try:
+                p1, p2, l = struct.unpack(">lll", m.read(12))
+            except struct.error:
+                raise mpatchError("patch cannot be decoded")
             _pull(new, frags, p1 - last) # what didn't change
             _pull([], frags, p2 - p1)    # what got deleted
             new.append((l, pos + 12))   # what got added
@@ -114,7 +121,7 @@
         outlen += length
 
     if bin != binend:
-        raise ValueError("patch cannot be decoded")
+        raise mpatchError("patch cannot be decoded")
 
     outlen += orig - last
     return outlen