Blame SOURCES/0004-Use-already-implemented-clear-for-COMPS-HSList-RhBug-1888343.patch

18a187
From 810efc113dc90a60689295ea99de61eee918c46f Mon Sep 17 00:00:00 2001
18a187
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
18a187
Date: Thu, 22 Oct 2020 13:23:25 +0200
18a187
Subject: [PATCH] Use already implemented clear for COMPS_HSList
18a187
 (RhBug:1888343)
18a187
18a187
Instead of reimplementing clear for COMPS_HSList use comps_hslist_clear.
18a187
The crash was caused by dangling pointers first and last in the
18a187
COMPS_HSList (rt->subnodes) struct, function comps_hslist_clear clears
18a187
them out properly.
18a187
18a187
= changelog =
18a187
msg: Fix a crash when clearing COMPS_ObjRTree
18a187
type: bugfix
18a187
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1888343
18a187
---
18a187
 libcomps/src/comps_objradix.c       | 15 ++-------------
18a187
 libcomps/src/python/tests/__test.py | 10 ++++++++++
18a187
 2 files changed, 12 insertions(+), 13 deletions(-)
18a187
18a187
diff --git a/libcomps/src/comps_objradix.c b/libcomps/src/comps_objradix.c
18a187
index 840592a..111ddbc 100644
18a187
--- a/libcomps/src/comps_objradix.c
18a187
+++ b/libcomps/src/comps_objradix.c
18a187
@@ -611,20 +611,9 @@ void comps_objrtree_unset(COMPS_ObjRTree * rt, const char * key) {
18a187
 }
18a187
 
18a187
 void comps_objrtree_clear(COMPS_ObjRTree * rt) {
18a187
-    COMPS_HSListItem *it, *oldit;
18a187
     if (rt==NULL) return;
18a187
-    if (rt->subnodes == NULL) return;
18a187
-    oldit = rt->subnodes->first;
18a187
-    it = (oldit)?oldit->next:NULL;
18a187
-    for (;it != NULL; it=it->next) {
18a187
-        comps_object_destroy(oldit->data);
18a187
-        free(oldit);
18a187
-        oldit = it;
18a187
-    }
18a187
-    if (oldit) {
18a187
-        comps_object_destroy(oldit->data);
18a187
-        free(oldit);
18a187
-    }
18a187
+    comps_hslist_clear(rt->subnodes);
18a187
+    rt->len = 0;
18a187
 }
18a187
 
18a187
 inline COMPS_HSList* __comps_objrtree_all(COMPS_ObjRTree * rt, char keyvalpair) {
18a187
diff --git a/libcomps/src/python/tests/__test.py b/libcomps/src/python/tests/__test.py
18a187
index 9cb957a..2b321e3 100644
18a187
--- a/libcomps/src/python/tests/__test.py
18a187
+++ b/libcomps/src/python/tests/__test.py
18a187
@@ -986,6 +986,16 @@ def test_envs(self):
18a187
 
18a187
         _f([x.name for x in env.option_ids], option_ids)
18a187
 
18a187
+    #@unittest.skip("")
18a187
+    def test_clear_for_COMPS_ObjRTree_such_as_group_or_category_namy_by_lang(self):
18a187
+        comps = libcomps.Comps()
18a187
+        ret = comps.fromxml_f("comps/f21-rawhide-comps.xml")
18a187
+
18a187
+        env = comps.categories[0].name_by_lang.clear()
18a187
+        env = comps.groups[0].name_by_lang.clear()
18a187
+
18a187
+        self.assertEqual(str(comps.categories[0].name_by_lang), u'{}')
18a187
+        self.assertEqual(str(comps.groups[0].name_by_lang), u'{}')
18a187
 
18a187
     #@unittest.skip("")
18a187
     def test_xml_options(self):