]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
table: json: fix returned errno value while parsing
authorPablo Neira Ayuso <pablo@netfilter.org>
Thu, 25 Jul 2013 21:22:26 +0000 (23:22 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 25 Jul 2013 21:23:13 +0000 (23:23 +0200)
Instead of returning ERANGE all the time, let functions set
errno accordingly and set EINVAL otherwise.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/jansson.c
src/table.c

index 2b15240a703b6749edb5aded04067fe39c9fb3e6..cc68ae0e69ad82e7bef5e2f803e9a59cbd29baad 100644 (file)
@@ -31,14 +31,11 @@ static int nft_jansson_load_int_node(json_t *root, const char *tag,
 
        if (!json_is_integer(node)) {
                errno = ERANGE;
-               goto err;
+               return -1;
        }
-
        *val = json_integer_value(node);
 
        return 0;
-err:
-       return -1;
 }
 
 const char *nft_jansson_value_parse_str(json_t *root, const char *tag)
@@ -47,9 +44,10 @@ const char *nft_jansson_value_parse_str(json_t *root, const char *tag)
        const char *val;
 
        node = json_object_get(root, tag);
-       if (node == NULL)
+       if (node == NULL) {
+               errno = EINVAL;
                return NULL;
-
+       }
        val = json_string_value(node);
 
        return val;
@@ -61,15 +59,12 @@ int nft_jansson_value_parse_val(json_t *root, const char *tag, int type,
        json_int_t val;
 
        if (nft_jansson_load_int_node(root, tag, &val) == -1)
-               goto err;
+               return -1;
 
        if (nft_get_value(type, &val, out) == -1)
-               goto err;
+               return -1;
 
        return 0;
-err:
-       errno = ERANGE;
-       return -1;
 }
 
 bool nft_jansson_node_exist(json_t *root, const char *tag)
index 65797e89624e5a9f89326b39e94eeaed57baec5d..1d17d3bf1035962b8211d4213c77995c8fa42b8d 100644 (file)
@@ -302,26 +302,29 @@ static int nft_table_json_parse(struct nft_table *t, char *json)
        json_error_t error;
        uint64_t version;
        uint32_t table_flag;
-       const char *str = NULL;
+       const char *str;
+       int family;
 
        root = json_loadb(json, strlen(json), 0, &error);
        if (!root) {
                errno = EINVAL;
-               return -1;
+               goto err;
        }
 
        root = json_object_get(root, "table");
        if (root == NULL) {
-               errno = ERANGE;
-               return -1;
+               errno = EINVAL;
+               goto err;
        }
 
        if (nft_jansson_value_parse_val(root, "version",
                                        NFT_TYPE_U64, &version) == -1)
                goto err;
 
-       if (version != NFT_TABLE_JSON_VERSION || version == -1)
+       if (version != NFT_TABLE_JSON_VERSION) {
+               errno = EINVAL;
                goto err;
+       }
 
        str = nft_jansson_value_parse_str(root, "name");
        if (str == NULL)
@@ -330,17 +333,20 @@ static int nft_table_json_parse(struct nft_table *t, char *json)
        nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, strdup(str));
 
        root = json_object_get(root, "properties");
-       if (root == NULL)
+       if (root == NULL) {
+               errno = EINVAL;
                goto err;
+       }
 
        str = nft_jansson_value_parse_str(root, "family");
        if (str == NULL)
                goto err;
 
-       if (nft_str2family(str) < 0)
+       family = nft_str2family(str);
+       if (family < 0)
                goto err;
 
-       nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, nft_str2family(str));
+       nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
        if (nft_jansson_value_parse_val(root, "table_flags",
                                        NFT_TYPE_U32, &table_flag) == -1)
@@ -352,9 +358,7 @@ static int nft_table_json_parse(struct nft_table *t, char *json)
        return 0;
 err:
        free(root);
-       errno = ERANGE;
        return -1;
-
 #else
        errno = EOPNOTSUPP;
        return -1;