Thread: pgsql: Fix use-of-already-freed-memory problem in EvalPlanQual processi

pgsql: Fix use-of-already-freed-memory problem in EvalPlanQual processi

From
Tom Lane
Date:
Fix use-of-already-freed-memory problem in EvalPlanQual processing.

Up to now, the "child" executor state trees generated for EvalPlanQual
rechecks have simply shared the ResultRelInfo arrays used for the original
execution tree.  However, this leads to dangling-pointer problems, because
ExecInitModifyTable() is all too willing to scribble on some fields of the
ResultRelInfo(s) even when it's being run in one of those child trees.
This trashes those fields from the perspective of the parent tree, because
even if the generated subtree is logically identical to what was in use in
the parent, it's in a memory context that will go away when we're done
with the child state tree.

We do however want to share information in the direction from the parent
down to the children; in particular, fields such as es_instrument *must*
be shared or we'll lose the stats arising from execution of the children.
So the simplest fix is to make a copy of the parent's ResultRelInfo array,
but not copy any fields back at end of child execution.

Per report from Manuel Kniep.  The added isolation test is based on his
example.  In an unpatched memory-clobber-enabled build it will reliably
fail with "ctid is NULL" errors in all branches back to 9.1, as a
consequence of junkfilter->jf_junkAttNo being overwritten with $7f7f.
This test cannot be run as-is before that for lack of WITH syntax; but
I have no doubt that some variant of this problem can arise in older
branches, so apply the code change all the way back.

Branch
------
REL9_2_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/0acb32efb7327f9e1abb13495c9db2048122bf80

Modified Files
--------------
src/backend/executor/execMain.c                |   24 +++++++++++++++---
src/test/isolation/expected/eval-plan-qual.out |   32 ++++++++++++++++++++++++
src/test/isolation/specs/eval-plan-qual.spec   |   15 ++++++++---
3 files changed, 65 insertions(+), 6 deletions(-)