Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b8w0J-0000Rz-O0 for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jun 2016 20:47:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b8w0I-00042T-Hh for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jun 2016 20:47:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8w0I-00042M-47 for pgsql-bugs@postgresql.org; Fri, 03 Jun 2016 20:47:38 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1b8w0E-00036S-PQ for pgsql-bugs@postgresql.org; Fri, 03 Jun 2016 20:47:37 +0000 Received: from localhost ([127.0.0.1]:48968 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1b8w0C-000PbR-4m; Fri, 03 Jun 2016 20:47:32 +0000 From: Andrew Gierth To: Tom Lane Cc: pgsql-bugs@postgresql.org, edward.greve@gmail.com Subject: Re: BUG #14174: Expanded-datum bug accessing freed memory In-Reply-To: <1664.1464974722@sss.pgh.pa.us> (Tom Lane's message of "Fri, 03 Jun 2016 13:25:22 -0400") Message-ID: <87bn3i2f9y.fsf@news-spur.riddles.org.uk> References: <20160603124628.9932.41279@wrigleys.postgresql.org> <1664.1464974722@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Fri, 03 Jun 2016 21:47:31 +0100 MIME-Version: 1.0 Content-Type: text/plain X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org [CC'ing in the original reporter, edward.greve at gmail.com] >>>>> "Tom" == Tom Lane writes: Tom> The core of the problem here is that afn1() is returning a Tom> read-write pointer to the expanded object holding "r", and then Tom> when that's passed to afn2(), it supposes that it can take Tom> ownership of it as a read-write local variable; which means the Tom> value gets destroyed when afn2() exits. That'd be all right Tom> except there's another reference to "a" still to be evaluated :-( Yeah, I figured it was something like that, but this is the first time I've had to look at the expanded object stuff. Tom> I am thinking maybe we need to have ExecProject do Tom> MakeExpandedObjectReadOnly on each result, rather than assuming Tom> that SubqueryScan is the place for that. This would slightly Tom> increase the general overhead attributable to the expanded-object Tom> feature, which is unfortunate, but right now it's not clear that Tom> anything less is safe. I concur. Tom> Making Result nodes do that would fix this particular instance but Tom> there are plenty of other node types that might appear at the top Tom> of a sub-select. The Result node here is certainly an artifact of the testcase construction; the original report (which featured about 300 lines of view and function definitions, some of them with additional subselects, nested in various ways) would probably have had an Agg node at the relevant spot, and could conceivably have had any projecting node type AFAIK. Tom> A possible future improvement is to teach the planner to detect Tom> which variables are actually multiply referenced, and force Tom> read-only-ness for only those values. But that's clearly not Tom> something that would be reasonable to back-patch into 9.5, or even Tom> 9.6 at this point. Clearly. -- Andrew (irc:RhodiumToad) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs