postgres.git / summary / log / commit / refs

commit    8b3babb6e1627928fb0111f48e0c8a9435c7633e
Author:   Masahiko Sawada <msawada@postgresql.org>
Commit:   Noah Misch <noah@leadboat.com>
Date:     Mon Nov 09 14:20:35 2026 +0000
First released in: 17.12, 18.7, 19.0

    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: 안지용 <jfdg1y76@gmail.com>
    Backpatch-through: 17
    Security: CVE-2026-116310


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(-) diff --git a/contrib/file_fdw/expected/file_fdw.out b/contrib/file_fdw/expected/file_fdw.out index bd502c98833..f354a4aae39 100644 --- a/contrib/file_fdw/expected/file_fdw.out +++ b/contrib/file_fdw/expected/file_fdw.out @@ -276,7 +276,7 @@ SELECT * FROM agg_bad; ALTER FOREIGN TABLE agg_bad OPTIONS (ADD reject_limit '1'); -- ERROR SELECT * FROM agg_bad; ERROR: skipped more than REJECT_LIMIT (1) rows due to data type incompatibility -CONTEXT: COPY agg_bad, line 5, column b: "bbb" +CONTEXT: COPY agg_bad, line 5: "1;@bbb@" ALTER FOREIGN TABLE agg_bad OPTIONS (SET reject_limit '2'); SELECT * FROM agg_bad; a | b diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index af45da46557..d33594162b5 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -1170,9 +1170,15 @@ CopyFromTextLikeOneRow(CopyFromState cstate, ExprContext *econtext, } if (cstate->opts.on_error == COPY_ON_ERROR_IGNORE) + { + /* The return below skips the clear at the bottom of the loop */ + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + return true; - else if (cstate->opts.on_error == COPY_ON_ERROR_SET_NULL) - continue; + } + + /* SET_NULL falls through to the clear at the bottom of the loop */ } cstate->cur_attname = NULL; diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index df6fcd91799..15839f7f933 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -887,10 +887,21 @@ CONTEXT: COPY check_ign_err, line 1: "1 {1}" COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: extra data after last expected column CONTEXT: COPY check_ign_err, line 1: "1 {1} 3 abc" +-- a hard error in the row following one skipped by ON_ERROR must report the +-- line correctly. +COPY check_ign_err FROM STDIN WITH (on_error ignore); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2 {2} 2 extra" +COPY check_ign_err FROM STDIN WITH (format csv, on_error ignore); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2,{2},2,extra" +COPY check_ign_err FROM STDIN WITH (on_error set_null); +ERROR: extra data after last expected column +CONTEXT: COPY check_ign_err, line 2: "2 {2} 2 extra" -- tests for reject_limit option COPY check_ign_err FROM STDIN WITH (on_error ignore, reject_limit 3); ERROR: skipped more than REJECT_LIMIT (3) rows due to data type incompatibility -CONTEXT: COPY check_ign_err, line 5, column n: "" +CONTEXT: COPY check_ign_err, line 5: "" COPY check_ign_err FROM STDIN WITH (on_error ignore, reject_limit 4); NOTICE: 4 rows were skipped due to data type incompatibility -- clean up diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index ec6f5e8cfdc..ef587fa1dd2 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -674,6 +674,23 @@ COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc \. +-- a hard error in the row following one skipped by ON_ERROR must report the +-- line correctly. +COPY check_ign_err FROM STDIN WITH (on_error ignore); +1 {1} stale_ignore_text +2 {2} 2 extra +\. + +COPY check_ign_err FROM STDIN WITH (format csv, on_error ignore); +1,{1},stale_ignore_csv +2,{2},2,extra +\. + +COPY check_ign_err FROM STDIN WITH (on_error set_null); +1 {1} stale_setnull_text +2 {2} 2 extra +\. + -- tests for reject_limit option COPY check_ign_err FROM STDIN WITH (on_error ignore, reject_limit 3); 6 {6} 6 [parent: 0bf1ac6bef44]