Blob Blame History Raw
From 810efc113dc90a60689295ea99de61eee918c46f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
Date: Thu, 22 Oct 2020 13:23:25 +0200
Subject: [PATCH] Use already implemented clear for COMPS_HSList
 (RhBug:1888343)

Instead of reimplementing clear for COMPS_HSList use comps_hslist_clear.
The crash was caused by dangling pointers first and last in the
COMPS_HSList (rt->subnodes) struct, function comps_hslist_clear clears
them out properly.

= changelog =
msg: Fix a crash when clearing COMPS_ObjRTree
type: bugfix
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1888343
---
 libcomps/src/comps_objradix.c       | 15 ++-------------
 libcomps/src/python/tests/__test.py | 10 ++++++++++
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/libcomps/src/comps_objradix.c b/libcomps/src/comps_objradix.c
index 840592a..111ddbc 100644
--- a/libcomps/src/comps_objradix.c
+++ b/libcomps/src/comps_objradix.c
@@ -611,20 +611,9 @@ void comps_objrtree_unset(COMPS_ObjRTree * rt, const char * key) {
 }
 
 void comps_objrtree_clear(COMPS_ObjRTree * rt) {
-    COMPS_HSListItem *it, *oldit;
     if (rt==NULL) return;
-    if (rt->subnodes == NULL) return;
-    oldit = rt->subnodes->first;
-    it = (oldit)?oldit->next:NULL;
-    for (;it != NULL; it=it->next) {
-        comps_object_destroy(oldit->data);
-        free(oldit);
-        oldit = it;
-    }
-    if (oldit) {
-        comps_object_destroy(oldit->data);
-        free(oldit);
-    }
+    comps_hslist_clear(rt->subnodes);
+    rt->len = 0;
 }
 
 inline COMPS_HSList* __comps_objrtree_all(COMPS_ObjRTree * rt, char keyvalpair) {
diff --git a/libcomps/src/python/tests/__test.py b/libcomps/src/python/tests/__test.py
index 9cb957a..2b321e3 100644
--- a/libcomps/src/python/tests/__test.py
+++ b/libcomps/src/python/tests/__test.py
@@ -986,6 +986,16 @@ def test_envs(self):
 
         _f([x.name for x in env.option_ids], option_ids)
 
+    #@unittest.skip("")
+    def test_clear_for_COMPS_ObjRTree_such_as_group_or_category_namy_by_lang(self):
+        comps = libcomps.Comps()
+        ret = comps.fromxml_f("comps/f21-rawhide-comps.xml")
+
+        env = comps.categories[0].name_by_lang.clear()
+        env = comps.groups[0].name_by_lang.clear()
+
+        self.assertEqual(str(comps.categories[0].name_by_lang), u'{}')
+        self.assertEqual(str(comps.groups[0].name_by_lang), u'{}')
 
     #@unittest.skip("")
     def test_xml_options(self):