diff -r 589eed45a452 -r 1fb0846ad792 mercurial/bundle2.py --- a/mercurial/bundle2.py Mon Nov 13 22:05:54 2017 -0800 +++ b/mercurial/bundle2.py Mon Nov 13 21:48:35 2017 -0800 @@ -1198,13 +1198,21 @@ headersize = struct.calcsize(_fpayloadsize) readexactly = changegroup.readexactly + read = fh.read chunksize = _unpack(_fpayloadsize, readexactly(fh, headersize))[0] indebug(ui, 'payload chunk size: %i' % chunksize) + # changegroup.readexactly() is inlined below for performance. while chunksize: if chunksize >= 0: - yield readexactly(fh, chunksize) + s = read(chunksize) + if len(s) < chunksize: + raise error.Abort(_('stream ended unexpectedly ' + ' (got %d bytes, expected %d)') % + (len(s), chunksize)) + + yield s elif chunksize == flaginterrupt: # Interrupt "signal" detected. The regular stream is interrupted # and a bundle2 part follows. Consume it. @@ -1213,7 +1221,13 @@ raise error.BundleValueError( 'negative payload chunk size: %s' % chunksize) - chunksize = _unpack(_fpayloadsize, readexactly(fh, headersize))[0] + s = read(headersize) + if len(s) < headersize: + raise error.Abort(_('stream ended unexpectedly ' + ' (got %d bytes, expected %d)') % + (len(s), chunksize)) + + chunksize = _unpack(_fpayloadsize, s)[0] # indebug() inlined for performance. if dolog: