From 98875443ea96361eb4da7890c87922ccb3571f51 Mon Sep 17 00:00:00 2001 From: Geoff Greer Date: Sun, 22 Oct 2017 01:00:32 -0700 Subject: swaygrab: Increase max depth of JSON parsing to 256. Prevent segfault if IPC response can't be parsed. The default max nesting depth of json-c is 32, which can cause some valid trees to fail to be parsed, so increase that. Also instead of segfaulting, just abort and print the error returned by json-c. --- swaygrab/json.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'swaygrab/json.c') diff --git a/swaygrab/json.c b/swaygrab/json.c index 80dae299..32b68612 100644 --- a/swaygrab/json.c +++ b/swaygrab/json.c @@ -4,6 +4,7 @@ #include #include #include +#include "log.h" #include "ipc-client.h" #include "swaygrab/json.h" @@ -12,7 +13,15 @@ static json_object *tree; void init_json_tree(int socketfd) { uint32_t len = 0; char *res = ipc_single_command(socketfd, IPC_GET_TREE, NULL, &len); - tree = json_tokener_parse(res); + struct json_tokener *tok = json_tokener_new_ex(256); + if (!tok) { + sway_abort("Unable to get json tokener."); + } + tree = json_tokener_parse_ex(tok, res, len); + if (!tree || tok->err != json_tokener_success) { + sway_abort("Unable to parse IPC response as JSON: %s", json_tokener_error_desc(tok->err)); + } + json_tokener_free(tok); } void free_json_tree() { -- cgit v1.2.3