@@ -47,7 +47,7 @@ static bool makeNewPyMethod(JSContext *cx, JS::MutableHandleValue function, JS::
4747 thisValue.setObject (*thisObject);
4848 PyObject *newSelf = pyTypeFactory (cx, thisValue);
4949 function.set (jsTypeFactory (cx, PyMethod_New (func, newSelf)));
50- Py_DECREF (newSelf);
50+ Py_XDECREF (newSelf);
5151
5252 return true ;
5353}
@@ -114,10 +114,10 @@ static bool array_push(JSContext *cx, unsigned argc, JS::Value *vp) { // surely
114114 elementVal.set (args[index].get ());
115115 PyObject *value = pyTypeFactory (cx, elementVal);
116116 if (PyList_Append (self, value) < 0 ) {
117- Py_DECREF (value);
117+ Py_XDECREF (value);
118118 return false ;
119119 }
120- Py_DECREF (value);
120+ Py_XDECREF (value);
121121 }
122122
123123 args.rval ().setInt32 (PyList_GET_SIZE (self));
@@ -166,10 +166,10 @@ static bool array_unshift(JSContext *cx, unsigned argc, JS::Value *vp) { // sure
166166 elementVal.set (args[index].get ());
167167 PyObject *value = pyTypeFactory (cx, elementVal);
168168 if (PyList_Insert (self, 0 , value) < 0 ) {
169- Py_DECREF (value);
169+ Py_XDECREF (value);
170170 return false ;
171171 }
172- Py_DECREF (value);
172+ Py_XDECREF (value);
173173 }
174174
175175 args.rval ().setInt32 (PyList_GET_SIZE (self));
@@ -281,7 +281,7 @@ static bool array_indexOf(JSContext *cx, unsigned argc, JS::Value *vp) {
281281 JS::RootedValue elementVal (cx, args[0 ].get ());
282282 PyObject *value = pyTypeFactory (cx, elementVal);
283283 PyObject *result = PyObject_CallMethod (self, " index" , " Oi" , value, start);
284- Py_DECREF (value);
284+ Py_XDECREF (value);
285285
286286 if (!result) {
287287 PyErr_Clear ();
@@ -364,10 +364,8 @@ static bool array_splice(JSContext *cx, unsigned argc, JS::Value *vp) {
364364 elementVal.set (args[index + 2 ].get ());
365365 PyObject *value = pyTypeFactory (cx, elementVal);
366366 if (PyList_SetItem (inserted, index, value) < 0 ) {
367- Py_DECREF (value);
368367 return false ;
369368 }
370- Py_DECREF (value);
371369 }
372370
373371 if (PyList_SetSlice (self, actualStart, actualStart + actualDeleteCount, inserted) < 0 ) {
@@ -430,12 +428,18 @@ static bool array_fill(JSContext *cx, unsigned argc, JS::Value *vp) {
430428
431429 JS::RootedValue fillValue (cx, args[0 ].get ());
432430 PyObject *fillValueItem = pyTypeFactory (cx, fillValue);
431+ bool setItemCalled = false ;
433432 for (int index = actualStart; index < actualEnd; index++) {
433+ setItemCalled = true ;
434434 if (PyList_SetItem (self, index, fillValueItem) < 0 ) {
435435 return false ;
436436 }
437437 }
438438
439+ if (!setItemCalled) {
440+ Py_XDECREF (fillValueItem);
441+ }
442+
439443 // return ref to self
440444 args.rval ().set (jsTypeFactory (cx, self));
441445 return true ;
@@ -564,27 +568,39 @@ static bool array_concat(JSContext *cx, unsigned argc, JS::Value *vp) {
564568 Py_ssize_t itemLength = JSArrayProxyMethodDefinitions::JSArrayProxy_length ((JSArrayProxy *)item);
565569 for (Py_ssize_t flatIndex = 0 ; flatIndex < itemLength; flatIndex++) {
566570 if (!JS_GetElement (cx, *(((JSArrayProxy *)item)->jsArray ), flatIndex, &elementVal)) {
571+ Py_XDECREF (item);
567572 return false ;
568573 }
569- if (PyList_Append (result, pyTypeFactory (cx, elementVal)) < 0 ) {
574+ PyObject *value = pyTypeFactory (cx, elementVal);
575+ if (PyList_Append (result, value) < 0 ) {
576+ Py_XDECREF (item);
577+ Py_XDECREF (value);
570578 return false ;
571579 }
580+ Py_XDECREF (value);
572581 }
573582 }
574583 else if (PyObject_TypeCheck (item, &PyList_Type)) {
575584 // flatten the array only at depth 1
576585 Py_ssize_t itemLength = PyList_GET_SIZE (item);
577586 for (Py_ssize_t flatIndex = 0 ; flatIndex < itemLength; flatIndex++) {
578587 if (PyList_Append (result, PyList_GetItem (item, flatIndex)) < 0 ) {
588+ Py_XDECREF (item);
579589 return false ;
580590 }
581591 }
582592 }
583593 else {
584- if (PyList_Append (result, pyTypeFactory (cx, elementVal)) < 0 ) {
594+ PyObject *value = pyTypeFactory (cx, elementVal);
595+ if (PyList_Append (result, value) < 0 ) {
596+ Py_XDECREF (item);
597+ Py_XDECREF (value);
585598 return false ;
586599 }
600+ Py_XDECREF (value);
587601 }
602+
603+ Py_XDECREF (item);
588604 }
589605
590606 args.rval ().set (jsTypeFactory (cx, result));
@@ -639,13 +655,16 @@ static bool array_lastIndexOf(JSContext *cx, unsigned argc, JS::Value *vp) {
639655 int cmp = PyObject_RichCompareBool (item, element, Py_EQ);
640656 Py_DECREF (item);
641657 if (cmp < 0 ) {
658+ Py_XDECREF (element);
642659 return false ;
643660 }
644661 else if (cmp == 1 ) {
662+ Py_XDECREF (element);
645663 args.rval ().setInt32 (index);
646664 return true ;
647665 }
648666 }
667+ Py_XDECREF (element);
649668
650669 args.rval ().setInt32 (-1 );
651670 return true ;
@@ -1247,7 +1266,6 @@ static uint32_t FlattenIntoArray(JSContext *cx,
12471266 JS::RootedValue elementVal (cx);
12481267
12491268 for (uint32_t sourceIndex = 0 ; sourceIndex < sourceLen; sourceIndex++) {
1250-
12511269 if (PyObject_TypeCheck (source, &JSArrayProxyType)) {
12521270 JS_GetElement (cx, *(((JSArrayProxy *)source)->jsArray ), sourceIndex, &elementVal);
12531271 }
@@ -1294,6 +1312,8 @@ static uint32_t FlattenIntoArray(JSContext *cx,
12941312
12951313 targetIndex++;
12961314 }
1315+
1316+ Py_XDECREF (element);
12971317 }
12981318
12991319 return targetIndex;
@@ -1391,6 +1411,8 @@ static uint32_t FlattenIntoArrayWithCallBack(JSContext *cx,
13911411 targetIndex++;
13921412 }
13931413 }
1414+
1415+ Py_XDECREF (element);
13941416 }
13951417
13961418 return targetIndex;
0 commit comments