From 35ce90eec969732839b2a20a564c617b7608a31e Mon Sep 17 00:00:00 2001 From: Deniod Date: Fri, 1 Dec 2023 08:57:19 +0100 Subject: [PATCH] prevent unwrap(), poisoned data --- lib/src/lightwallet.rs | 45 +++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/src/lightwallet.rs b/lib/src/lightwallet.rs index 3271a95..76d7f17 100644 --- a/lib/src/lightwallet.rs +++ b/lib/src/lightwallet.rs @@ -1470,26 +1470,43 @@ pub fn scan_full_tx(&self, tx: &Transaction, height: i32, datetime: u64) { // Do it in a short scope because of the write lock. { info!("A sapling output was sent in {}", tx.txid()); - let mut txs = self.txs.write().unwrap(); - if txs.get(&tx.txid()).unwrap().outgoing_metadata.iter() - .find(|om| om.address == address && om.value == note.value && om.memo == memo) - .is_some() { - warn!("Duplicate outgoing metadata"); - continue; - } + match self.txs.write() { + Ok(mut txs) => { + match txs.get(&tx.txid()) { + Some(wtx) => { + if wtx.outgoing_metadata.iter() + .any(|om| om.address == address && om.value == note.value && om.memo == memo) + { + warn!("Duplicate outgoing metadata"); + continue; + } - // Write the outgoing metadata - txs.get_mut(&tx.txid()).unwrap() - .outgoing_metadata - .push(OutgoingTxMetadata{ - address, value: note.value, memo, - }); + // Write the outgoing metadata + txs.get_mut(&tx.txid()).unwrap() + .outgoing_metadata + .push(OutgoingTxMetadata { + address, + value: note.value, + memo, + }); + }, + None => { + error!("Can not find any entry for txid : {}", tx.txid()); + continue; + } + } + }, + Err(poisoned) => { + error!("Lock is poisoned: {}", poisoned); + return; + } + } } }, None => {} }; } - } + } // Mark this Tx as scanned { let mut txs = self.txs.write().unwrap();