@@ -2345,6 +2345,8 @@ PLyList_FromArray(PLyDatumToOb *arg, Datum d)
2345
2345
length = ARR_DIMS (array )[0 ];
2346
2346
lbound = ARR_LBOUND (array )[0 ];
2347
2347
list = PyList_New (length );
2348
+ if (list == NULL )
2349
+ PLy_elog (ERROR , "could not create new list" );
2348
2350
2349
2351
for (i = 0 ; i < length ; i ++ )
2350
2352
{
@@ -3664,7 +3666,7 @@ PLy_spi_execute_query(char *query, long limit)
3664
3666
int rv ;
3665
3667
volatile MemoryContext oldcontext ;
3666
3668
volatile ResourceOwner oldowner ;
3667
- PyObject * ret ;
3669
+ PyObject * ret = NULL ;
3668
3670
3669
3671
oldcontext = CurrentMemoryContext ;
3670
3672
oldowner = CurrentResourceOwner ;
@@ -3727,6 +3729,7 @@ PLy_spi_execute_query(char *query, long limit)
3727
3729
3728
3730
if (rv < 0 )
3729
3731
{
3732
+ Py_XDECREF (ret );
3730
3733
PLy_exception_set (PLy_exc_spi_error ,
3731
3734
"SPI_execute failed: %s" ,
3732
3735
SPI_result_code_string (rv ));
@@ -3967,7 +3970,13 @@ PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
3967
3970
PyObject * sqlstate ;
3968
3971
PyObject * dict = PyDict_New ();
3969
3972
3973
+ if (dict == NULL )
3974
+ PLy_elog (ERROR , "could not generate SPI exceptions" );
3975
+
3970
3976
sqlstate = PyString_FromString (unpack_sql_state (exception_map [i ].sqlstate ));
3977
+ if (sqlstate == NULL )
3978
+ PLy_elog (ERROR , "could not generate SPI exceptions" );
3979
+
3971
3980
PyDict_SetItemString (dict , "sqlstate" , sqlstate );
3972
3981
Py_DECREF (sqlstate );
3973
3982
exc = PyErr_NewException (exception_map [i ].name , base , dict );
@@ -4008,6 +4017,11 @@ PLy_add_exceptions(PyObject *plpy)
4008
4017
PLy_exc_fatal = PyErr_NewException ("plpy.Fatal" , NULL , NULL );
4009
4018
PLy_exc_spi_error = PyErr_NewException ("plpy.SPIError" , NULL , NULL );
4010
4019
4020
+ if (PLy_exc_error == NULL ||
4021
+ PLy_exc_fatal == NULL ||
4022
+ PLy_exc_spi_error == NULL )
4023
+ PLy_elog (ERROR , "could not create the base SPI exceptions" );
4024
+
4011
4025
Py_INCREF (PLy_exc_error );
4012
4026
PyModule_AddObject (plpy , "Error" , PLy_exc_error );
4013
4027
Py_INCREF (PLy_exc_fatal );
@@ -4124,6 +4138,8 @@ PLy_init_interp(void)
4124
4138
Py_INCREF (mainmod );
4125
4139
PLy_interp_globals = PyModule_GetDict (mainmod );
4126
4140
PLy_interp_safe_globals = PyDict_New ();
4141
+ if (PLy_interp_safe_globals == NULL )
4142
+ PLy_elog (ERROR , "could not create globals" );
4127
4143
PyDict_SetItemString (PLy_interp_globals , "GD" , PLy_interp_safe_globals );
4128
4144
Py_DECREF (mainmod );
4129
4145
if (PLy_interp_globals == NULL || PyErr_Occurred ())
@@ -4164,9 +4180,11 @@ PLy_init_plpy(void)
4164
4180
main_mod = PyImport_AddModule ("__main__" );
4165
4181
main_dict = PyModule_GetDict (main_mod );
4166
4182
plpy_mod = PyImport_AddModule ("plpy" );
4183
+ if (plpy_mod == NULL )
4184
+ PLy_elog (ERROR , "could not initialize plpy" );
4167
4185
PyDict_SetItemString (main_dict , "plpy" , plpy_mod );
4168
4186
if (PyErr_Occurred ())
4169
- elog (ERROR , "could not initialize plpy" );
4187
+ PLy_elog (ERROR , "could not initialize plpy" );
4170
4188
}
4171
4189
4172
4190
/* the python interface to the elog function
@@ -4232,7 +4250,8 @@ PLy_output(volatile int level, PyObject *self, PyObject *args)
4232
4250
*/
4233
4251
PyObject * o ;
4234
4252
4235
- PyArg_UnpackTuple (args , "plpy.elog" , 1 , 1 , & o );
4253
+ if (!PyArg_UnpackTuple (args , "plpy.elog" , 1 , 1 , & o ))
4254
+ PLy_elog (ERROR , "could not unpack arguments in plpy.elog" );
4236
4255
so = PyObject_Str (o );
4237
4256
}
4238
4257
else
@@ -4554,6 +4573,10 @@ get_source_line(const char *src, int lineno)
4554
4573
const char * next = src ;
4555
4574
int current = 0 ;
4556
4575
4576
+ /* sanity check */
4577
+ if (lineno <= 0 )
4578
+ return NULL ;
4579
+
4557
4580
while (current < lineno )
4558
4581
{
4559
4582
s = next ;
0 commit comments