From 1f7dd9495e063de8036c5d61087ed070e452253a Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 7 Oct 2013 18:09:38 +0200 Subject: [PATCH] Script: clone tx before performing correctlySpends check. This prevents thread safety issues and corrupted transactions if validation fails. --- core/src/main/java/com/google/bitcoin/script/Script.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/main/java/com/google/bitcoin/script/Script.java b/core/src/main/java/com/google/bitcoin/script/Script.java index a5f2c17e..26cc3efe 100644 --- a/core/src/main/java/com/google/bitcoin/script/Script.java +++ b/core/src/main/java/com/google/bitcoin/script/Script.java @@ -1168,6 +1168,13 @@ public class Script { */ public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, boolean enforceP2SH) throws ScriptException { + // Clone the transaction because executing the script involves editing it, and if we die, we'll leave + // the tx half broken (also it's not so thread safe to work on it directly. + try { + txContainingThis = new Transaction(txContainingThis.getParams(), txContainingThis.bitcoinSerialize()); + } catch (ProtocolException e) { + throw new RuntimeException(e); // Should not happen unless we were given a totally broken transaction. + } if (getProgram().length > 10000 || scriptPubKey.getProgram().length > 10000) throw new ScriptException("Script larger than 10,000 bytes");