Skip to content

Commit 2b9eb2d

Browse files
fix a vicious issue in list.remove on iterable objects
1 parent 79d8dbe commit 2b9eb2d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

swig/openscap_api.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,36 @@ def remove(self, item):
7878
"""Function to remove item from list. This removed item should be also
7979
removed from parent oscap list. This function is supported only if there exists
8080
reset function on iterators. Exception is throwed otherwise."""
81-
8281
try:
8382
self.iterator.reset()
8483
while self.iterator.has_more():
8584
litem = self.iterator.next()
8685
if (type(item) == str and type(litem) == str and litem == item) or \
8786
("instance" in item.__dict__ and litem.instance == item.instance):
87+
8888
self.iterator.remove()
89-
list.remove(self, item)
89+
90+
91+
'''
92+
Warning, list.remove(self, item) will fail because python yield
93+
a new reference at each loop. So wee need to loop again into the python list,
94+
get the new reference and remove it. Demo:
95+
96+
print(item.instance)
97+
print(litem.instance)
98+
print(litem.instance == item.instance)
99+
100+
RETURNS:
101+
102+
<Swig Object of type 'struct xccdf_refine_value *' at 0x7ff85ed73bd0>
103+
<Swig Object of type 'struct xccdf_refine_value *' at 0x7ff85ed73c90>
104+
True
105+
'''
106+
107+
for i in self[:]:
108+
if "instance" in item.__dict__ and i.instance == item.instance:
109+
list.remove(self, i)
110+
90111
except NameError:
91112
raise Exception("Removing %s items throught oscap list is not allowed. "
92113
"Please use appropriate function."
@@ -103,6 +124,8 @@ def generate(self, iterator):
103124

104125
while iterator.has_more():
105126
list.append(self, iterator.next())
127+
128+
106129

107130
def append(self, item, n=1):
108131
"""This function is not allowed. Please use appropriate function from library."""
@@ -623,10 +646,11 @@ def set_tailor_items(self, items):
623646

624647
oper = remarks = setvalue = None
625648
for r_value in self.profile.refine_values[:]:
626-
if r_value.item == item["id"] and r_value in self.profile.refine_values:
649+
if r_value.item == item["id"]:
627650
oper = r_value.oper
628651
remarks = r_value.remarks
629652
self.profile.refine_values.remove(r_value)
653+
630654
for s_value in self.profile.setvalues[:]:
631655
if s_value.item == item["id"]:
632656
setvalue = s_value.value

0 commit comments

Comments
 (0)