@@ -415,15 +415,20 @@ static PyObject *
415
415
PLy_function_build_args (FunctionCallInfo fcinfo , PLyProcedure * proc )
416
416
{
417
417
PyObject * volatile arg = NULL ;
418
- PyObject * volatile args = NULL ;
418
+ PyObject * args ;
419
419
int i ;
420
420
421
+ /*
422
+ * Make any Py*_New() calls before the PG_TRY block so that we can quickly
423
+ * return NULL on failure. We can't return within the PG_TRY block, else
424
+ * we'd miss unwinding the exception stack.
425
+ */
426
+ args = PyList_New (proc -> nargs );
427
+ if (!args )
428
+ return NULL ;
429
+
421
430
PG_TRY ();
422
431
{
423
- args = PyList_New (proc -> nargs );
424
- if (!args )
425
- return NULL ;
426
-
427
432
for (i = 0 ; i < proc -> nargs ; i ++ )
428
433
{
429
434
PLyDatumToOb * arginfo = & proc -> args [i ];
@@ -687,19 +692,34 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
687
692
* pltlevel ,
688
693
* pltrelid ,
689
694
* plttablename ,
690
- * plttableschema ;
691
- PyObject * pltargs ,
695
+ * plttableschema ,
696
+ * pltargs = NULL ,
692
697
* pytnew ,
693
- * pytold ;
694
- PyObject * volatile pltdata = NULL ;
698
+ * pytold ,
699
+ * pltdata ;
695
700
char * stroid ;
696
701
697
- PG_TRY ();
702
+ /*
703
+ * Make any Py*_New() calls before the PG_TRY block so that we can quickly
704
+ * return NULL on failure. We can't return within the PG_TRY block, else
705
+ * we'd miss unwinding the exception stack.
706
+ */
707
+ pltdata = PyDict_New ();
708
+ if (!pltdata )
709
+ return NULL ;
710
+
711
+ if (tdata -> tg_trigger -> tgnargs )
698
712
{
699
- pltdata = PyDict_New ();
700
- if (!pltdata )
713
+ pltargs = PyList_New (tdata -> tg_trigger -> tgnargs );
714
+ if (!pltargs )
715
+ {
716
+ Py_DECREF (pltdata );
701
717
return NULL ;
718
+ }
719
+ }
702
720
721
+ PG_TRY ();
722
+ {
703
723
pltname = PyString_FromString (tdata -> tg_trigger -> tgname );
704
724
PyDict_SetItemString (pltdata , "name" , pltname );
705
725
Py_DECREF (pltname );
@@ -839,12 +859,9 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
839
859
int i ;
840
860
PyObject * pltarg ;
841
861
842
- pltargs = PyList_New (tdata -> tg_trigger -> tgnargs );
843
- if (!pltargs )
844
- {
845
- Py_DECREF (pltdata );
846
- return NULL ;
847
- }
862
+ /* pltargs should have been allocated before the PG_TRY block. */
863
+ Assert (pltargs );
864
+
848
865
for (i = 0 ; i < tdata -> tg_trigger -> tgnargs ; i ++ )
849
866
{
850
867
pltarg = PyString_FromString (tdata -> tg_trigger -> tgargs [i ]);
@@ -865,6 +882,7 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r
865
882
}
866
883
PG_CATCH ();
867
884
{
885
+ Py_XDECREF (pltargs );
868
886
Py_XDECREF (pltdata );
869
887
PG_RE_THROW ();
870
888
}
0 commit comments