Wireless Daemon for Linux
 help / color / mirror / Atom feed
* [PATCH 4/5] json: restrict json_iter_{parse,next} to objects/arrays
@ 2022-01-06 22:42 James Prestwood
  0 siblings, 0 replies; only message in thread
From: James Prestwood @ 2022-01-06 22:42 UTC (permalink / raw)
  To: iwd 

[-- Attachment #1: Type: text/plain, Size: 994 bytes --]

json_iter_parse is only meant to work on objects while
json_iter_next is only meant to work on arrays.

This adds checks in both APIs to ensure they aren't being
used incorrectly.
---
 src/json.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/json.c b/src/json.c
index 0942dae3..869a3f43 100644
--- a/src/json.c
+++ b/src/json.c
@@ -200,6 +200,9 @@ bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)
 	if (iter->start == -1)
 		return false;
 
+	if (c->tokens[iter->start].type != JSMN_OBJECT)
+		return false;
+
 	args = l_queue_new();
 
 	va_start(va, type);
@@ -427,6 +430,9 @@ bool json_iter_next(struct json_iter *iter)
 	jsmntok_t *t = c->tokens + iter->current;
 	int inc = 1;
 
+	if (iter->current == iter->start && t->type != JSMN_ARRAY)
+		return false;
+
 	/*
 	 * If this is the initial iteration skip this and just increment
 	 * current by 1 since this iterator points to a container which needs to
-- 
2.31.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-06 22:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-06 22:42 [PATCH 4/5] json: restrict json_iter_{parse,next} to objects/arrays James Prestwood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox