diff options
author | Jake Leahy <jake@leahy.dev> | 2022-10-24 20:17:14 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 17:17:14 +0800 |
commit | d261135c5cdd9fe90f4c6cd21fffded8949ef1c9 (patch) | |
tree | ef66a252d3681855a014bd7453f0b39707ac6de4 /lib/std | |
parent | 98b2838a3084649a13f38f60df3f349530fb4907 (diff) | |
download | Nim-d261135c5cdd9fe90f4c6cd21fffded8949ef1c9.tar.gz |
Fix tuple size check in `std/jsonutils` (#20637)
* Add test for tuple being invalid size * Test tuple size before accessing fields * Fix formatting for import * Fix not being able to build from csources_v1 Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/std')
-rw-r--r-- | lib/std/jsonutils.nim | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim index 9db1106c4..05f9f661c 100644 --- a/lib/std/jsonutils.nim +++ b/lib/std/jsonutils.nim @@ -32,7 +32,7 @@ add a way to customize serialization, for e.g.: import macros from enumutils import symbolName -from typetraits import OrdinalEnum +from typetraits import OrdinalEnum, tupleLen when defined(nimPreviewSlimSystem): import std/assertions @@ -286,11 +286,21 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) = fromJsonFields(a, nil, b, seq[string].default, opt) else: checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull + + when compiles(tupleLen(T)): + let tupleSize = tupleLen(T) + else: + # Tuple len isn't in csources_v1 so using tupleLen would fail. + # Else branch basically never runs (tupleLen added in 1.1 and jsonutils in 1.4), but here for consistency + var tupleSize = 0 + for val in fields(a): + tupleSize.inc + + checkJson b.len == tupleSize, $(b.len, tupleSize, $T, b) # could customize var i = 0 for val in fields(a): fromJson(val, b[i], opt) i.inc - checkJson b.len == i, $(b.len, i, $T, b) # could customize else: # checkJson not appropriate here static: doAssert false, "not yet implemented: " & $T |