evm: fix premature position truncation of byte op

This commit is contained in:
jangko 2023-02-13 21:20:52 +07:00
parent 77135e7001
commit 28129d1df2
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 19 additions and 3 deletions

View File

@ -230,11 +230,11 @@ const
byteOp: Vm2OpFn = proc(k: var Vm2Ctx) = byteOp: Vm2OpFn = proc(k: var Vm2Ctx) =
## 0x20, Retrieve single byte from word. ## 0x20, Retrieve single byte from word.
let (position, value) = k.cpt.stack.popInt(2) let (position, value) = k.cpt.stack.popInt(2)
let pos = position.truncate(int)
k.cpt.stack.push: k.cpt.stack.push:
if pos >= 32 or pos < 0: if position >= 32.u256:
zero(UInt256) zero(UInt256)
else: else:
let pos = position.truncate(int)
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
cast[array[32, byte]](value)[pos].u256 cast[array[32, byte]](value)[pos].u256
else: else:

View File

@ -439,5 +439,21 @@ proc opArithMain*() =
SIGNEXTEND SIGNEXTEND
stack: "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307" stack: "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307"
assembler:
title: "BYTE with overflow pos 1"
code:
PUSH32 "0x77676767676760000000000000001002e000000000000040000000e000000000"
PUSH32 "0x0000000000000000000000000000000000000000000000010000000000000000"
BYTE
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler:
title: "BYTE with overflow pos 2"
code:
PUSH32 "0x001f000000000000000000000000000000200000000100000000000000000000"
PUSH32 "0x0000000000000000000000000000000080000000000000000000000000000001"
BYTE
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
when isMainModule: when isMainModule:
opArithMain() opArithMain()