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