From: Jozsef Kadlecsik Date: Sat, 19 Apr 2025 13:06:44 +0000 (+0200) Subject: Improve fixing commas in sorted json output X-Git-Tag: v7.24~2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=7153bdda964fc64e6fa20b8e9e9a9ecfb7f768ca;p=thirdparty%2Fipset.git Improve fixing commas in sorted json output Signed-off-by: Jozsef Kadlecsik --- 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);