summaryrefslogtreecommitdiffstats
path: root/src/parser_json.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-06-01 17:32:11 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2018-06-03 11:47:22 +0200
commit6aead97061f0681ebf37369fe7d2c8bbe09f914c (patch)
treeca926c4e05233da6888f4e996f9fe03231a983f7 /src/parser_json.c
parent6d79096aea854409a86a1854dcd3c52d5ed533f6 (diff)
JSON: Review large number parsing/printing
When parsing large (uint64_t) values, capital 'I' has to be used in format string. While being at it, make sure JSON output code handles those variables correctly, too. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/parser_json.c')
-rw-r--r--src/parser_json.c28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/parser_json.c b/src/parser_json.c
index 9b7aef4b..adf1564b 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -1119,7 +1119,7 @@ static struct expr *json_parse_set_elem_expr(struct json_ctx *ctx,
{
struct expr *expr;
json_t *tmp;
- int i;
+ uint64_t i;
if (json_unpack_err(ctx, root, "{s:o}", "val", &tmp))
return NULL;
@@ -1130,9 +1130,9 @@ static struct expr *json_parse_set_elem_expr(struct json_ctx *ctx,
expr = set_elem_expr_alloc(int_loc, expr);
- if (!json_unpack(root, "{s:i}", "timeout", &i))
+ if (!json_unpack(root, "{s:I}", "timeout", &i))
expr->timeout = i * 1000;
- if (!json_unpack(root, "{s:i}", "expires", &i))
+ if (!json_unpack(root, "{s:I}", "expires", &i))
expr->expiration = i * 1000;
if (!json_unpack(root, "{s:s}", "comment", &expr->comment))
expr->comment = xstrdup(expr->comment);
@@ -1374,13 +1374,13 @@ static struct stmt *json_parse_match_stmt(struct json_ctx *ctx,
static struct stmt *json_parse_counter_stmt(struct json_ctx *ctx,
const char *key, json_t *value)
{
- int packets, bytes;
+ uint64_t packets, bytes;
struct stmt *stmt;
if (json_is_null(value))
return counter_stmt_alloc(int_loc);
- if (!json_unpack(value, "{s:i, s:i}",
+ if (!json_unpack(value, "{s:I, s:I}",
"packets", &packets,
"bytes", &bytes)) {
stmt = counter_stmt_alloc(int_loc);
@@ -1461,7 +1461,7 @@ static struct stmt *json_parse_mangle_stmt(struct json_ctx *ctx,
}
}
-static uint64_t rate_to_bytes(int val, const char *unit)
+static uint64_t rate_to_bytes(uint64_t val, const char *unit)
{
uint64_t bytes = val;
@@ -1478,12 +1478,12 @@ static struct stmt *json_parse_quota_stmt(struct json_ctx *ctx,
struct stmt *stmt;
int inv = 0;
const char *val_unit = "bytes", *used_unit = "bytes";
- int val, used = 0;
+ uint64_t val, used = 0;
- if (!json_unpack(value, "{s:i}", "val", &val)) {
+ if (!json_unpack(value, "{s:I}", "val", &val)) {
json_unpack(value, "{s:b}", "inv", &inv);
json_unpack(value, "{s:s}", "val_unit", &val_unit);
- json_unpack(value, "{s:i}", "used", &used);
+ json_unpack(value, "{s:I}", "used", &used);
json_unpack(value, "{s:s}", "used_unit", &used_unit);
stmt = quota_stmt_alloc(int_loc);
stmt->quota.bytes = rate_to_bytes(val, val_unit);
@@ -2397,7 +2397,7 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,
return NULL;
}
}
- if (!json_unpack(root, "{s:i}", "timeout", &set->timeout))
+ if (!json_unpack(root, "{s:I}", "timeout", &set->timeout))
set->timeout *= 1000;
if (!json_unpack(root, "{s:i}", "gc-interval", &set->gc_int))
set->gc_int *= 1000;
@@ -2573,13 +2573,13 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
switch (cmd_obj) {
case CMD_OBJ_COUNTER:
obj->type = NFT_OBJECT_COUNTER;
- json_unpack(root, "{s:i}", "packets", &obj->counter.packets);
- json_unpack(root, "{s:i}", "bytes", &obj->counter.bytes);
+ json_unpack(root, "{s:I}", "packets", &obj->counter.packets);
+ json_unpack(root, "{s:I}", "bytes", &obj->counter.bytes);
break;
case CMD_OBJ_QUOTA:
obj->type = NFT_OBJECT_QUOTA;
- json_unpack(root, "{s:i}", "bytes", &obj->quota.bytes);
- json_unpack(root, "{s:i}", "used", &obj->quota.used);
+ json_unpack(root, "{s:I}", "bytes", &obj->quota.bytes);
+ json_unpack(root, "{s:I}", "used", &obj->quota.used);
json_unpack(root, "{s:b}", "inv", &obj->quota.flags);
if (obj->quota.flags)
obj->quota.flags = NFT_QUOTA_F_INV;