avoid invalid codegen/UB by avoiding case object discriminant assignment (#566)

This commit is contained in:
tersec 2022-12-01 12:55:42 +00:00 committed by GitHub
parent 833818e9c7
commit 571b98d7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -244,19 +244,19 @@ func toBlockNonce*(n: uint64): BlockNonce =
func toUint*(n: BlockNonce): uint64 = func toUint*(n: BlockNonce): uint64 =
uint64.fromBytesBE(n) uint64.fromBytesBE(n)
proc newAccount*(nonce: AccountNonce = 0, balance: UInt256 = 0.u256): Account = func newAccount*(nonce: AccountNonce = 0, balance: UInt256 = 0.u256): Account =
result.nonce = nonce result.nonce = nonce
result.balance = balance result.balance = balance
result.storageRoot = EMPTY_ROOT_HASH result.storageRoot = EMPTY_ROOT_HASH
result.codeHash = EMPTY_CODE_HASH result.codeHash = EMPTY_CODE_HASH
proc hasStatus*(rec: Receipt): bool {.inline.} = func hasStatus*(rec: Receipt): bool {.inline.} =
rec.isHash == false rec.isHash == false
proc hasStateRoot*(rec: Receipt): bool {.inline.} = func hasStateRoot*(rec: Receipt): bool {.inline.} =
rec.isHash == true rec.isHash == true
proc stateRoot*(rec: Receipt): Hash256 {.inline.} = func stateRoot*(rec: Receipt): Hash256 {.inline.} =
doAssert(rec.hasStateRoot) doAssert(rec.hasStateRoot)
rec.hash rec.hash
@ -272,14 +272,14 @@ func destination*(tx: Transaction): EthAddress =
func init*(T: type BlockHashOrNumber, str: string): T func init*(T: type BlockHashOrNumber, str: string): T
{.raises: [ValueError, Defect].} = {.raises: [ValueError, Defect].} =
if str.startsWith "0x": if str.startsWith "0x":
if str.len != sizeof(result.hash.data) * 2 + 2: if str.len != sizeof(default(T).hash.data) * 2 + 2:
raise newException(ValueError, "Block hash has incorrect length") raise newException(ValueError, "Block hash has incorrect length")
result.isHash = true var res = T(isHash: true)
hexToByteArray(str, result.hash.data) hexToByteArray(str, res.hash.data)
res
else: else:
result.isHash = false T(isHash: false, number: parseBiggestUInt str)
result.number = parseBiggestUInt str
func `$`*(x: BlockHashOrNumber): string = func `$`*(x: BlockHashOrNumber): string =
if x.isHash: if x.isHash: