summaryrefslogtreecommitdiff
path: root/json_util.c
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2019-11-10 00:12:27 -0500
committerEric Haszlakiewicz <erh+git@nimenees.com>2019-11-10 00:14:44 -0500
commitbaed9983b30b46cf6685a061632bbc42cec0ce8d (patch)
tree064180d4b9fc0701e560b6ab909bd1af877ca667 /json_util.c
parentac26ea9c5bb42bd19aa466acea830a90435d65de (diff)
downloadjson-c-baed9983b30b46cf6685a061632bbc42cec0ce8d.tar.gz
Add a json_object_from_fd_ex() function, to allow the max nesting depth to be specified.
Diffstat (limited to 'json_util.c')
-rw-r--r--json_util.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/json_util.c b/json_util.c
index ad7704a..a72ab41 100644
--- a/json_util.c
+++ b/json_util.c
@@ -83,6 +83,10 @@ void _json_c_set_last_err(const char *err_fmt, ...)
struct json_object* json_object_from_fd(int fd)
{
+ return json_object_from_fd_ex(fd, -1);
+}
+struct json_object* json_object_from_fd_ex(int fd, int in_depth)
+{
struct printbuf *pb;
struct json_object *obj;
char buf[JSON_FILE_BUF_SIZE];
@@ -92,17 +96,35 @@ struct json_object* json_object_from_fd(int fd)
_json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
return NULL;
}
+
+ int depth = JSON_TOKENER_DEFAULT_DEPTH;
+ if (in_depth != -1)
+ depth = in_depth;
+ json_tokener *tok = json_tokener_new_ex(depth);
+ if (!tok)
+ {
+ _json_c_set_last_err("json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth, strerror(errno));
+ printbuf_free(pb);
+ return NULL;
+ }
+
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
printbuf_memappend(pb, buf, ret);
}
if(ret < 0) {
_json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
+ json_tokener_free(tok);
printbuf_free(pb);
return NULL;
}
- obj = json_tokener_parse(pb->buf);
- printbuf_free(pb);
- return obj;
+
+ obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
+ if (obj == NULL)
+ _json_c_set_last_err("json_tokener_parse_ex failed: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
+
+ json_tokener_free(tok);
+ printbuf_free(pb);
+ return obj;
}
struct json_object* json_object_from_file(const char *filename)