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 1puQz3-0006Cb-Qz for pgsql-hackers@arkaria.postgresql.org; Thu, 04 May 2023 04:54:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1puQz2-0004Lf-Cl for pgsql-hackers@arkaria.postgresql.org; Thu, 04 May 2023 04:54:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1puQz2-0004LW-3r for pgsql-hackers@lists.postgresql.org; Thu, 04 May 2023 04:54:24 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1puQyv-000QTd-De for pgsql-hackers@postgresql.org; Thu, 04 May 2023 04:54:22 +0000 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-5191796a483so4153692a12.0 for ; Wed, 03 May 2023 21:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683176056; x=1685768056; 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=i9j8kEbsTrJx6Pvv23J0vLm5pEXoDVCjCsHopDa10j8=; b=pnxM81qZxDM0QWFCDmF/12ggogtcQXgkDZ2fg1/vXJIXRVoZQfFlBn34VRGZZPCsfp Ym3e0ZrGYG2VsPl8MAgJbDwSdgJkDu4iwR1LWAtc6vo6sxLSkAaaKflM9BMZn+l1A8MX sBdWJ/anMAs2230mhY8WtHb2omIw7gL0v337ZpZXLBp4t7MpP21h8uUfS7qJZKCAIMpc 97monBEJEtKL9LCl/G2ouVkjWkDxnvt5M5TzkmpBz0hgY334HXSkpGDdnSZRmE04/6fQ 1tR9VvS8tOqVAv1eHxUWHz7JMPqXeLJ6S+AsocTmno8NrG0muTgHJ+lpacnI7g8A9geO /2+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683176056; x=1685768056; 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=i9j8kEbsTrJx6Pvv23J0vLm5pEXoDVCjCsHopDa10j8=; b=izfzmNyAPzpzx+bUxzR/Od7gll11ie5A4+yaaATUJ5zmdegFEO/3JV/SUCIlB0Jiou Xds/Z4w7Tk9cqFIPdw9aUm9se/PVh2n4KRo5DlF55qaNQ4llGIFyfZVrvCVlBx4+jqEF Ks/qNVxE8O/iagyhE8sfeYRHr7j0thJs3HNibsJw6ruxbrS+MglFc2Zy160MNzeSfmPA Q+tawhuKgLHwaTy/LHZthEZFdvCeoS2iWgIJ6tMA/uRZvfO0xoBmdtchOaAx/SFdTF5X hqWNORIdEZjxCsmItjxmc6n83NA7L9pC9j+mZoZmG/qL9r+9Qyr7utU1HtviWspLXKkS C7Bg== X-Gm-Message-State: AC+VfDw4q5s2UqVSvBa1anuvlXer4AlZahEZl1tUJ6bbPrkoehQWKxn6 0tn1jGhRw6k/mEE+7ICVt2Q= X-Google-Smtp-Source: ACHHUZ7brplVS3ywi6UrmcDfeDgH9PppWxOYbIqDLIhb8/K4cpLIMMHpM6dh8bgclGusMDhC58Ch5w== X-Received: by 2002:a17:90b:698:b0:23d:3549:82b7 with SMTP id m24-20020a17090b069800b0023d354982b7mr830557pjz.46.1683176056294; Wed, 03 May 2023 21:54:16 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id cq2-20020a17090af98200b002470b9503desm2210074pjb.55.2023.05.03.21.54.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 21:54:15 -0700 (PDT) Date: Wed, 3 May 2023 21:54:13 -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: <20230504045413.GA2202617@nathanxps13> References: <20230112184433.GA2104952@nathanxps13> <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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="4Ckj6UjgE2iN1+kY" Content-Disposition: inline In-Reply-To: <20230503205838.GA2112379@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --4Ckj6UjgE2iN1+kY Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, May 03, 2023 at 01:58:38PM -0700, Nathan Bossart wrote: > With this change, pltdata isn't modified in the PG_TRY section, and the > only modification of pltargs happens after all elogs. It might be worth > keeping pltargs volatile in case someone decides to add an elog() in the > future, but I see no need to keep it for pltdata. Here's a new patch that removes the volatile marker from pltdata. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --4Ckj6UjgE2iN1+kY Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-Fix-improper-returns-in-PG_TRY-blocks.patch" From 53d2d942b8ee553439da6f324186a62ebb7fce1f Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 3 May 2023 11:32:43 -0700 Subject: [PATCH v4 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 | 52 +++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 923703535a..1e3efd4d86 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -414,12 +414,17 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc) PyObject *volatile 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 --4Ckj6UjgE2iN1+kY--