@@ -183,7 +183,7 @@ class OpCodeFunSpec extends FunSuite with OpCodeTesting with Matchers with Prope
183
183
withStackVerification(op, stateIn, stateOut) {
184
184
val (offset, _) = stateIn.stack.pop
185
185
val (data, _) = stateOut.stack.pop
186
- data shouldEqual UInt256 (OpCode .sliceBytes(stateIn.inputData, offset.toInt , 32 ))
186
+ data shouldEqual UInt256 (OpCode .sliceBytes(stateIn.inputData, offset, 32 ))
187
187
188
188
val expectedState = stateIn.withStack(stateOut.stack).step()
189
189
stateOut shouldEqual expectedState
@@ -203,7 +203,7 @@ class OpCodeFunSpec extends FunSuite with OpCodeTesting with Matchers with Prope
203
203
204
204
withStackVerification(op, stateIn, stateOut) {
205
205
val (Seq (memOffset, dataOffset, size), _) = stateIn.stack.pop(3 )
206
- val data = OpCode .sliceBytes(stateIn.inputData, dataOffset.toInt , size.toInt )
206
+ val data = OpCode .sliceBytes(stateIn.inputData, dataOffset, size)
207
207
val (storedInMem, _) = stateOut.memory.load(memOffset, size)
208
208
data shouldEqual storedInMem
209
209
@@ -225,7 +225,7 @@ class OpCodeFunSpec extends FunSuite with OpCodeTesting with Matchers with Prope
225
225
226
226
withStackVerification(op, stateIn, stateOut) {
227
227
val (Seq (memOffset, codeOffset, size), _) = stateIn.stack.pop(3 )
228
- val code = stateIn.program.getBytes( codeOffset.toInt , size.toInt )
228
+ val code = OpCode .sliceBytes( stateIn.program.code, codeOffset, size)
229
229
val (storedInMem, _) = stateOut.memory.load(memOffset, size)
230
230
code shouldEqual storedInMem
231
231
@@ -284,7 +284,7 @@ class OpCodeFunSpec extends FunSuite with OpCodeTesting with Matchers with Prope
284
284
285
285
withStackVerification(op, stateIn, stateOut) {
286
286
val (Seq (addr, memOffset, codeOffset, size), _) = stateIn.stack.pop(4 )
287
- val code = OpCode .sliceBytes(stateIn.world.getCode(Address (addr)), codeOffset.toInt , size.toInt )
287
+ val code = OpCode .sliceBytes(stateIn.world.getCode(Address (addr)), codeOffset, size)
288
288
val (storedInMem, _) = stateOut.memory.load(memOffset, size)
289
289
code shouldEqual storedInMem
290
290
@@ -670,5 +670,46 @@ class OpCodeFunSpec extends FunSuite with OpCodeTesting with Matchers with Prope
670
670
671
671
verifyAllOpCodesRegistered(except = CREATE , CALL , CALLCODE , DELEGATECALL )
672
672
673
+ test(" sliceBytes helper" ) {
674
+ def zeroes (i : Int ): ByteString =
675
+ ByteString (Array .fill[Byte ](i)(0 ))
676
+
677
+ val table = Table [Int , UInt256 , UInt256 , Int , ByteString => ByteString ](
678
+ (" dataSize" , " sliceOffset" , " sliceSize" , " expectedSize" , " expectedContentFn" ),
679
+
680
+ // both offset and size are greater than data size
681
+ (0 , 16 , 32 , 32 , _ => zeroes(32 )),
682
+
683
+ // offset is within bounds, offset + size is greater than data size
684
+ (20 , 16 , 32 , 32 , bs => bs.drop(16 ) ++ zeroes(28 )),
685
+
686
+ // offset + size are within bounds
687
+ (64 , 16 , 31 , 31 , bs => bs.slice(16 , 47 )),
688
+
689
+ // offset is greater than Int.MaxValue
690
+ (64 , Two ** 128 , 32 , 32 , _ => zeroes(32 )),
691
+
692
+ // offset is within bounds, size is greater than Int.MaxValue
693
+ (64 , 16 , Two ** 64 + 7 , 48 , bs => bs.drop(16 )),
694
+
695
+ // offset is within bounds, size is greater than Int.MaxValue and size.toInt > dataSize
696
+ // this case a bit strange because we purposefully let size overflow when converting to Int
697
+ // but sliceBytes is supposed to copy the behaviour of geth:
698
+ // https://github.com/ethereum/go-ethereum/blob/5f7826270c9e87509fd7731ec64953a5e4761de0/core/vm/common.go#L42
699
+ (64 , 40 , Two ** 64 + 124 , 124 , bs => bs.drop(40 ) ++ zeroes(100 )),
700
+
701
+ // both offset and size are greater than Int.MaxValue
702
+ (64 , Two ** 33 , Two ** 96 + 13 , 13 , _ => zeroes(13 ))
703
+ )
704
+
705
+ forAll(table) { (dataSize, sliceOffset, sliceSize, expectedSize, expectedContentFn) =>
706
+ val bytes = getByteStringGen(dataSize, dataSize).sample.get
707
+ val slice = OpCode .sliceBytes(bytes, sliceOffset, sliceSize)
708
+
709
+ slice.size shouldEqual expectedSize
710
+ slice shouldEqual expectedContentFn(bytes)
711
+ }
712
+ }
713
+
673
714
}
674
715
0 commit comments