Re: BUG #16801: Invalid memory access on WITH RECURSIVE with nested WITHs

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Alexander Lakhin <exclusion(at)gmail(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #16801: Invalid memory access on WITH RECURSIVE with nested WITHs
Date: 2021-02-24 15:19:23
Message-ID: 4057243.1614179963@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Michael Paquier <michael(at)paquier(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Ryan Vinzent 2021-02-24 20:18:26 Bug: Cannot insert multiple records using DEFAULT keyword for generated column
Previous Message Антон Курочкин 2021-02-24 12:22:56 Re: BUG #16894: PANIC: WAL contains references to invalid pages