changeset 20887:662b79be093c

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.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Tue, 01 Apr 2014 00:08:15 -0700
parents 203908968644
children 0b0d3800c248
files mercurial/bundle2.py
diffstat 1 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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