Parsing should raise CatchableError for user inputs (#110)

This commit is contained in:
Mamy Ratsimbazafy 2021-10-06 18:53:52 +02:00 committed by GitHub
parent 484031fdff
commit 06e5c4829c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -216,13 +216,16 @@ func skipPrefixes(current_idx: var int, str: string, radix: range[2..16]) {.inli
doAssert current_idx == 0, "skipPrefixes only works for prefixes (position 0 and 1 of the string)"
if str[0] == '0':
if str[1] in {'x', 'X'}:
doAssert radix == 16, "Parsing mismatch, 0x prefix is only valid for a hexadecimal number (base 16)"
if radix != 16:
raise newException(ValueError,"Parsing mismatch, 0x prefix is only valid for a hexadecimal number (base 16)")
current_idx = 2
elif str[1] in {'o', 'O'}:
doAssert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)"
if radix != 8:
raise newException(ValueError, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)")
current_idx = 2
elif str[1] in {'b', 'B'}:
doAssert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)"
if radix != 2:
raise newException(ValueError, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)")
current_idx = 2
func nextNonBlank(current_idx: var int, s: string) {.inline.} =

View File

@ -1197,6 +1197,12 @@ proc main() =
else:
echo "Next test skipped when Stint forces uint32 backend in test mode"
test "Parsing an unexpected 0x prefix for a decimal string is a CatchableError and not a defect":
let s = "0x123456"
expect(ValueError):
let value = parse(s, StUint[256], 10)
suite "Testing conversion functions: Hex, Bytes, Endianness using secp256k1 curve":
let