Skip to content

Commit 4c6744e

Browse files
committed
PL/Python: Fix potential NULL pointer dereference
After d0aa965, 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
1 parent 85abb5b commit 4c6744e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/pl/plpython/plpy_spi.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
361361

362362
result = (PLyResultObject *) PLy_result_new();
363363
if (!result)
364+
{
365+
SPI_freetuptable(tuptable);
364366
return NULL;
367+
}
365368
Py_DECREF(result->status);
366369
result->status = PyInt_FromLong(status);
367370

@@ -411,12 +414,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
411414

412415
Py_DECREF(result->rows);
413416
result->rows = PyList_New(rows);
414-
if (!result->rows)
415-
{
416-
Py_DECREF(result);
417-
result = NULL;
418-
}
419-
else
417+
if (result->rows)
420418
{
421419
PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
422420
exec_ctx->curr_proc);
@@ -455,6 +453,13 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
455453

456454
MemoryContextDelete(cxt);
457455
SPI_freetuptable(tuptable);
456+
457+
/* in case PyList_New() failed above */
458+
if (!result->rows)
459+
{
460+
Py_DECREF(result);
461+
result = NULL;
462+
}
458463
}
459464

460465
return (PyObject *) result;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy