pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Andres Freund <andres@anarazel.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: starbugs@qq.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16190: The usage of NULL pointer in refint.c
Date: Tue, 7 Jan 2020 10:27:41 +0900
Message-ID: <20200107012741.GD2386@paquier.xyz> (raw)
In-Reply-To: <20200106174443.3pwnmshbxrbwggrm@alap3.anarazel.de>
References: <16190-70181c803641c3dc@postgresql.org>
	<20200106051800.GL3598@paquier.xyz>
	<1234.1578291695@sss.pgh.pa.us>
	<20200106174443.3pwnmshbxrbwggrm@alap3.anarazel.de>

On Mon, Jan 06, 2020 at 09:44:43AM -0800, Andres Freund wrote:
> I think we should consider either moving this out of contrib, or fixing
> it up. test/example code is fine, but contrib gets installed by default
> for a lot of people... And yea, this isn't just about contrib/spi.

No idea about moving that out of contrib/, but here is a patch to fix
things that just moves the allocations to TopMemoryContext and removes
the system calls.
--
Michael

Attachments:

refint-alloc-fixes.patch (text/x-diff, 2.2K) download diff --git a/contrib/spi/refint.c b/contrib/spi/refint.c index adf0490f85..05595c52c3 100644 --- a/contrib/spi/refint.c +++ b/contrib/spi/refint.c @@ -12,6 +12,7 @@ #include "commands/trigger.h" #include "executor/spi.h" #include "utils/builtins.h" +#include "utils/memutils.h" #include "utils/rel.h" PG_MODULE_MAGIC; @@ -186,12 +187,13 @@ check_primary_key(PG_FUNCTION_ARGS) /* * Remember that SPI_prepare places plan in current memory context - - * so, we have to save plan in Top memory context for later use. + * so, we have to save plan in TopMemoryContext for later use. */ if (SPI_keepplan(pplan)) /* internal error */ elog(ERROR, "check_primary_key: SPI_keepplan failed"); - plan->splan = (SPIPlanPtr *) malloc(sizeof(SPIPlanPtr)); + plan->splan = (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext, + sizeof(SPIPlanPtr)); *(plan->splan) = pplan; plan->nplans = 1; } @@ -417,7 +419,8 @@ check_foreign_key(PG_FUNCTION_ARGS) char sql[8192]; char **args2 = args; - plan->splan = (SPIPlanPtr *) malloc(nrefs * sizeof(SPIPlanPtr)); + plan->splan = (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext, + nrefs * sizeof(SPIPlanPtr)); for (r = 0; r < nrefs; r++) { @@ -614,6 +617,13 @@ find_plan(char *ident, EPlan **eplan, int *nplans) { EPlan *newp; int i; + MemoryContext oldcontext; + + /* + * All allocations done for the plans need to happen in a session-safe + * context. + */ + oldcontext = MemoryContextSwitchTo(TopMemoryContext); if (*nplans > 0) { @@ -623,20 +633,24 @@ find_plan(char *ident, EPlan **eplan, int *nplans) break; } if (i != *nplans) + { + MemoryContextSwitchTo(oldcontext); return (*eplan + i); - *eplan = (EPlan *) realloc(*eplan, (i + 1) * sizeof(EPlan)); + } + *eplan = (EPlan *) repalloc(*eplan, (i + 1) * sizeof(EPlan)); newp = *eplan + i; } else { - newp = *eplan = (EPlan *) malloc(sizeof(EPlan)); + newp = *eplan = (EPlan *) palloc(sizeof(EPlan)); (*nplans) = i = 0; } - newp->ident = strdup(ident); + newp->ident = pstrdup(ident); newp->nplans = 0; newp->splan = NULL; (*nplans)++; + MemoryContextSwitchTo(oldcontext); return newp; }
signature.asc (application/pgp-signature, 832B) download

view thread (11+ messages)  latest in thread

Message-ID: <20200107012741.GD2386@paquier.xyz>
Permalink:  ../20200107012741.GD2386@paquier.xyz/
Also on:    postgresql.org/message-id/20200107012741.GD2386@paquier.xyz

 ·  · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, andres@anarazel.de, tgl@sss.pgh.pa.us, starbugs@qq.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #16190: The usage of NULL pointer in refint.c
  In-Reply-To: <20200107012741.GD2386@paquier.xyz>

* 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