]> gerrit.simantics Code Review - simantics/python.git/commitdiff
Plug leaks from PySequence_GetItem. 28/328/1
authortkorvola <Timo.Korvola@vtt.fi>
Tue, 7 Feb 2017 11:39:18 +0000 (13:39 +0200)
committertkorvola <Timo.Korvola@vtt.fi>
Tue, 7 Feb 2017 11:45:26 +0000 (13:45 +0200)
One needs to be careful and RTFM: PySequence_GetItem returns a new
reference but, e.g., PyDict_GetItem does not.

Change-Id: I93f08163a2bbf66da2966044c7bfc7abcd140bbd

org.simantics.pythonlink.win32.x86_64/jnipython.dll
org.simantics.pythonlink.win32.x86_64/src/sclpy.c

index bb0a05aae18c761e217ad82a9f77104dc8cbff9e..c2f62cb7f5de3bbfa0d808fa900bb308711c0fab 100644 (file)
Binary files a/org.simantics.pythonlink.win32.x86_64/jnipython.dll and b/org.simantics.pythonlink.win32.x86_64/jnipython.dll differ
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;
                }