Skip to content

Commit 5cd72c7

Browse files
committed
Patch some corner-case bugs in pl/python.
Dave Malcolm of Red Hat is working on a static code analysis tool for Python-related C code. It reported a number of problems in plpython, most of which were failures to check for NULL results from object-creation functions, so would only be an issue in very-low-memory situations. Patch in HEAD and 9.1. We could go further back but it's not clear that these issues are important enough to justify the work. Jan Urbański
1 parent a14fa84 commit 5cd72c7

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

src/pl/plpython/plpy_elog.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ get_source_line(const char *src, int lineno)
367367
const char *next = src;
368368
int current = 0;
369369

370+
/* sanity check */
371+
if (lineno <= 0)
372+
return NULL;
373+
370374
while (current < lineno)
371375
{
372376
s = next;

src/pl/plpython/plpy_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ PLy_init_interp(void)
142142
Py_INCREF(mainmod);
143143
PLy_interp_globals = PyModule_GetDict(mainmod);
144144
PLy_interp_safe_globals = PyDict_New();
145+
if (PLy_interp_safe_globals == NULL)
146+
PLy_elog(ERROR, "could not create globals");
145147
PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
146148
Py_DECREF(mainmod);
147149
if (PLy_interp_globals == NULL || PyErr_Occurred())

src/pl/plpython/plpy_plpymodule.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ PLy_init_plpy(void)
173173
main_mod = PyImport_AddModule("__main__");
174174
main_dict = PyModule_GetDict(main_mod);
175175
plpy_mod = PyImport_AddModule("plpy");
176+
if (plpy_mod == NULL)
177+
PLy_elog(ERROR, "could not initialize plpy");
176178
PyDict_SetItemString(main_dict, "plpy", plpy_mod);
177179
if (PyErr_Occurred())
178-
elog(ERROR, "could not initialize plpy");
180+
PLy_elog(ERROR, "could not initialize plpy");
179181
}
180182

181183
static void
@@ -208,6 +210,11 @@ PLy_add_exceptions(PyObject *plpy)
208210
PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
209211
PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
210212

213+
if (PLy_exc_error == NULL ||
214+
PLy_exc_fatal == NULL ||
215+
PLy_exc_spi_error == NULL)
216+
PLy_elog(ERROR, "could not create the base SPI exceptions");
217+
211218
Py_INCREF(PLy_exc_error);
212219
PyModule_AddObject(plpy, "Error", PLy_exc_error);
213220
Py_INCREF(PLy_exc_fatal);
@@ -241,7 +248,13 @@ PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
241248
PyObject *sqlstate;
242249
PyObject *dict = PyDict_New();
243250

251+
if (dict == NULL)
252+
PLy_elog(ERROR, "could not generate SPI exceptions");
253+
244254
sqlstate = PyString_FromString(unpack_sql_state(exception_map[i].sqlstate));
255+
if (sqlstate == NULL)
256+
PLy_elog(ERROR, "could not generate SPI exceptions");
257+
245258
PyDict_SetItemString(dict, "sqlstate", sqlstate);
246259
Py_DECREF(sqlstate);
247260
exc = PyErr_NewException(exception_map[i].name, base, dict);
@@ -370,7 +383,8 @@ PLy_output(volatile int level, PyObject *self, PyObject *args)
370383
*/
371384
PyObject *o;
372385

373-
PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o);
386+
if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
387+
PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
374388
so = PyObject_Str(o);
375389
}
376390
else

src/pl/plpython/plpy_spi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ PLy_spi_execute_query(char *query, long limit)
340340
int rv;
341341
volatile MemoryContext oldcontext;
342342
volatile ResourceOwner oldowner;
343-
PyObject *ret;
343+
PyObject *ret = NULL;
344344

345345
oldcontext = CurrentMemoryContext;
346346
oldowner = CurrentResourceOwner;
@@ -366,6 +366,7 @@ PLy_spi_execute_query(char *query, long limit)
366366

367367
if (rv < 0)
368368
{
369+
Py_XDECREF(ret);
369370
PLy_exception_set(PLy_exc_spi_error,
370371
"SPI_execute failed: %s",
371372
SPI_result_code_string(rv));

src/pl/plpython/plpy_typeio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ PLyList_FromArray(PLyDatumToOb *arg, Datum d)
584584
length = ARR_DIMS(array)[0];
585585
lbound = ARR_LBOUND(array)[0];
586586
list = PyList_New(length);
587+
if (list == NULL)
588+
PLy_elog(ERROR, "could not create new Python list");
587589

588590
for (i = 0; i < length; i++)
589591
{

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