Skip to content

Commit 6eadb6e

Browse files
authored
check length before converting to range type (#102)
The index type of an array is a `range` meaning that converting an out-of-bounds value to it will raise a `Defect`. This PR fixes the defect but does nothing for arrays which are not "full" - probably, this should become an error. * bump version
1 parent 96fcb65 commit 6eadb6e

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

json_serialization.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
mode = ScriptMode.Verbose
1111

1212
packageName = "json_serialization"
13-
version = "0.2.8"
13+
version = "0.2.9"
1414
author = "Status Research & Development GmbH"
1515
description = "Flexible JSON serialization not relying on run-time type information"
1616
license = "Apache License 2.0"

json_serialization/reader_impl.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,11 @@ proc readValue*[T](r: var JsonReader, value: var T)
312312
elif value is array:
313313
type IDX = typeof low(value)
314314
r.parseArray(idx):
315-
let i = IDX(idx + low(value).int)
316-
if i <= high(value):
317-
# TODO: dont's ask. this makes the code compile
318-
if false: value[i] = value[i]
315+
if idx < value.len:
316+
let i = IDX(idx + low(value).int)
319317
readValue(r, value[i])
318+
else:
319+
r.raiseUnexpectedValue("Too many items for " & $(typeof(value)))
320320

321321
elif value is (object or tuple):
322322
mixin flavorUsesAutomaticObjectSerialization

tests/test_reader.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,12 @@ suite "JsonReader basic test":
211211

212212
var z = toReaderNullFields("""{"something":null,"bool":999,"string":100}""")
213213
check execReadObject(z) == 2
214+
215+
test "readValue of array":
216+
var r = toReader "[false, true, false]"
217+
check r.readValue(array[3, bool]) == [false, true, false]
218+
219+
test "readValue of array error":
220+
var r = toReader "[false, true, false]"
221+
expect JsonReaderError:
222+
discard r.readValue(array[2, bool])

0 commit comments

Comments
 (0)