postgres.git / summary / log / commit / refs

commit    c88c71a4b52a514cf9e9582c2d657e0fc6d4ce97
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Fri Nov 06 00:18:40 2026 +0000
First released in: REL_17_STABLE (reverted before release), REL_18_STABLE (reverted before release), REL_19_STABLE (reverted before release)
Reverted by: 134dc506ee9b (in 17.12), 9b71ffa061d7 (in 18.7), 160f31522793 (in 19.0), c07545dfe1a9

    Fix use-after-free in COPY (ON_ERROR)
    
    When ON_ERROR handled a soft error, some of the contents set for the
    error context callback could remain set and pointing at areas that could
    be reset, leading to some use-after-frees depending on the input data
    provided through COPY.
    
    These contents are now reset at the beginning of NextCopyFrom(), before
    processing the next tuple.
    
    SET_NULL is affected down to v19, IGNORE down to v17.
    
    Author: Gowdham M <gowdham.m@zohocorp.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Reviewed-by: Shihao Zhong <zhong950419@gmail.com>
    Discussion: https://postgr.es/m/1a181ff6be8.6d14feb261368.4036709598649280477@zohocorp.com
    Backpatch-through: 17


src/backend/commands/copyfromparse.c | 4 ++++ src/test/regress/expected/copy2.out | 4 ++++ src/test/regress/sql/copy2.sql | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c index fedaf78ba5e..129f1a1aff8 100644 --- a/src/backend/commands/copyfromparse.c +++ b/src/backend/commands/copyfromparse.c @@ -886,6 +886,10 @@ NextCopyFrom(CopyFromState cstate, ExprContext *econtext, MemSet(nulls, true, num_phys_attrs * sizeof(bool)); MemSet(cstate->defaults, false, num_phys_attrs * sizeof(bool)); + /* Clear the current attribute for error messages. */ + cstate->cur_attname = NULL; + cstate->cur_attval = NULL; + /* Get one row from source */ if (!cstate->routine->CopyFromOneRow(cstate, econtext, values, nulls)) return false; diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index e5f9540292f..148399b75c6 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -808,6 +808,10 @@ CONTEXT: COPY hard_err, line 1, column foo: "1" COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: missing data for column "k" CONTEXT: COPY check_ign_err, line 1: "1 {1}" +-- test extra data after a skipped row: should fail, reporting the line +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" -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); ERROR: extra data after last expected column diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index b2cfe6523a6..bbc5dd7e1eb 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -610,6 +610,12 @@ COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} \. +-- test extra data after a skipped row: should fail, reporting the line +COPY check_ign_err FROM STDIN WITH (on_error ignore); +1 {1} a +2 {2} 2 extra +\. + -- test extra data: should fail COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc [parent: dcf112bd4e4c]