pgsql: Prevent possible double-free when update trigger returns old tup

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Prevent possible double-free when update trigger returns old tup
Date: 2019-08-16 00:05:03
Message-ID: E1hyPjr-00062y-As@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Prevent possible double-free when update trigger returns old tuple.

This is a variant of the problem fixed in commit 25b692568, which
unfortunately we failed to detect at the time. If an update trigger
returns the "old" tuple, as it's entitled to do, then a subsequent
iteration of the loop in ExecBRUpdateTriggers would have "oldtuple"
equal to "trigtuple" and would fail to notice that it shouldn't
free that.

In addition to fixing the code, extend the test case added by
25b692568 so that it covers multiple-trigger-iterations cases.

This problem does not manifest in v12/HEAD, as a result of the
relevant code having been largely rewritten for slotification.
However, include the test case into v12/HEAD anyway, since this
is clearly an area that someone could break again in future.

Per report from Piotr Gabriel Kosinski. Back-patch into all
supported branches, since the bug seems quite old.

Diagnosis and code fix by Thomas Munro, test case by me.

Discussion: https://postgr.es/m/CAFMLSdP0rd7LqC3j-H6Fh51FYSt5A10DDh-3=W4PPc4LLUQ8YQ@mail.gmail.com

Branch
------
REL9_5_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cb0c79ae62316fe367f207a5ed58a76629070534

Modified Files
--------------
src/backend/commands/trigger.c | 4 +-
src/test/regress/expected/triggers.out | 70 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/triggers.sql | 35 +++++++++++++++++
3 files changed, 108 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Etsuro Fujita 2019-08-16 05:39:03 pgsql: Remove useless bms_free() calls in build_child_join_rel().
Previous Message Tom Lane 2019-08-15 19:22:07 pgsql: Fix plpgsql to re-look-up composite type names at need.