~sircmpwn/public-inbox

hare: encoding::json: fix infinite loop in iterator next v4 APPLIED

Patrick Widmer: 1
 encoding::json: fix infinite loop in iterator next

 2 files changed, 18 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/public-inbox/patches/32241/mbox | git am -3
Learn more about email & git

[PATCH hare v4] encoding::json: fix infinite loop in iterator next Export this patch

Signed-off-by: Patrick Widmer <patrick.widmer@tbwnet.ch>
---
Improve test case by using object containing multiple pairs

 encoding/json/+test/value.ha | 15 +++++++++++++++
 encoding/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
index 79e08dea..41127148 100644
--- 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
index 486be438..8fd1f642 100644
--- 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;
	};
};

-- 
2.36.1
Thanks!

To git@git.sr.ht:~sircmpwn/hare
   a0ecfc62..1ecd1920  master -> master