Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMJAL-0000dh-8L for pgsql-committers@arkaria.postgresql.org; Tue, 05 Dec 2017 19:46:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMJAK-0007g8-K4 for pgsql-committers@arkaria.postgresql.org; Tue, 05 Dec 2017 19:46:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eMJAK-0007fy-CF for pgsql-committers@lists.postgresql.org; Tue, 05 Dec 2017 19:46:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eMJAI-0001Df-4K for pgsql-committers@postgresql.org; Tue, 05 Dec 2017 19:46:03 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id vB5JjxIx016696; Tue, 5 Dec 2017 14:45:59 -0500 From: Tom Lane To: Peter Eisentraut cc: pgsql-committers@postgresql.org Subject: Re: pgsql: PL/Python: Fix potential NULL pointer dereference In-reply-to: References: <26463.1511886937@sss.pgh.pa.us> Comments: In-reply-to Peter Eisentraut message dated "Tue, 05 Dec 2017 14:16:59 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16694.1512503159.1@sss.pgh.pa.us> Date: Tue, 05 Dec 2017 14:45:59 -0500 Message-ID: <16695.1512503159@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Peter Eisentraut writes: > On 11/28/17 13:35, Peter Eisentraut wrote: >> OK, I'll revert and rethink. > How about this instead? This looks better, but I think there are a couple more things: * Seems like you ought to s/Py_DECREF(result)/Py_XDECREF(result)/ in the PG_CATCH block (line 451 in HEAD). As this is coded, although result is set to NULL within the PG_TRY, it doesn't seem possible for control to reach PG_CATCH after that ... but that's a pretty fragile assumption. * You'd also need to declare "result" as PLyResultObject *volatile result; if you intend to change it within the PG_TRY block. On the whole it seems like it might be better to dodge this whole business of changing "result" inside the TRY. You could do that if you did something like result->rows = PyList_New(rows); - if (!result->rows) - { - Py_DECREF(result); - result = NULL; - } - else + if (result->rows) { PLy_input_setup_tuple(&ininfo, tuptable->tupdesc, and then add after the PG_END_TRY if (!result->rows) { Py_DECREF(result); result = NULL; } The repeated test is a bit annoying but maybe it's better than the alternative. You'd probably need a comment explaining why it's like that, though. regards, tom lane