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