]> gerrit.simantics Code Review - simantics/python.git/blobdiff - org.simantics.pythonlink.win32.x86_64/src/sclpy.c
Plug leaks from PySequence_GetItem.
[simantics/python.git] / org.simantics.pythonlink.win32.x86_64 / src / sclpy.c
index 1592a2f56de443ddc298ad847a17f48175e04b02..caed56dc8c8ab30e3cc9c0d2437969eca7470571 100644 (file)
@@ -690,9 +690,11 @@ jobjectArray pythonSequenceAsStringArray(JNIEnv *env, PyObject *seq) {
                PyObject *item = PySequence_GetItem(seq, i);
                if (PyUnicode_Check(item)) {
                        jstring value = pythonStringAsJavaString(env, item);
+                       Py_DECREF(item);
                        (*env)->SetObjectArrayElement(env, array, i, value);
                }
                else {
+                       Py_DECREF(item);
                        throwPythonException(env, "List item not a string");
                        return NULL;
                }
@@ -711,10 +713,12 @@ jdoubleArray pythonSequenceAsDoubleArray(JNIEnv *env, PyObject *seq) {
        for (i = 0; i < jlen; i++) {
                PyObject *item = PySequence_GetItem(seq, i);
                if (PyFloat_Check(item)) {
-                       double value = PyFloat_AsDouble(item);
+                       jdouble value = PyFloat_AsDouble(item);
+                       Py_DECREF(item);
                        (*env)->SetDoubleArrayRegion(env, array, i, 1, &value);
                }
                else {
+                       Py_DECREF(item);
                        throwPythonException(env, "List item not a floating point value");
                        return NULL;
                }
@@ -754,6 +758,7 @@ jobjectArray pythonSequenceAsObjectArray(JNIEnv *env, PyObject *seq) {
        for (i = 0; i < jlen; i++) {
                PyObject *item = PySequence_GetItem(seq, i);
                jobject object = pythonObjectAsObject(env, item);
+               Py_DECREF(item);
                (*env)->SetObjectArrayElement(env, array, i, object);
        }
 
@@ -771,9 +776,11 @@ jbooleanArray pythonSequenceAsBooleanArray(JNIEnv *env, PyObject *seq) {
                PyObject *item = PySequence_GetItem(seq, i);
                if (PyBool_Check(item)) {
                        jboolean value = item == Py_True;
+                       Py_DECREF(item);
                        (*env)->SetBooleanArrayRegion(env, array, i, 1, &value);
                }
                else {
+                       Py_DECREF(item);
                        throwPythonException(env, "List item not a boolean");
                        return NULL;
                }
@@ -793,9 +800,11 @@ jintArray pythonSequenceAsIntegerArray(JNIEnv *env, PyObject *seq) {
                PyObject *item = PySequence_GetItem(seq, i);
                if (PyLong_Check(item)) {
                        jint value = PyLong_AsLong(item);
+                       Py_DECREF(item);
                        (*env)->SetIntArrayRegion(env, array, i, 1, &value);
                }
                else {
+                       Py_DECREF(item);
                        throwPythonException(env, "List item not an integer");
                        return NULL;
                }
@@ -815,9 +824,11 @@ jlongArray pythonSequenceAsLongArray(JNIEnv *env, PyObject *seq) {
                PyObject *item = PySequence_GetItem(seq, i);
                if (PyLong_Check(item)) {
                        jlong value = PyLong_AsLongLong(item);
+                       Py_DECREF(item);
                        (*env)->SetLongArrayRegion(env, array, i, 1, &value);
                }
                else {
+                       Py_DECREF(item);
                        throwPythonException(env, "List item not an integer");
                        return NULL;
                }