diff options
author | Jozsef Kadlecsik <kadlec@netfilter.org> | 2025-04-19 15:06:44 +0200 |
---|---|---|
committer | Jozsef Kadlecsik <kadlec@netfilter.org> | 2025-04-19 15:06:44 +0200 |
commit | 7153bdda964fc64e6fa20b8e9e9a9ecfb7f768ca (patch) | |
tree | 285a33fd9a7fb3db2d06fc708beb39ba2f3749cc | |
parent | 152962ffc33ae3c96143453ed6dd63e010294c66 (diff) |
Improve fixing commas in sorted json output
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
-rw-r--r-- | lib/session.c | 16 |
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); |