changegroup: don't accept odd chunk headers stable
authorMads Kiilerich <mads@kiilerich.com>
Tue, 22 Feb 2011 03:10:37 +0100
branchstable
changeset 13458 9f2c407caf34
parent 13457 e74fe15dc7fd
child 13459 acbe171c8fbe
changegroup: don't accept odd chunk headers
mercurial/changegroup.py
--- a/mercurial/changegroup.py	Tue Feb 22 03:03:39 2011 +0100
+++ b/mercurial/changegroup.py	Tue Feb 22 03:10:37 2011 +0100
@@ -23,6 +23,8 @@
     d = readexactly(stream, 4)
     l = struct.unpack(">l", d)[0]
     if l <= 4:
+        if l:
+            raise util.Abort(_("invalid chunk length %d") % l)
         return ""
     return readexactly(stream, l - 4)
 
@@ -149,11 +151,15 @@
         return self._stream.close()
 
     def chunklength(self):
-        d = readexactly(self._stream, 4)
-        l = max(0, struct.unpack(">l", d)[0] - 4)
-        if l and self.callback:
+        d = readexactly(stream, 4)
+        l = struct.unpack(">l", d)[0]
+        if l <= 4:
+            if l:
+                raise util.Abort(_("invalid chunk length %d") % l)
+            return 0
+        if self.callback:
             self.callback()
-        return l
+        return l - 4
 
     def chunk(self):
         """return the next chunk from changegroup 'source' as a string"""