diff --git a/stint/io.nim b/stint/io.nim index 3359324..d1f98cf 100644 --- a/stint/io.nim +++ b/stint/io.nim @@ -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.} = diff --git a/tests/test_io.nim b/tests/test_io.nim index 93daddb..48ca288 100644 --- a/tests/test_io.nim +++ b/tests/test_io.nim @@ -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