From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Paquier <michael@paquier.xyz>
Cc: Alexander Lakhin <exclusion@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16801: Invalid memory access on WITH RECURSIVE with nested WITHs
Date: Wed, 24 Feb 2021 10:19:23 -0500
Message-ID: <4057243.1614179963@sss.pgh.pa.us> (raw)
In-Reply-To: <YDXeLBqjDeQQPGoO@paquier.xyz>
References: <16801-393c7922143eaa4d@postgresql.org>
<X/FTDocQD3D7+aIJ@paquier.xyz>
<eb358b3f-8272-d0a9-6645-983e7ad2bec4@gmail.com>
<YDXeLBqjDeQQPGoO@paquier.xyz>
Michael Paquier <michael@paquier.xyz> writes:
> So attached is a patch to take care of this, with a regression test
> based on what has been sent upthread. This solves the issue for me.
Surely that breaks things entirely (if it doesn't, then we are badly
under-testing this area). A nil list is just a null pointer, so
appending to "new_cte_list" later isn't going to affect what was
previously put into the innerwiths list.
I haven't tested, but I think a more correct fix would be
- ListCell *cell1;
cstate->innerwiths = lcons(NIL, cstate->innerwiths);
- cell1 = list_head(cstate->innerwiths);
foreach(lc, stmt->withClause->ctes)
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
+ ListCell *cell1;
(void) makeDependencyGraphWalker(cte->ctequery, cstate);
+ /* note innerwiths list can change during recursion */
+ cell1 = list_head(cstate->innerwiths);
lfirst(cell1) = lappend((List *) lfirst(cell1), cte);
}
ie, recompute the "cell1" pointer each time it's needed instead of
assuming that the original value is good throughout the loop.
regards, tom lane
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: tgl@sss.pgh.pa.us, michael@paquier.xyz, exclusion@gmail.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16801: Invalid memory access on WITH RECURSIVE with nested WITHs
In-Reply-To: <4057243.1614179963@sss.pgh.pa.us>
* 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