diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2019-10-07 12:09:43 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-10-07 12:54:53 +0200 |
commit | e3af7eed4d0bb9b82bdb591eb20f259c428625f8 (patch) | |
tree | 5cc1d0895ce3764b10bfe141e94ce0a2f956082c | |
parent | ad4d4bdbbe182eede4079be1e0ea345288a3e3df (diff) |
#915 Improve error management for og_json_parse_partition()
Validate json input and return an error code when parsing is not correct.
-rw-r--r-- | sources/ogAdmServer.cpp | 30 |
1 files changed, 23 insertions, 7 deletions
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; } |