Reject out-of-range integers instead of silently truncating - #112
Open
youdie006 wants to merge 1 commit into
Open
Reject out-of-range integers instead of silently truncating#112youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
parseHook for unsigned integers accumulated into a uint64 and converted with `type(v)(v2)`, and Nim's unsigned conversions wrap modulo with no range check even in debug, so `"256".fromJson(uint8)` returned 0 and huge numbers wrapped silently. The signed path relied on a `try/except` around the conversion, but Nim's range checks are disabled under -d:release, so over-large values wrapped there too (e.g. `"128".fromJson(int8)` == -128). Range-check both paths explicitly against the target type's bounds (the unsigned check also guards the uint64 accumulation itself), raising the existing "Number type to small to contain the number." error. The signed negative branch handles the extra low(T) value (e.g. int8 reaches -128). Fixes treeform#109
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #109. Parsing an out-of-range integer silently wraps/truncates instead of raising.
Root cause
parseHookfor unsigned integers accumulates digits into auint64and converts withtype(v)(v2). Nim's unsigned conversions wrap modulo with no range check even in debug, so"256".fromJson(uint8)returns0and very large numbers wrap silently.The signed path wrapped the conversion in a
try/exceptto raise"Number type to small to contain the number.", but Nim's range checks are disabled under-d:release/-d:danger, so over-large values wrapped there too. The negative branch had no check at all.The signed path already intends to reject over-large numbers (that error message), so this makes the intent build-mode-independent and extends it to the unsigned path.
Fix
Range-check both paths explicitly against the target type's bounds, raising the existing error:
high(type(v))during accumulation (this also guards theuint64accumulation itself against overflow).try/exceptwith explicit bounds checks; the negative branch handles the extralow(T)value (e.g.int8reaches-128).No API/signature change; one comparison per number on the non-
nimvmpath.Test
Added cases to
tests/test_numbers.nim: out-of-range values (uint8/uint64/int8) now raiseJsonError, and boundary values (255,127,-128,65535,uint64.high) still parse correctly.nim r tests/tests.nimfails atdoAssertRaises(JsonError): discard "256".fromJson(uint8)(nothing raised).Verified locally with the repo's CI command
nim r tests/tests.nim(default debug), which exercises the clean red→green for the unsigned path.Disclosure: this fix was prepared with AI assistance (Claude). I reviewed it, traced the parse path, and verified the red-green test with the repo's own CI command myself.