Fix multiple memory leaks in PLy_spi_execute_fetch_result: it would leak
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 30 Apr 2010 19:16:27 +0000 (19:16 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 30 Apr 2010 19:16:27 +0000 (19:16 +0000)
memory if the result had zero rows, and also if there was any sort of error
while converting the result tuples into Python data.  Reported and partially
fixed by Andres Freund.

Back-patch to all supported versions.  Note: I haven't tested the 7.4 fix.
7.4's configure check for python is so obsolete it doesn't work on my
current machines :-(.  The logic change is pretty straightforward though.

src/pl/plpython/plpython.c

index f765a685caee8f6328edd4b2ca8a870211abfcac..93548520292aa33ba2d82c3442c06b518d7733ae 100644 (file)
@@ -29,7 +29,7 @@
  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  * IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/plpython/plpython.c,v 1.41.2.4 2007/11/23 01:48:08 alvherre Exp $
+ * $Header: /cvsroot/pgsql/src/pl/plpython/plpython.c,v 1.41.2.5 2010/04/30 19:16:27 tgl Exp $
  *
  *********************************************************************
  */
@@ -2283,9 +2283,9 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
        PLyTypeInfo args;
        int         i;
 
-       PLy_typeinfo_init(&args);
        Py_DECREF(result->nrows);
        result->nrows = PyInt_FromLong(rows);
+       PLy_typeinfo_init(&args);
 
        SAVE_EXC();
        if (TRAP_EXC())
@@ -2295,8 +2295,9 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
            if (!PyErr_Occurred())
                PyErr_SetString(PLy_exc_error,
                        "Unknown error in PLy_spi_execute_fetch_result");
-           Py_DECREF(result);
            PLy_typeinfo_dealloc(&args);
+           SPI_freetuptable(tuptable);
+           Py_DECREF(result);
            RERAISE_EXC();
        }
 
@@ -2313,11 +2314,11 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
 
                PyList_SetItem(result->rows, i, row);
            }
-           PLy_typeinfo_dealloc(&args);
-
-           SPI_freetuptable(tuptable);
        }
        RESTORE_EXC();
+
+       PLy_typeinfo_dealloc(&args);
+       SPI_freetuptable(tuptable);
    }
 
    return (PyObject *) result;