bundle2: safely read unpack data from part header
authorPierre-Yves David <pierre-yves.david@fb.com>
Tue, 01 Apr 2014 00:08:15 -0700
changeset 20887 662b79be093c
parent 20886 203908968644
child 20888 0b0d3800c248
bundle2: safely read unpack data from part header We use the same approach that the other unpack, as function is given the struct format and his both responsible for reading the right amount of data from the header and unpack the struct. This give use flexibility if we decide to change the size of something in the format before the release.
mercurial/bundle2.py
--- a/mercurial/bundle2.py	Wed Apr 02 13:41:23 2014 -0500
+++ b/mercurial/bundle2.py	Tue Apr 01 00:08:15 2014 -0700
@@ -292,16 +292,22 @@
             data = headerblock[offset:(offset + size)]
             self._offset = offset + size
             return data
-        typesize = _unpack(_fparttypesize, fromheader(1))[0]
+        def unpackheader(format):
+            """read given format from header
+
+            This automatically compute the size of the format to read."""
+            data = fromheader(struct.calcsize(format))
+            return _unpack(format, data)
+
+        typesize = unpackheader(_fparttypesize)[0]
         parttype = fromheader(typesize)
         self.ui.debug('part type: "%s"\n' % parttype)
         ## reading parameters
         # param count
-        mancount, advcount = _unpack(_fpartparamcount, fromheader(2))
+        mancount, advcount = unpackheader(_fpartparamcount)
         self.ui.debug('part parameters: %i\n' % (mancount + advcount))
         # param size
-        paramsizes = _unpack(_makefpartparamsizes(mancount + advcount),
-                             fromheader(2*(mancount + advcount)))
+        paramsizes = unpackheader(_makefpartparamsizes(mancount + advcount))
         # make it a list of couple again
         paramsizes = zip(paramsizes[::2], paramsizes[1::2])
         # split mandatory from advisory