Fix stale attribute pointer in COPY FROM error context.

Previously, cur_attname and cur_attval were left set when ON_ERROR was
'ignore' or 'set_null'. As a result, if an error was raised while
processing the next row, the COPY error context callback could
dereference a pointer that no longer belonged to it. cur_attval isn't
a copy of the field value; it points into attribute_buf, which the
next row resets and, for a longer line, repallocs. The callback
measures the value with strlen() before applying the display limit, so
the limit bounds what we print but not what we read. So depending on
what has reused the allocation, the CONTEXT line reports a value from
the previous row or bytes belonging to something else entirely.

file_fdw reaches the same code through its soft-error retry loop, so
on v18 and later, a SELECT or ANALYZE on such a foreign table hits
this too.

Fix this by making sure to clear both fields before processing the
next column data.

This also corrects the context reported for a REJECT_LIMIT overrun,
which is a statement-level error but was picking up the column and
value left behind by the last skipped row.

Backpatch to v17, where ON_ERROR was introduced.

Reported-by: 안지용 <[email protected]>
Backpatch-through: 17
Security: CVE-2026-116310

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8b3babb6e1627928fb0111f48e0c8a9435c7633e
Author: Masahiko Sawada <[email protected]>

Modified Files
--------------
contrib/file_fdw/expected/file_fdw.out |  2 +-
src/backend/commands/copyfromparse.c   | 10 ++++++++--
src/test/regress/expected/copy2.out    | 13 ++++++++++++-
src/test/regress/sql/copy2.sql         | 17 +++++++++++++++++
4 files changed, 38 insertions(+), 4 deletions(-)

Reply via email to