From: tkorvola Date: Tue, 7 Feb 2017 11:39:18 +0000 (+0200) Subject: Plug leaks from PySequence_GetItem. X-Git-Tag: v1.31.0~1 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fpython.git;a=commitdiff_plain;h=843f99efcf673dfd4937db0f2ed1c973880e876b Plug leaks from PySequence_GetItem. One needs to be careful and RTFM: PySequence_GetItem returns a new reference but, e.g., PyDict_GetItem does not. Change-Id: I93f08163a2bbf66da2966044c7bfc7abcd140bbd --- diff --git a/org.simantics.pythonlink.win32.x86_64/jnipython.dll b/org.simantics.pythonlink.win32.x86_64/jnipython.dll index bb0a05a..c2f62cb 100644 Binary files a/org.simantics.pythonlink.win32.x86_64/jnipython.dll and b/org.simantics.pythonlink.win32.x86_64/jnipython.dll differ diff --git a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c index 1592a2f..caed56d 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -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; }