Skip to content

Commit 4662ba5

Browse files
committed
Fix null-pointer-deref crash while doing COPY IN with check constraints.
In commit bf7ca15 I introduced an assumption that an RTE referenced by a whole-row Var must have a valid eref field. This is false for RTEs constructed by DoCopy, and there are other places taking similar shortcuts. Perhaps we should make all those places go through addRangeTableEntryForRelation or its siblings instead of having ad-hoc logic, but the most reliable fix seems to be to make the new code in ExecEvalWholeRowVar cope if there's no eref. We can reasonably assume that there's no need to insert column aliases if no aliases were provided. Add a regression test case covering this, and also verifying that a sane column name is in fact available in this situation. Although the known case only crashes in 9.4 and HEAD, it seems prudent to back-patch the code change to 9.2, since all the ingredients for a similar failure exist in the variant patch applied to 9.3 and 9.2. Per report from Jean-Pierre Pelletier.
1 parent d5f70a2 commit 4662ba5

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/backend/executor/execQual.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,9 @@ ExecEvalWholeRowVar(WholeRowVarExprState *wrvstate, ExprContext *econtext,
900900
* If we can't locate the RTE, assume the column names we've got are OK.
901901
* (As of this writing, the only cases where we can't locate the RTE are
902902
* in execution of trigger WHEN clauses, and then the Var will have the
903-
* trigger's relation's rowtype, so its names are fine.)
903+
* trigger's relation's rowtype, so its names are fine.) Also, if the
904+
* creator of the RTE didn't bother to fill in an eref field, assume our
905+
* column names are OK. (This happens in COPY, and perhaps other places.)
904906
*/
905907
if (variable->vartype == RECORDOID &&
906908
econtext->ecxt_estate &&
@@ -909,7 +911,8 @@ ExecEvalWholeRowVar(WholeRowVarExprState *wrvstate, ExprContext *econtext,
909911
RangeTblEntry *rte = rt_fetch(variable->varno,
910912
econtext->ecxt_estate->es_range_table);
911913

912-
ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
914+
if (rte->eref)
915+
ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
913916
}
914917

915918
/* Bless the tupdesc if needed, and save it in the execution state */

src/test/regress/expected/copy2.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,41 @@ SELECT * FROM vistest;
382382
e
383383
(2 rows)
384384

385+
-- test case with whole-row Var in a check constraint
386+
create table check_con_tbl (f1 int);
387+
create function check_con_function(check_con_tbl) returns bool as $$
388+
begin
389+
raise notice 'input = %', row_to_json($1);
390+
return $1.f1 > 0;
391+
end $$ language plpgsql immutable;
392+
alter table check_con_tbl add check (check_con_function(check_con_tbl.*));
393+
\d+ check_con_tbl
394+
Table "public.check_con_tbl"
395+
Column | Type | Modifiers | Storage | Stats target | Description
396+
--------+---------+-----------+---------+--------------+-------------
397+
f1 | integer | | plain | |
398+
Check constraints:
399+
"check_con_tbl_check" CHECK (check_con_function(check_con_tbl.*))
400+
Has OIDs: no
401+
402+
copy check_con_tbl from stdin;
403+
NOTICE: input = {"f1":1}
404+
CONTEXT: COPY check_con_tbl, line 1: "1"
405+
NOTICE: input = {"f1":null}
406+
CONTEXT: COPY check_con_tbl, line 2: "\N"
407+
copy check_con_tbl from stdin;
408+
NOTICE: input = {"f1":0}
409+
CONTEXT: COPY check_con_tbl, line 1: "0"
410+
ERROR: new row for relation "check_con_tbl" violates check constraint "check_con_tbl_check"
411+
DETAIL: Failing row contains (0).
412+
CONTEXT: COPY check_con_tbl, line 1: "0"
413+
select * from check_con_tbl;
414+
f1
415+
----
416+
1
417+
418+
(2 rows)
419+
385420
DROP TABLE vistest;
386421
DROP FUNCTION truncate_in_subxact();
387422
DROP TABLE x, y;

src/test/regress/sql/copy2.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ e
270270
SELECT * FROM vistest;
271271
COMMIT;
272272
SELECT * FROM vistest;
273+
274+
-- test case with whole-row Var in a check constraint
275+
create table check_con_tbl (f1 int);
276+
create function check_con_function(check_con_tbl) returns bool as $$
277+
begin
278+
raise notice 'input = %', row_to_json($1);
279+
return $1.f1 > 0;
280+
end $$ language plpgsql immutable;
281+
alter table check_con_tbl add check (check_con_function(check_con_tbl.*));
282+
\d+ check_con_tbl
283+
copy check_con_tbl from stdin;
284+
1
285+
\N
286+
\.
287+
copy check_con_tbl from stdin;
288+
0
289+
\.
290+
select * from check_con_tbl;
291+
273292
DROP TABLE vistest;
274293
DROP FUNCTION truncate_in_subxact();
275294
DROP TABLE x, y;

0 commit comments

Comments
 (0)