summaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/json.c26
-rw-r--r--src/parser_json.c28
2 files changed, 29 insertions, 25 deletions
diff --git a/src/json.c b/src/json.c
index 11607b67..07774482 100644
--- a/src/json.c
+++ b/src/json.c
@@ -126,7 +126,7 @@ static json_t *set_print_json(struct output_ctx *octx, const struct set *set)
}
if (set->timeout) {
- tmp = json_pack("i", set->timeout / 1000);
+ tmp = json_integer(set->timeout / 1000);
json_object_set_new(root, "timeout", tmp);
}
if (set->gc_int) {
@@ -498,6 +498,7 @@ json_t *set_ref_expr_json(const struct expr *expr, struct output_ctx *octx)
json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
{
json_t *root = expr_print_json(expr->key, octx);
+ json_t *tmp;
if (!root)
return NULL;
@@ -506,15 +507,18 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
if (expr->timeout || expr->expiration || expr->comment) {
root = json_pack("{s:o}", "val", root);
- if (expr->timeout)
- json_object_set_new(root, "timeout",
- json_integer(expr->timeout / 1000));
- if (expr->expiration)
- json_object_set_new(root, "expires",
- json_integer(expr->expiration / 1000));
- if (expr->comment)
- json_object_set_new(root, "comment",
- json_string(expr->comment));
+ if (expr->timeout) {
+ tmp = json_integer(expr->timeout / 1000);
+ json_object_set_new(root, "timeout", tmp);
+ }
+ if (expr->expiration) {
+ tmp = json_integer(expr->expiration / 1000);
+ json_object_set_new(root, "expires", tmp);
+ }
+ if (expr->comment) {
+ tmp = json_string(expr->comment);
+ json_object_set_new(root, "comment", tmp);
+ }
return json_pack("{s:o}", "elem", root);
}
@@ -938,7 +942,7 @@ json_t *quota_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
json_object_set_new(root, "inv", json_true());
if (!octx->stateless && stmt->quota.used) {
data_unit = get_rate(stmt->quota.used, &bytes);
- json_object_set_new(root, "used", json_integer((int)bytes));
+ json_object_set_new(root, "used", json_integer(bytes));
json_object_set_new(root, "used_unit", json_string(data_unit));
}
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;