From b28f8b3ebd7f00eb69216e1a7ea4c6ae241ee519 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 6 Nov 2017 12:27:47 +0100 Subject: [PATCH] make tx deserialization more robust to ill-formed tx --- gui/qt/main_window.py | 20 ++++++++++++++------ lib/transaction.py | 33 ++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index c2cfb06e..1cfd6e67 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -2119,17 +2119,25 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): return self.tx_from_text(file_content) def do_process_from_text(self): + from electrum.transaction import SerializationError text = text_dialog(self, _('Input raw transaction'), _("Transaction:"), _("Load transaction")) if not text: return - tx = self.tx_from_text(text) - if tx: - self.show_transaction(tx) + try: + tx = self.tx_from_text(text) + if tx: + self.show_transaction(tx) + except SerializationError as e: + self.show_critical(_("Electrum was unable to deserialize the transaction:") + "\n" + str(e)) def do_process_from_file(self): - tx = self.read_tx_from_file() - if tx: - self.show_transaction(tx) + from electrum.transaction import SerializationError + try: + tx = self.read_tx_from_file() + if tx: + self.show_transaction(tx) + except SerializationError as e: + self.show_critical(_("Electrum was unable to deserialize the transaction:") + "\n" + str(e)) def do_process_from_txid(self): from electrum import transaction diff --git a/lib/transaction.py b/lib/transaction.py index b5094738..eec28744 100644 --- a/lib/transaction.py +++ b/lib/transaction.py @@ -77,10 +77,7 @@ class BCDataStream(object): if self.input is None: raise SerializationError("call write(bytes) before trying to deserialize") - try: - length = self.read_compact_size() - except IndexError: - raise SerializationError("attempt to read past end of buffer") + length = self.read_compact_size() return self.read_bytes(length).decode(encoding) @@ -117,15 +114,18 @@ class BCDataStream(object): def write_uint64(self, val): return self._write_num('