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 1puIza-0004lG-EA for pgsql-hackers@arkaria.postgresql.org; Wed, 03 May 2023 20:22:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1puIyZ-0004N8-EH for pgsql-hackers@arkaria.postgresql.org; Wed, 03 May 2023 20:21:23 +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 1puIyZ-0004Mu-3Z for pgsql-hackers@lists.postgresql.org; Wed, 03 May 2023 20:21:23 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1puIyW-000Mal-E1 for pgsql-hackers@postgresql.org; Wed, 03 May 2023 20:21:21 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1a516fb6523so55052225ad.3 for ; Wed, 03 May 2023 13:21:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683145279; x=1685737279; 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=vZTHxP1rIi9TLA6mPzMN2lQLR2/61MqbjCWaMn+E9hA=; b=TWei6No8K74DC7spWsnl2WumzTUPH+kPwkQbldtHQ+cGlIq9kGSVXCmxm2HE5kBSqb upoxdKMgWPdWiSHP3HDM/kR4FwkK7uxYu/SC2kUOZ2iyLMez/JijX7mtQR0mx1kPjBgp RW3nIUMmMohlA+0I78O40qOnsWJHPYejbCFEBejRD39LQc5Uvil2zrrjjR8qnpE+pdQJ jcqHxDMHHqG2nHPzC9M/Vyl0n5NS3g6ixwImgvYW6VeWkALJY7QzVWg00TTVaBa3q5ls 4XOx7gTq3XzWOjdJ+yTCPuHMsGw1zTjcbK8hDHQacKHAGG1jIYiP4jcsHi9A/AtCUZUA ByCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683145279; x=1685737279; 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=vZTHxP1rIi9TLA6mPzMN2lQLR2/61MqbjCWaMn+E9hA=; b=hBXfafOOtSUh8AdSIkQpO/60n+VqhRN/xYPLZ2zd/jluPPPCSceFjVGTXkdiwYwEif 2ibE8rrrr8zgglh13dTHZ5UTbfqc4jNh7mCPA8coBHzRZddm28nyo6cblxh3D/DK/MR5 ToexHq+dmfLOKr2mXlT96HKrSxbVWmmlatZSMAI6Q1ENlQqN8udpaihJ95vak8puWaxT 64+wqOG642J2JOdwTBqWeBeRRhNPDKs0jBkt3+QDd5S9m6sfaENU7U6eBJjvmKlsHCH1 TTmQhPtVXv2Sm/J2Qe9K8ZY3PQS36ZQj09CrUu0HvCPYjXsOw9tHkWINvWUGJaXKGjwq im2w== X-Gm-Message-State: AC+VfDyqMzts+HCIo70F+2zKJiMRBthvlxgbGXaF5OGZF6iLmv5AIO6k Q1gq4qR8fYj4ACe9O1GlYLs= X-Google-Smtp-Source: ACHHUZ4mrQP+aSBvB8qFfE02PZrjSIo5rZkRChxUhm96d/tDQSCFxWMfGC2tPehwGZD+J6KxNgDCHw== X-Received: by 2002:a17:902:f68c:b0:1ab:18d9:f3a3 with SMTP id l12-20020a170902f68c00b001ab18d9f3a3mr1554681plg.18.1683145279428; Wed, 03 May 2023 13:21:19 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id jm23-20020a17090304d700b001a1faed8707sm15153121plb.63.2023.05.03.13.21.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 13:21:18 -0700 (PDT) Date: Wed, 3 May 2023 13:21:16 -0700 From: Nathan Bossart To: Andres Freund Cc: Tom Lane , Xing Guo , pgsql-hackers@postgresql.org Subject: Re: PL/Python: Fix return in the middle of PG_TRY() block. Message-ID: <20230503202116.GA2110623@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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="k+w/mQv8wyuph6w0" Content-Disposition: inline In-Reply-To: <20230120190201.GC4106863@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --k+w/mQv8wyuph6w0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here's a new version of the patch. Besides adding comments and a commit message, I made sure to decrement the reference count for pltargs in the PG_CATCH block (which means that pltargs likely needs to be volatile). I'm not too wild about moving the chunk of code for pltargs like this, but I haven't thought of a better option. We could error instead of returning NULL, but IIUC that would go against d0aa965's stated purpose. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --k+w/mQv8wyuph6w0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Fix-improper-returns-in-PG_TRY-blocks.patch" From 12daf35ca398a34046d911270283f2cb7ebcbf3f Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 3 May 2023 11:32:43 -0700 Subject: [PATCH v3 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 | 48 +++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 923703535a..9a483daa8e 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 pltargs = NULL; PyObject *volatile pltdata = 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 --k+w/mQv8wyuph6w0--