Skip to content

Commit dda2916

Browse files
Xmaderzollqir
andcommitted
fix: reference count for array_fill
Since each call of `PyList_SetItem` steals a reference (even if its to the same object), we need multiple references to it for it to steal Co-authored-by: Caleb Aikens <[email protected]>
1 parent a1f11b4 commit dda2916

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/PyListProxyHandler.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,16 @@ static bool array_fill(JSContext *cx, unsigned argc, JS::Value *vp) {
428428

429429
JS::RootedValue fillValue(cx, args[0].get());
430430
PyObject *fillValueItem = pyTypeFactory(cx, fillValue);
431-
bool setItemCalled = false;
432431
for (int index = actualStart; index < actualEnd; index++) {
433-
setItemCalled = true;
432+
// Since each call of `PyList_SetItem` steals a reference (even if its to the same object),
433+
// We need multiple references to it for it to steal.
434+
Py_INCREF(fillValueItem);
434435
if (PyList_SetItem(self, index, fillValueItem) < 0) {
435436
return false;
436437
}
437438
}
438439

439-
Py_INCREF(fillValueItem);
440-
if (setItemCalled) {
441-
Py_INCREF(fillValueItem);
442-
}
440+
Py_DECREF(fillValueItem);
443441

444442
// return ref to self
445443
args.rval().set(jsTypeFactory(cx, self));

0 commit comments

Comments
 (0)