Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iodf6-0002Fl-Lx for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jan 2020 01:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iodf3-0006fN-Vf for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jan 2020 01:27:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iodf2-0006fG-DD for pgsql-bugs@lists.postgresql.org; Tue, 07 Jan 2020 01:27:57 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iodeu-0003TB-AW for pgsql-bugs@lists.postgresql.org; Tue, 07 Jan 2020 01:27:55 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 69519529; Mon, 6 Jan 2020 20:27:46 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 06 Jan 2020 20:27:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=6md5BNAOC8vRUouj6ylp6agtljb 2Evcayh5Rrq8k1bQ=; b=EAC4YuQMnU65vI9o+RRbQlABfk2eWP3wDHrf3yPEsvx uZ4Gw70uLqSax2S/Pn1ffKUs99Gi6c7jVg7zAg38jYPQC2Sz6C/z1a22no6txi4S rOh9gz8Y8XT9Z7bVxXm3R1bAOl0TdMn8CIiBTLVjIpnr0Tb6gcBFzjzCSp3OTYVx FxgnN4SEiPD9NbiEZJqYqUqcEgTUUpXSMnNC8nV+094SIN8iEADCX9iDb1kW4Ah2 hzWaefpUQYK+6JQjjCraUXZK1UJnJAJM/LIGbwEix64q24bTXfn1xbrlUt19No5D NrePgDI15xGrgtU5D8QU97wjYPUj6q5TFnrvj4PtFCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=6md5BN AOC8vRUouj6ylp6agtljb2Evcayh5Rrq8k1bQ=; b=Crpr74U7DmKKY0oY6IZX9D ubYMqOGUTZbUTpza6GPMYniAruCFElzx4qUAaJ9L4ZdH2jDTw1fuFCKIUDXkZzp2 VGu86adqZHZ2pVolLU/7lBvWb4XhfQ5f2SelGhIpWYgA0PkAimvzvXuMhinAV3j6 Xtnwn+7ET58p1xHcAWevbeRmiLp3adcym98J6XhfcsmjQ5Dhybien+EpqTZKdyMK Tas/Pgi4ZMHqurrogJm9WaoaS17P4ashFPvzj7fSdFpd+Fvmp+Sb4N0qXagY9qo1 jYy+CIRVFo/NN5NdPfXzCGYaOaOowj+zGom9wAMvgB71GrvEflIEecc9Zh8S4LVg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdehuddgfeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucfkphepudduuddruddtvddrudektddrudekheenucfrrghr rghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihiienucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id BAD0B80061; Mon, 6 Jan 2020 20:27:43 -0500 (EST) Date: Tue, 7 Jan 2020 10:27:41 +0900 From: Michael Paquier To: Andres Freund Cc: Tom Lane , starbugs@qq.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16190: The usage of NULL pointer in refint.c Message-ID: <20200107012741.GD2386@paquier.xyz> References: <16190-70181c803641c3dc@postgresql.org> <20200106051800.GL3598@paquier.xyz> <1234.1578291695@sss.pgh.pa.us> <20200106174443.3pwnmshbxrbwggrm@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="IU5/I01NYhRvwH70" Content-Disposition: inline In-Reply-To: <20200106174443.3pwnmshbxrbwggrm@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --IU5/I01NYhRvwH70 Content-Type: multipart/mixed; boundary="ZJcv+A0YCCLh2VIg" Content-Disposition: inline --ZJcv+A0YCCLh2VIg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --ZJcv+A0YCCLh2VIg Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="refint-alloc-fixes.patch" Content-Transfer-Encoding: quoted-printable 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" =20 PG_MODULE_MAGIC; @@ -186,12 +187,13 @@ check_primary_key(PG_FUNCTION_ARGS) =20 /* * 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 =3D (SPIPlanPtr *) malloc(sizeof(SPIPlanPtr)); + plan->splan =3D (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext, + sizeof(SPIPlanPtr)); *(plan->splan) =3D pplan; plan->nplans =3D 1; } @@ -417,7 +419,8 @@ check_foreign_key(PG_FUNCTION_ARGS) char sql[8192]; char **args2 =3D args; =20 - plan->splan =3D (SPIPlanPtr *) malloc(nrefs * sizeof(SPIPlanPtr)); + plan->splan =3D (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext, + nrefs * sizeof(SPIPlanPtr)); =20 for (r =3D 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 =3D MemoryContextSwitchTo(TopMemoryContext); =20 if (*nplans > 0) { @@ -623,20 +633,24 @@ find_plan(char *ident, EPlan **eplan, int *nplans) break; } if (i !=3D *nplans) + { + MemoryContextSwitchTo(oldcontext); return (*eplan + i); - *eplan =3D (EPlan *) realloc(*eplan, (i + 1) * sizeof(EPlan)); + } + *eplan =3D (EPlan *) repalloc(*eplan, (i + 1) * sizeof(EPlan)); newp =3D *eplan + i; } else { - newp =3D *eplan =3D (EPlan *) malloc(sizeof(EPlan)); + newp =3D *eplan =3D (EPlan *) palloc(sizeof(EPlan)); (*nplans) =3D i =3D 0; } =20 - newp->ident =3D strdup(ident); + newp->ident =3D pstrdup(ident); newp->nplans =3D 0; newp->splan =3D NULL; (*nplans)++; =20 + MemoryContextSwitchTo(oldcontext); return newp; } --ZJcv+A0YCCLh2VIg-- --IU5/I01NYhRvwH70 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl4T3o0ACgkQnvQgOdby QH1CLA/+IpLnl0DOXED0fAOETR9lSeyGVgkON3jPEBnJ+33Th2ord8nkbQCbkbFW lno6wL1UO31bk5UO18fN0lh/cS1DJqNMbaBazUgI0i9dTdHSHH2YuS5BcMjN0XQ8 JNeCtlQ7VK7k3YBxDXFqqm8TD2XH3Y+Ccvd8//7GjqBoAhYpex9OzVGA9C2NMamg ZYDt/PrRp2IL8vDerLqKeufv+MXbLd7H2+9qyfhEhRBhw2Z2IGuDqWUbvjokwB3w PcjJIPaFESu1yGPsLAcHC8z/3Xo5+TEn1lsNvOfBL4f712FPIMvqZCOoAQPH7rIL /0IVgSCScPK8w0COOwb2Fqo1sS67r7MoOxR6fzdV6+yArINnSKWu7vEG9MzpxZ5B bJf5JEiaDl2Hlfmct6Na6CAlRyMezEssQcbpoDDOZM6JGCHhi+mo2dnXFTrcj9CB PBX/j7CkF+piirk6ksXgBYT49PrYGQUGMC+VCXJRuBnocwqgxdSqu7I0OS0N3Eqg FrlfIdGtCC3no02mc056027VY7x3dCJuRtK1166riofAthYNYrfh4drNM7g0Jdm0 kzva3qHbXuqkG9JqqigijPGshzAQRjyJ2JuWSLAV4u3q7zH6l9HKkLZo2wSxFiZ4 BBDQlZ14rwGFw/pxyIoFt2FpgopyRlzBG8k68BM6kUu6+bS+n1M= =XhA6 -----END PGP SIGNATURE----- --IU5/I01NYhRvwH70--