hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 35c64b917aeb39f9b254dc1fb3d9379f66955ef6
parent c0f442d0e509a65e0cfeef267269d5a0cae6d51b
Author: Patrick Widmer <patrick.widmer@tbwnet.ch>
Date:   Fri, 13 May 2022 18:18:35 +0200

encoding::json: fix infinite loop in iterator next

Signed-off-by: Patrick Widmer <patrick.widmer@tbwnet.ch>

Diffstat:
Mencoding/json/+test/value.ha | 15+++++++++++++++
Mencoding/json/value.ha | 4+++-
2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/encoding/json/+test/value.ha b/encoding/json/+test/value.ha @@ -18,3 +18,18 @@ del(&obj, "hello"); assert(get(&obj, "hello") is void); }; + +@test fn iterator() void = { + let obj = newobject(); + defer finish(obj); + + set(&obj, "hello", "world"); + set(&obj, "foo", "bar"); + set(&obj, "the answer", 42.0); + + let it = iter(&obj); + assert(next(&it) is (const str, const *value)); + assert(next(&it) is (const str, const *value)); + assert(next(&it) is (const str, const *value)); + assert(next(&it) is void); +}; diff --git a/encoding/json/value.ha b/encoding/json/value.ha @@ -75,11 +75,13 @@ export fn iter(obj: *object) iterator = { export fn next(iter: *iterator) ((const str, const *value) | void) = { for (iter.i < len(iter.obj.buckets); iter.i += 1) { const bucket = &iter.obj.buckets[iter.i]; - for (iter.j < len(bucket); iter.j += 1) { + for (iter.j < len(bucket)) { const key = bucket[iter.j].0; const val = &bucket[iter.j].1; + iter.j += 1; return (key, val); }; + iter.j = 0; }; };