]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: use list_add_tail() in more robust way
authorKarel Zak <kzak@redhat.com>
Mon, 18 Feb 2019 11:33:12 +0000 (12:33 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 18 Feb 2019 11:33:12 +0000 (12:33 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/grouping.c
libsmartcols/src/table.c

index 23dd7fcf07c9e60db1554125827559000de91e7d..c5fb2cb9ff31ad247ae18e05f28da9b137d84c46 100644 (file)
@@ -77,6 +77,7 @@ static void groups_fix_members_order(struct libscols_line *ln)
        struct libscols_line *child;
 
        if (ln->group) {
+               INIT_LIST_HEAD(&ln->ln_groups);
                list_add_tail(&ln->ln_groups, &ln->group->gr_members);
                DBG(GROUP, ul_debugobj(ln->group, "fixing member line=%p [%zu/%zu]",
                                        ln, ln->group->nmembers,
@@ -477,10 +478,12 @@ void scols_groups_reset_state(struct libscols_table *tb)
 static void add_member(struct libscols_group *gr, struct libscols_line *ln)
 {
        DBG(GROUP, ul_debugobj(gr, "add member"));
+
        ln->group = gr;
        gr->nmembers++;
        scols_ref_group(gr);
 
+       INIT_LIST_HEAD(&ln->ln_groups);
        list_add_tail(&ln->ln_groups, &gr->gr_members);
        scols_ref_line(ln);
 }
@@ -575,6 +578,9 @@ int scols_line_link_group(struct libscols_line *ln, struct libscols_line *member
        if (!ln || !member || !member->group || ln->parent)
                return -EINVAL;
 
+       if (!list_empty(&ln->ln_children))
+               return -EINVAL;
+
        DBG(GROUP, ul_debugobj(member->group, "add child"));
 
        list_add_tail(&ln->ln_children, &member->group->gr_children);
index e4f27a916cd57a8b3b9eabff3bf52c71727b81ee..944c1e593bfd5fdf08971f19b68e90fe7f577891 100644 (file)
@@ -225,6 +225,9 @@ int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl
        if (!tb || !cl || cl->table)
                return -EINVAL;
 
+       if (!list_empty(&cl->cl_columns))
+               return -EINVAL;
+
        if (cl->flags & SCOLS_FL_TREE)
                tb->ntreecols++;
 
@@ -594,6 +597,9 @@ int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln)
        if (!tb || !ln)
                return -EINVAL;
 
+       if (!list_empty(&ln->ln_lines))
+               return -EINVAL;
+
        if (tb->ncols > ln->ncells) {
                int rc = scols_line_alloc_cells(ln, tb->ncols);
                if (rc)