diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/DataWord.java b/ethereumj-core/src/main/java/org/ethereum/vm/DataWord.java index acceac26..33e8de44 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/DataWord.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/DataWord.java @@ -295,7 +295,7 @@ public class DataWord implements Comparable { } public void signExtend(byte k) { - if (k > 31) + if (0 > k || k > 31) throw new IndexOutOfBoundsException(); byte mask = this.sValue().testBit((k * 8) + 7) ? (byte) 0xff : 0; for (int i = 31; i > k; i--) { diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/DataWordTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/DataWordTest.java index 5373a0b4..3a8a7063 100644 --- a/ethereumj-core/src/test/java/org/ethereum/vm/DataWordTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/vm/DataWordTest.java @@ -216,6 +216,36 @@ public class DataWordTest { System.out.println(x.toString()); assertEquals(expected, x.toString()); } + + @Test + public void testSignExtend8() { + + byte k = 30; + DataWord x = new DataWord(Hex.decode("ff34567882345678823456788234567882345678823456788234567882345678")); + String expected = "0034567882345678823456788234567882345678823456788234567882345678"; + + x.signExtend(k); + System.out.println(x.toString()); + assertEquals(expected, x.toString()); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testSignExtendException1() { + + byte k = -1; + DataWord x = new DataWord(); + + x.signExtend(k); // should throw an exception + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testSignExtendException2() { + + byte k = 32; + DataWord x = new DataWord(); + + x.signExtend(k); // should throw an exception + } public static BigInteger pow(BigInteger x, BigInteger y) { if (y.compareTo(BigInteger.ZERO) < 0)