Check range on deserialising enum

This commit is contained in:
kdeme 2020-02-04 11:15:01 +01:00
parent 1756f7b41f
commit 40b96e2d3c
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
2 changed files with 21 additions and 1 deletions

View File

@ -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 =

View File

@ -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)