diff options
author | Yousong Zhou <yszhou4tech@gmail.com> | 2014-11-12 16:59:20 +0300 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2014-12-11 19:58:42 +0300 |
commit | 8c6dadbe038509f8428966c03bcf6be83d832904 (patch) | |
tree | 14f2be344bd1aff2a207c02b4de6a384736a8b1d /jshn.c | |
parent | 7f1ce63a84a4c0c3cd1c44ec49a758190e5e1e79 (diff) |
jshn: add error handling and fix memory leak in jshn_format().
Though currently jshn is more a one-shot data transformation tool and
won't leak much memory in its lifetime, people may use it as example
code, so do it right.
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Diffstat (limited to 'jshn.c')
-rw-r--r-- | jshn.c | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -258,18 +258,31 @@ static int jshn_format(bool no_newline, bool indent) { json_object *obj; const char *output; + char *blobmsg_output = NULL; + int ret = -1; + + if (!(obj = json_object_new_object())) + return -1; - obj = json_object_new_object(); jshn_add_objects(obj, "J_V", false); - output = json_object_to_json_string(obj); + if (!(output = json_object_to_json_string(obj))) + goto out; + if (indent) { blob_buf_init(&b, 0); - blobmsg_add_json_from_string(&b, output); - output = blobmsg_format_json_indent(b.head, 1, 0); + if (!blobmsg_add_json_from_string(&b, output)) + goto out; + if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0))) + goto out; + output = blobmsg_output; } fprintf(stdout, "%s%s", output, no_newline ? "" : "\n"); + free(blobmsg_output); + ret = 0; + +out: json_object_put(obj); - return 0; + return ret; } static int usage(const char *progname) |