pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-committers@postgresql.org
Subject: Re: pgsql: PL/Python: Fix potential NULL pointer dereference
Date: Tue, 12 Dec 2017 12:10:21 -0500
Message-ID: <ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com> (raw)
In-Reply-To: <16695.1512503159@sss.pgh.pa.us>
References: <E1eJin0-0002iK-Ge@gemulon.postgresql.org>
	<26463.1511886937@sss.pgh.pa.us>
	<a1f87b42-fd0c-e985-ff35-79e6ad5c890e@2ndquadrant.com>
	<de6d3b84-287a-b943-b5cc-6f5825657c6b@2ndquadrant.com>
	<16695.1512503159@sss.pgh.pa.us>

On 12/5/17 14:45, Tom Lane wrote:
> 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;
>                 }

Yeah, that looks much better.  Next try attached.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

v2-0001-PL-Python-Fix-potential-NULL-pointer-dereference.patch (text/plain, 1.9K) download From aa2f7ca289b3475f1680b181b08be84bda5d74bc Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter_e@gmx.net> Date: Tue, 5 Dec 2017 14:14:55 -0500 Subject: [PATCH v2] PL/Python: Fix potential NULL pointer dereference After d0aa965c0a0ac2ff7906ae1b1dad50a7952efa56, one error path in PLy_spi_execute_fetch_result() could result in the variable "result" being dereferenced after being set to NULL. Put a conditional around that to fix that. Also add another SPI_freetuptable() call so that that is cleared in all error paths. discovered by John Naylor <jcnaylor@gmail.com> via scan-build --- src/pl/plpython/plpy_spi.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c index ade27f3924..0c623a9458 100644 --- a/src/pl/plpython/plpy_spi.c +++ b/src/pl/plpython/plpy_spi.c @@ -361,7 +361,10 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) result = (PLyResultObject *) PLy_result_new(); if (!result) + { + SPI_freetuptable(tuptable); return NULL; + } Py_DECREF(result->status); result->status = PyInt_FromLong(status); @@ -411,12 +414,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) Py_DECREF(result->rows); result->rows = PyList_New(rows); - if (!result->rows) - { - Py_DECREF(result); - result = NULL; - } - else + if (result->rows) { PLy_input_setup_tuple(&ininfo, tuptable->tupdesc, exec_ctx->curr_proc); @@ -455,6 +453,13 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status) MemoryContextDelete(cxt); SPI_freetuptable(tuptable); + + /* in case PyList_New() failed above */ + if (!result->rows) + { + Py_DECREF(result); + result = NULL; + } } return (PyObject *) result; base-commit: d329dc2ea4bfac84ec60ba14b96561a7508bb37b -- 2.15.1

view thread (8+ messages)  latest in thread

Message-ID: <ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com>
Permalink:  ../ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com/
Also on:    postgresql.org/message-id/ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: peter.eisentraut@2ndquadrant.com, tgl@sss.pgh.pa.us
  Subject: Re: pgsql: PL/Python: Fix potential NULL pointer dereference
  In-Reply-To: <ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com>
  References: <E1eJin0-0002iK-Ge@gemulon.postgresql.org> <26463.1511886937@sss.pgh.pa.us> <a1f87b42-fd0c-e985-ff35-79e6ad5c890e@2ndquadrant.com> <de6d3b84-287a-b943-b5cc-6f5825657c6b@2ndquadrant.com> <16695.1512503159@sss.pgh.pa.us> <ff4fa2ea-5085-40dd-aabb-110c6255dade@2ndquadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox