Skip to content

Commit 7b1f523

Browse files
committed
fix a crash when adding a pass with an incorrect keyword argument
The assert: assert(obj->wr_prev); within gcc_python_wrapper_untrack() could fail when cleaning up a partially-constructed object that never made it onto the list. Bulletproof the function against such __init__ failures.
1 parent 26b1316 commit 7b1f523

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

gcc-python-wrapper.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,25 @@ gcc_python_wrapper_untrack(struct PyGccWrapper *obj)
206206

207207
assert(obj);
208208
assert(Py_REFCNT(obj) == 0);
209-
assert(sentinel.wr_next);
210-
assert(sentinel.wr_prev);
211-
assert(obj->wr_prev);
212-
assert(obj->wr_next);
213209

214-
/* Remove from linked list: */
215-
obj->wr_prev->wr_next = obj->wr_next;
216-
obj->wr_next->wr_prev = obj->wr_prev;
217-
obj->wr_prev = NULL;
218-
obj->wr_next = NULL;
210+
/*
211+
Remove from the linked list if it's within it
212+
213+
(If the object constructor fails, then we have only a
214+
partially-constructed object, which might not have been
215+
added to the linked list yet)
216+
*/
217+
if (obj->wr_prev) {
218+
assert(sentinel.wr_next);
219+
assert(sentinel.wr_prev);
220+
assert(obj->wr_next);
221+
222+
/* Remove from linked list: */
223+
obj->wr_prev->wr_next = obj->wr_next;
224+
obj->wr_next->wr_prev = obj->wr_prev;
225+
obj->wr_prev = NULL;
226+
obj->wr_next = NULL;
227+
}
219228
}
220229

221230
void

tests/plugin/new-passes/script.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,14 @@ def execute(*args):
130130
ps = GimplePassSettingLocation(name='gimple-pass-setting-location')
131131
print('registering: %r' % ps)
132132
ps.register_after('cfg')
133+
134+
# Verify that the plugin doesn't crash when constructing a pass with
135+
# an unrecognized kwarg:
136+
class TestBogusKwargs(gcc.GimplePass):
137+
def __init__(self):
138+
gcc.GimplePass.__init__(self, 'test-bogus-kwargs')
139+
140+
try:
141+
ps = TestBogusKwargs(this_is_not_a_valid_kwarg=42)
142+
except:
143+
pass

0 commit comments

Comments
 (0)