Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1puR5l-0006YR-80 for pgsql-hackers@arkaria.postgresql.org; Thu, 04 May 2023 05:01:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1puR5k-00010n-26 for pgsql-hackers@arkaria.postgresql.org; Thu, 04 May 2023 05:01:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1puR5j-00010e-PO for pgsql-hackers@lists.postgresql.org; Thu, 04 May 2023 05:01:19 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1puR5f-000TLn-Ah for pgsql-hackers@postgresql.org; Thu, 04 May 2023 05:01:19 +0000 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-643846c006fso140497b3a.0 for ; Wed, 03 May 2023 22:01:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683176474; x=1685768474; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=zlTrR5sF/1dfJpQxJcKFQi/SyfXGJVmySZX9C4DG/Ng=; b=iWyYn6ACqb+O8IxnfeTpa45YbGlymmXtxMwwCPxzJDEtg13Y3YfrmIH5qK/SGLVFyz RTU1ZrYJmgl8F8WjaZvj+BzqZ00Bv19a6b/NRmOXKudmGsm+KRZFcBIq1PxwLRiyJ2kM HEBa9zCSeWbzoqLsoYjP+zb3WZVdY6sE3sWGRG1dx0nggyRoGaLVAQkJjE32swkPufgA j2E2HStZPw6Jq66bo5wgKRTv249z6rHD0EqgsYlAGkZXn89GZHB4eoxZfHtB60EhVMMO PYCARlehBFxTujB7CoqXS/fA19FyKWB9srk1a7IrDiFVlhVm7pUiA0IqTmM393Sw2Mmp 3riw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683176474; x=1685768474; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zlTrR5sF/1dfJpQxJcKFQi/SyfXGJVmySZX9C4DG/Ng=; b=fN6NvzCyGpGGl798o7uAh3OM9ka12pr5Kx2Q626091LJSFPeRcQUVUFix9HFVAu4Ny gSOOUvwhWl26EsRmP1m6VoF2h8UzHpc3OkZZXUGO0at0xl1WYW9tFACRCcIf5YrNONwN xwYS0u/+xE0QDVKi3mc6MtioS+Tv6FV/un7ToUDTol22yrwKzwpn9qooCSVCojFZ79NX 1YrIQn/qYocB5adzVcJmGFr19svfgZWWEkNUR6ETJBZxkeWW5dnIpRzfZVyvVH345/Ya 4XUfRLPoW+YVNf6/9WI6WHg5Tb4bKzND8oWiEvmvCdQPh6Y9iIH3R2x5vwX8mTim6VhS /LzA== X-Gm-Message-State: AC+VfDzJRz8oN5EUiqXba2wr7LxvFYMbp69YvG3isFycTpHvGpK5xov8 XFio14t6DVllxl+9v476oIc= X-Google-Smtp-Source: ACHHUZ7aBwsXmjB8IsKUdI/GIa/dUP/E8kBZpS/7N4u4LSzuOwtT9PyTJX25xSDcYThIOuf7mUvTbw== X-Received: by 2002:a05:6a00:1251:b0:63d:4446:18ab with SMTP id u17-20020a056a00125100b0063d444618abmr984989pfi.23.1683176473691; Wed, 03 May 2023 22:01:13 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b00642c5ef6050sm6871523pfi.173.2023.05.03.22.01.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 22:01:13 -0700 (PDT) Date: Wed, 3 May 2023 22:01:08 -0700 From: Nathan Bossart To: Tom Lane Cc: Andres Freund , Xing Guo , pgsql-hackers@postgresql.org Subject: Re: PL/Python: Fix return in the middle of PG_TRY() block. Message-ID: <20230504050108.GA2203557@nathanxps13> References: <20230113054900.b7onkvwtkrykeu3z@awork3.anarazel.de> <20230113180335.GA2160040@nathanxps13> <1864041.1673882943@sss.pgh.pa.us> <20230120010711.jpqbaelpgmuvt2vq@awork3.anarazel.de> <20230120190201.GC4106863@nathanxps13> <20230503202116.GA2110623@nathanxps13> <3012766.1683146012@sss.pgh.pa.us> <20230503205838.GA2112379@nathanxps13> <20230504045413.GA2202617@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="M9NhX3UHpAaciwkO" Content-Disposition: inline In-Reply-To: <20230504045413.GA2202617@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --M9NhX3UHpAaciwkO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, May 03, 2023 at 09:54:13PM -0700, Nathan Bossart wrote: > Here's a new patch that removes the volatile marker from pltdata. Gah, right after I sent that, I realized we can remove one more volatile marker. Sorry for the noise. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --M9NhX3UHpAaciwkO Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v5-0001-Fix-improper-returns-in-PG_TRY-blocks.patch" From 24a4e91b3cdd66b985fab70f3aed8f6a202b31fe Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 3 May 2023 11:32:43 -0700 Subject: [PATCH v5 1/1] Fix improper returns in PG_TRY blocks. If we exit a PG_TRY block early via "continue", "break", "goto", or "return", we'll skip unwinding its exception stack. This change moves a couple of such "return" statements in PL/Python out of PG_TRY blocks. This was introduced in d0aa965c0a and affects all supported versions. We might also be able to add compile-time checks to avoid recurrence, but that is left as a future exercise. Reported-by: Mikhail Gribkov, Xing Guo Author: Xing Guo Reviewed-by: Michael Paquier, Andres Freund, Tom Lane Discussion: https://postgr.es/m/CAMEv5_v5Y%2B-D%3DCO1%2Bqoe16sAmgC4sbbQjz%2BUtcHmB6zcgS%2B5Ew%40mail.gmail.com Discussion: https://postgr.es/m/CACpMh%2BCMsGMRKFzFMm3bYTzQmMU5nfEEoEDU2apJcc4hid36AQ%40mail.gmail.com Backpatch-through: 11 (all supported versions) --- src/pl/plpython/plpy_exec.c | 54 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 923703535a..fcb363891a 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -411,15 +411,20 @@ static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc) { PyObject *volatile arg = NULL; - PyObject *volatile args = NULL; + PyObject *args = NULL; int i; + /* + * Make any Py*_New() calls before the PG_TRY block so that we can quickly + * return NULL on failure. We can't return within the PG_TRY block, else + * we'd miss unwinding the exception stack. + */ + args = PyList_New(proc->nargs); + if (!args) + return NULL; + PG_TRY(); { - args = PyList_New(proc->nargs); - if (!args) - return NULL; - for (i = 0; i < proc->nargs; i++) { PLyDatumToOb *arginfo = &proc->args[i]; @@ -683,19 +688,34 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r *pltlevel, *pltrelid, *plttablename, - *plttableschema; - PyObject *pltargs, + *plttableschema, *pytnew, - *pytold; - PyObject *volatile pltdata = NULL; + *pytold, + *pltdata; + PyObject *volatile pltargs = NULL; char *stroid; - PG_TRY(); + /* + * Make any Py*_New() calls before the PG_TRY block so that we can quickly + * return NULL on failure. We can't return within the PG_TRY block, else + * we'd miss unwinding the exception stack. + */ + pltdata = PyDict_New(); + if (!pltdata) + return NULL; + + if (tdata->tg_trigger->tgnargs) { - pltdata = PyDict_New(); - if (!pltdata) + pltargs = PyList_New(tdata->tg_trigger->tgnargs); + if (!pltargs) + { + Py_DECREF(pltdata); return NULL; + } + } + PG_TRY(); + { pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname); PyDict_SetItemString(pltdata, "name", pltname); Py_DECREF(pltname); @@ -835,12 +855,9 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r int i; PyObject *pltarg; - pltargs = PyList_New(tdata->tg_trigger->tgnargs); - if (!pltargs) - { - Py_DECREF(pltdata); - return NULL; - } + /* pltargs should have been allocated before the PG_TRY block. */ + Assert(pltargs); + for (i = 0; i < tdata->tg_trigger->tgnargs; i++) { pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]); @@ -861,6 +878,7 @@ PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *r } PG_CATCH(); { + Py_XDECREF(pltargs); Py_XDECREF(pltdata); PG_RE_THROW(); } -- 2.25.1 --M9NhX3UHpAaciwkO--