mirror of https://github.com/status-im/nim-eth.git
Check range on deserialising enum
This commit is contained in:
parent
1756f7b41f
commit
40b96e2d3c
|
@ -304,7 +304,11 @@ proc readImpl(rlp: var Rlp, T: type Integer): Integer =
|
||||||
rlp.skipElem
|
rlp.skipElem
|
||||||
|
|
||||||
proc readImpl(rlp: var Rlp, T: type[enum]): T =
|
proc readImpl(rlp: var Rlp, T: type[enum]): T =
|
||||||
result = type(result)(rlp.toInt(int))
|
let value = rlp.toInt(int)
|
||||||
|
if value < ord(T.low) or value > ord(T.high):
|
||||||
|
raise newException(RlpTypeMismatch,
|
||||||
|
"Enum expected, but the source RLP is not in valid range.")
|
||||||
|
result = type(result)(value)
|
||||||
rlp.skipElem
|
rlp.skipElem
|
||||||
|
|
||||||
proc readImpl(rlp: var Rlp, T: type bool): T =
|
proc readImpl(rlp: var Rlp, T: type bool): T =
|
||||||
|
|
|
@ -194,3 +194,19 @@ test "encode/decode floats":
|
||||||
chk f
|
chk f
|
||||||
chk -f
|
chk -f
|
||||||
|
|
||||||
|
test "invalid enum":
|
||||||
|
type
|
||||||
|
MyEnum = enum
|
||||||
|
foo,
|
||||||
|
bar
|
||||||
|
|
||||||
|
var writer = initRlpWriter()
|
||||||
|
writer.append(2)
|
||||||
|
writer.append(-1)
|
||||||
|
let bytes = writer.finish()
|
||||||
|
var rlp = rlpFromBytes(bytes.toRange)
|
||||||
|
expect RlpTypeMismatch:
|
||||||
|
discard rlp.read(MyEnum)
|
||||||
|
rlp.skipElem()
|
||||||
|
expect RlpTypeMismatch:
|
||||||
|
discard rlp.read(MyEnum)
|
||||||
|
|
Loading…
Reference in New Issue