Commit 4c6744ed authored by Peter Eisentraut's avatar Peter Eisentraut

PL/Python: Fix potential NULL pointer dereference

After d0aa965c, one error path in
PLy_spi_execute_fetch_result() could result in the variable "result"
being dereferenced after being set to NULL.  Rearrange the code a bit to
fix that.

Also add another SPI_freetuptable() call so that that is cleared in all
error paths.

discovered by John Naylor <jcnaylor@gmail.com> via scan-build

ideas and review by Tom Lane
parent 85abb5b2
...@@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) ...@@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
result = (PLyResultObject *) PLy_result_new(); result = (PLyResultObject *) PLy_result_new();
if (!result) if (!result)
{
SPI_freetuptable(tuptable);
return NULL; return NULL;
}
Py_DECREF(result->status); Py_DECREF(result->status);
result->status = PyInt_FromLong(status); result->status = PyInt_FromLong(status);
...@@ -411,12 +414,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) ...@@ -411,12 +414,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
Py_DECREF(result->rows); Py_DECREF(result->rows);
result->rows = PyList_New(rows); result->rows = PyList_New(rows);
if (!result->rows) if (result->rows)
{
Py_DECREF(result);
result = NULL;
}
else
{ {
PLy_input_setup_tuple(&ininfo, tuptable->tupdesc, PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
exec_ctx->curr_proc); exec_ctx->curr_proc);
...@@ -455,6 +453,13 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) ...@@ -455,6 +453,13 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
MemoryContextDelete(cxt); MemoryContextDelete(cxt);
SPI_freetuptable(tuptable); SPI_freetuptable(tuptable);
/* in case PyList_New() failed above */
if (!result->rows)
{
Py_DECREF(result);
result = NULL;
}
} }
return (PyObject *) result; return (PyObject *) result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment