]> git.ipfire.org Git - thirdparty/ipset.git/commitdiff
Improve fixing commas in sorted json output
authorJozsef Kadlecsik <kadlec@netfilter.org>
Sat, 19 Apr 2025 13:06:44 +0000 (15:06 +0200)
committerJozsef Kadlecsik <kadlec@netfilter.org>
Sat, 19 Apr 2025 13:06:44 +0000 (15:06 +0200)
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
lib/session.c

index 61b05c3c3e5d3bad98622cdda90f862c913a8682..c91f029ef68fd49f27f5f72c5c0a7f5308330636 100644 (file)
@@ -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);