summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozsef Kadlecsik <kadlec@netfilter.org>2025-04-19 15:06:44 +0200
committerJozsef Kadlecsik <kadlec@netfilter.org>2025-04-19 15:06:44 +0200
commit7153bdda964fc64e6fa20b8e9e9a9ecfb7f768ca (patch)
tree285a33fd9a7fb3db2d06fc708beb39ba2f3749cc
parent152962ffc33ae3c96143453ed6dd63e010294c66 (diff)
Improve fixing commas in sorted json output
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
-rw-r--r--lib/session.c16
1 files changed, 7 insertions, 9 deletions
diff --git a/lib/session.c b/lib/session.c
index 61b05c3..c91f029 100644
--- a/lib/session.c
+++ b/lib/session.c
@@ -1254,9 +1254,8 @@ print_set_done(struct ipset_session *session, bool callback_done)
D("called for %s", session->saved_setname[0] == '\0'
? "NONE" : session->saved_setname);
if (session->sort) {
- struct ipset_sorted *pos;
+ struct ipset_sorted *pos, *next;
const char *comma = session->mode == IPSET_LIST_JSON ? "," : "";
- uint32_t n = 0, numentries = 0;
int ret;
/* Print set header */
@@ -1266,15 +1265,14 @@ print_set_done(struct ipset_session *session, bool callback_done)
return MNL_CB_ERROR;
list_sort(session, &session->sorted, bystrcmp);
- /* In JSON output we must not emit the last comma */
- if (session->mode == IPSET_LIST_JSON) {
- list_for_each_entry(pos, &session->sorted, list)
- numentries++;
- }
list_for_each_entry(pos, &session->sorted, list) {
- if (++n == numentries)
- comma = "";
+ /* In JSON output we must not emit the last comma */
+ if (session->mode == IPSET_LIST_JSON) {
+ next = list_entry(pos->list.next, typeof(*pos), list);
+ if (&next->list == &session->sorted)
+ comma = "";
+ }
ret = session->print_outfn(session, session->p,
"%s%s",
session->outbuf + pos->offset, comma);