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
This commit is contained in:
Tom Lane 2019-08-15 20:04:19 -04:00
parent fe9b7b2fe5
commit 1ced082b95
2 changed files with 105 additions and 0 deletions

View File

@ -144,6 +144,76 @@ select * from trigtest;
----+----
(0 rows)
-- Also check what happens when such a trigger runs before or after others
create function f1_times_10() returns trigger as
$$ begin new.f1 := new.f1 * 10; return new; end $$ language plpgsql;
create trigger trigger_alpha
before insert or update on trigtest
for each row execute procedure f1_times_10();
insert into trigtest values(1, 'foo');
select * from trigtest;
f1 | f2
----+-----
10 | foo
(1 row)
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
f1 | f2
----+-----
10 | foo
(1 row)
delete from trigtest;
select * from trigtest;
f1 | f2
----+----
(0 rows)
create trigger trigger_zed
before insert or update on trigtest
for each row execute procedure f1_times_10();
insert into trigtest values(1, 'foo');
select * from trigtest;
f1 | f2
-----+-----
100 | foo
(1 row)
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
f1 | f2
------+-----
1000 | foo
(1 row)
delete from trigtest;
select * from trigtest;
f1 | f2
----+----
(0 rows)
drop trigger trigger_alpha on trigtest;
insert into trigtest values(1, 'foo');
select * from trigtest;
f1 | f2
----+-----
10 | foo
(1 row)
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
f1 | f2
-----+-----
100 | foo
(1 row)
delete from trigtest;
select * from trigtest;
f1 | f2
----+----
(0 rows)
drop table trigtest;
create sequence ttdummy_seq increment 10 start 0 minvalue 0;
create table tttest (

View File

@ -117,6 +117,41 @@ select * from trigtest;
delete from trigtest;
select * from trigtest;
-- Also check what happens when such a trigger runs before or after others
create function f1_times_10() returns trigger as
$$ begin new.f1 := new.f1 * 10; return new; end $$ language plpgsql;
create trigger trigger_alpha
before insert or update on trigtest
for each row execute procedure f1_times_10();
insert into trigtest values(1, 'foo');
select * from trigtest;
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
delete from trigtest;
select * from trigtest;
create trigger trigger_zed
before insert or update on trigtest
for each row execute procedure f1_times_10();
insert into trigtest values(1, 'foo');
select * from trigtest;
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
delete from trigtest;
select * from trigtest;
drop trigger trigger_alpha on trigtest;
insert into trigtest values(1, 'foo');
select * from trigtest;
update trigtest set f2 = f2 || 'bar';
select * from trigtest;
delete from trigtest;
select * from trigtest;
drop table trigtest;
create sequence ttdummy_seq increment 10 start 0 minvalue 0;