From e3af7eed4d0bb9b82bdb591eb20f259c428625f8 Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Mon, 7 Oct 2019 12:09:43 +0200 Subject: #915 Improve error management for og_json_parse_partition() Validate json input and return an error code when parsing is not correct. --- sources/ogAdmServer.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'sources') diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index a72c2c2..da33988 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3297,13 +3297,28 @@ static int og_json_parse_string(json_t *element, const char **str) return 0; } -static void og_json_parse_partition(json_t *element, og_partition *part) +static int og_json_parse_partition(json_t *element, og_partition *part) { - part->number = json_string_value(json_object_get(element, "partition")); - part->code = json_string_value(json_object_get(element, "code")); - part->filesystem = json_string_value(json_object_get(element, "filesystem")); - part->size = json_string_value(json_object_get(element, "size")); - part->format = json_string_value(json_object_get(element, "format")); + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "partition")) + err = og_json_parse_string(value, &part->number); + else if (!strcmp(key, "code")) + err = og_json_parse_string(value, &part->code); + else if (!strcmp(key, "filesystem")) + err = og_json_parse_string(value, &part->filesystem); + else if (!strcmp(key, "size")) + err = og_json_parse_string(value, &part->size); + else if (!strcmp(key, "format")) + err = og_json_parse_string(value, &part->format); + + if (err < 0) + return err; + } + return err; } static int og_json_parse_partition_setup(json_t *element, og_partition *part) @@ -3320,7 +3335,8 @@ static int og_json_parse_partition_setup(json_t *element, og_partition *part) if (json_typeof(k) != JSON_OBJECT) return -1; - og_json_parse_partition(k, part + i); + if (og_json_parse_partition(k, part + i) != 0) + return -1; } return 0; } -- cgit v1.2.3-18-g5258