tfix BCDataStream.read_bytes (#5991) - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit c0be0471f22f71ae30ff6e1983c1a958c83c5eef (DIR) parent aaf174ef3efb7744a48bcacd805618c1523766f3 (HTM) Author: zebra-lucky <zebra.lucky@gmail.com> Date: Tue, 25 Feb 2020 20:58:03 +0200 fix BCDataStream.read_bytes (#5991) * fix BCDataStream.read_bytes * followup fix BCDataStream.read_bytes: fix TestBCDataStream.test_bytes Diffstat: M electrum/tests/test_transaction.py | 8 ++++++-- M electrum/transaction.py | 12 ++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) --- (DIR) diff --git a/electrum/tests/test_transaction.py b/electrum/tests/test_transaction.py t@@ -58,8 +58,12 @@ class TestBCDataStream(ElectrumTestCase): s.write(b'foobar') self.assertEqual(s.read_bytes(3), b'foo') self.assertEqual(s.read_bytes(2), b'ba') - self.assertEqual(s.read_bytes(4), b'r') - self.assertEqual(s.read_bytes(1), b'') + with self.assertRaises(transaction.SerializationError): + s.read_bytes(4) + self.assertEqual(s.read_bytes(0), b'') + self.assertEqual(s.read_bytes(1), b'r') + self.assertEqual(s.read_bytes(0), b'') + class TestTransaction(ElectrumTestCase): (DIR) diff --git a/electrum/transaction.py b/electrum/transaction.py t@@ -272,12 +272,16 @@ class BCDataStream(object): self.write(string) def read_bytes(self, length) -> bytes: - try: - result = self.input[self.read_cursor:self.read_cursor+length] # type: bytearray + assert length >= 0 + input_len = len(self.input) + read_begin = self.read_cursor + read_end = read_begin + length + if 0 <= read_begin <= input_len and read_end <= input_len: + result = self.input[read_begin:read_end] # type: bytearray self.read_cursor += length return bytes(result) - except IndexError: - raise SerializationError("attempt to read past end of buffer") from None + else: + raise SerializationError('attempt to read past end of buffer') def can_read_more(self) -> bool: if not self.input: