Fix overflows and add + activate tests

This commit is contained in:
kdeme 2019-05-30 17:33:59 +02:00 committed by zah
parent 88b3b949a8
commit d2d6d7fc16
4 changed files with 54 additions and 5 deletions

View File

@ -107,7 +107,9 @@ proc payloadOffset(self: Rlp): int =
if isSingleByte(): 0 else: 1 + lengthBytesCount() if isSingleByte(): 0 else: 1 + lengthBytesCount()
template readAheadCheck(numberOfBytes) = template readAheadCheck(numberOfBytes) =
if position + numberOfBytes >= bytes.len: eosError() # important to add nothing to the left side of the equation as `numberOfBytes`
# can in theory be at max size of its type already
if numberOfBytes >= bytes.len - position: eosError()
template nonCanonicalNumberError = template nonCanonicalNumberError =
raise newException(MalformedRlpError, "Small number encoded in a non-canonical way") raise newException(MalformedRlpError, "Small number encoded in a non-canonical way")
@ -138,7 +140,9 @@ proc payloadBytesCount(self: Rlp): int =
if remainingBytes > 1 and self.bytes[self.position + 1] == 0: if remainingBytes > 1 and self.bytes[self.position + 1] == 0:
raise newException(MalformedRlpError, "Number encoded with a leading zero") raise newException(MalformedRlpError, "Number encoded with a leading zero")
if lengthBytes > sizeof(result): # check if the size is not bigger than the max that result can hold
if lengthBytes > sizeof(result) or
(lengthBytes == sizeof(result) and self.bytes[self.position + 1].int > 127):
raise newException(UnsupportedRlpError, "Message too large to fit in memory") raise newException(UnsupportedRlpError, "Message too large to fit in memory")
for i in 1 .. lengthBytes: for i in 1 .. lengthBytes:

View File

@ -42,5 +42,46 @@
"bytesShouldBeSingleByte7F": { "bytesShouldBeSingleByte7F": {
"in": "INVALID", "in": "INVALID",
"out": "817F" "out": "817F"
},
"fakelonglist": {
"in": "INVALID",
"out": "ff7fffffffffffffffff"
},
"fakelonglist2": {
"in": "INVALID",
"out": "ff800000000000000000"
},
"fakelongblob": {
"in": "INVALID",
"out": "bf7fffffffffffffffff"
},
"fakelongblob2": {
"in": "INVALID",
"out": "bf800000000000000000"
},
"fakelongblobinlist": {
"in": "INVALID",
"out": "f2bf7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
},
"fakelongblobinlist2": {
"in": "INVALID",
"out": "f2bf80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},
"fakelonglistinlist": {
"in": "INVALID",
"out": "f2ff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
},
"fakelonglistinlist2": {
"in": "INVALID",
"out": "f2ff80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
} }
} }

View File

@ -1,8 +1,12 @@
import import
os, strutils, os, strutils, strformat,
util/json_testing util/json_testing
for file in walkDirRec("tests/cases"): template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
const casesDir = &"{sourceDir}{DirSep}cases{DirSep}"
for file in walkDirRec(casesDir):
if file.endsWith("json"): if file.endsWith("json"):
runTests(file) runTests(file)

View File

@ -48,7 +48,7 @@ proc runTests*(filename: string) =
inspectOutput = rlp.inspect(1) inspectOutput = rlp.inspect(1)
discard rlp.getType discard rlp.getType
while rlp.hasData: discard rlp.toNodes while rlp.hasData: discard rlp.toNodes
except MalformedRlpError, ValueError: except MalformedRlpError, UnsupportedRlpError, ValueError:
success = true success = true
if not success: if not success:
testStatus "FAILED" testStatus "FAILED"