From 776f37405da713d3d5ad3908dd9aee4f50c1b1f9 Mon Sep 17 00:00:00 2001 From: "Z. Liu" Date: Mon, 20 May 2024 22:23:40 +0800 Subject: [PATCH] ipset: fix json output format for IPSET_OPT_IP IPSET_OPT_IP should be quoted to be a well formed json file, otherwise see following bad example (range is not quoted): # ipset create foo bitmap:ip range 192.168.0.0/16 # ipset list -o json foo [ { "name" : "foo", "type" : "bitmap:ip", "revision" : 3, "header" : { "range" : 192.168.0.0-192.168.255.255, "memsize" : 8280, "references" : 0, "numentries" : 0 }, "members" : [ ] } ] Signed-off-by: Z. Liu Signed-off-by: Jozsef Kadlecsik --- lib/print.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/print.c b/lib/print.c index 6ea79cb4..3806e1c0 100644 --- a/lib/print.c +++ b/lib/print.c @@ -261,6 +261,7 @@ ipset_print_ip(char *buf, unsigned int len, uint8_t family, cidr; int flags, size, offset = 0; enum ipset_opt cidropt; + const char *quoted = env & IPSET_ENV_QUOTED ? "\"" : ""; assert(buf); assert(len > 0); @@ -277,20 +278,26 @@ ipset_print_ip(char *buf, unsigned int len, cidr = family == NFPROTO_IPV6 ? 128 : 32; flags = (env & IPSET_ENV_RESOLVE) ? 0 : NI_NUMERICHOST; + size = snprintf(buf, len, "%s", quoted); + SNPRINTF_FAILURE(size, len, offset); + ip = ipset_data_get(data, opt); assert(ip); if (family == NFPROTO_IPV4) - size = snprintf_ipv4(buf, len, flags, ip, cidr); + size = snprintf_ipv4(buf + offset, len, flags, ip, cidr); else if (family == NFPROTO_IPV6) - size = snprintf_ipv6(buf, len, flags, ip, cidr); + size = snprintf_ipv6(buf + offset, len, flags, ip, cidr); else return -1; D("size %i, len %u", size, len); SNPRINTF_FAILURE(size, len, offset); D("len: %u, offset %u", len, offset); - if (!ipset_data_test(data, IPSET_OPT_IP_TO)) + if (!ipset_data_test(data, IPSET_OPT_IP_TO)) { + size = snprintf(buf + offset, len, "%s", quoted); + SNPRINTF_FAILURE(size, len, offset); return offset; + } size = snprintf(buf + offset, len, "%s", IPSET_RANGE_SEPARATOR); SNPRINTF_FAILURE(size, len, offset); @@ -304,6 +311,10 @@ ipset_print_ip(char *buf, unsigned int len, return -1; SNPRINTF_FAILURE(size, len, offset); + + size = snprintf(buf + offset, len, "%s", quoted); + SNPRINTF_FAILURE(size, len, offset); + return offset; } -- 2.47.2