From 8cd9cc11a4f5c2f1879502b67be486b58c2fdcd7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 14 Jul 2012 03:17:27 +0200 Subject: [PATCH] Make Script throw ScriptException instead of ArrayIndexOutOfBounds ...when an invalid PUSHDATA constant is used. --- .../main/java/com/google/bitcoin/core/Script.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Script.java b/core/src/main/java/com/google/bitcoin/core/Script.java index 90278b32..0da13106 100644 --- a/core/src/main/java/com/google/bitcoin/core/Script.java +++ b/core/src/main/java/com/google/bitcoin/core/Script.java @@ -114,6 +114,8 @@ public class Script { private byte[] getData(int len) throws ScriptException { + if (len > program.length - cursor) + throw new ScriptException("Failed read of " + len + " bytes"); try { byte[] buf = new byte[len]; System.arraycopy(program, cursor, buf, 0, len); @@ -123,11 +125,19 @@ public class Script { // We want running out of data in the array to be treated as a handleable script parsing exception, // not something that abnormally terminates the app. throw new ScriptException("Failed read of " + len + " bytes", e); + } catch (NegativeArraySizeException e) { + // We want running out of data in the array to be treated as a handleable script parsing exception, + // not something that abnormally terminates the app. + throw new ScriptException("Failed read of " + len + " bytes", e); } } - private int readByte() { - return 0xFF & program[cursor++]; + private int readByte() throws ScriptException { + try { + return 0xFF & program[cursor++]; + } catch (ArrayIndexOutOfBoundsException e) { + throw new ScriptException("Attempted to read outside of script boundaries"); + } } /**