COPY FROM ON_ERROR SET_NULL bypasses domain NOT NULL with partial column list

Started by SATYANARAYANA NARLAPURAM7 months ago5 messageshackers
Jump to latest
#1SATYANARAYANA NARLAPURAM
satyanarlapuram@gmail.com

HI hackers,

domain_with_constraint[] was allocated with list_length(attnumlist)
elements and indexed sequentially via foreach_current_index(), but
copyfromparse.c accesses it via attnum - 1 (physical attribute index).
With a partial column list targeting high-numbered columns, this caused
an out-of-bounds read that bypassed domain NOT NULL checks, silently
inserting NULL into NOT NULL domain columns.

Fix by allocating with num_phys_attrs and indexing by attnum - 1,
consistent with all other per-column arrays in BeginCopyFrom().

Patch is attached, and added a new test case to cover this scenario.

Repro:

CREATE DOMAIN d_notnull_int AS int NOT NULL;
CREATE TABLE t (
c1 text, c2 text, c3 text, c4 text, c5 text,
c6 text, c7 text, c8 text, c9 text,
c10 d_notnull_int
);

COPY t(c1, c10) FROM stdin WITH (on_error set_null);
hello bad
\.

SELECT c10 IS NULL FROM t;

Thanks,
Satya

Attachments:

0001-Fix-COPY-FROM-ON_ERROR-SET_NULL-OOB-read-with-partia.patchapplication/octet-stream; name=0001-Fix-COPY-FROM-ON_ERROR-SET_NULL-OOB-read-with-partia.patchDownload+44-7
#2jian he
jian.universality@gmail.com
In reply to: SATYANARAYANA NARLAPURAM (#1)
Re: COPY FROM ON_ERROR SET_NULL bypasses domain NOT NULL with partial column list

On Fri, Apr 17, 2026 at 1:09 AM SATYANARAYANA NARLAPURAM
<satyanarlapuram@gmail.com> wrote:

HI hackers,

domain_with_constraint[] was allocated with list_length(attnumlist)
elements and indexed sequentially via foreach_current_index(), but
copyfromparse.c accesses it via attnum - 1 (physical attribute index).
With a partial column list targeting high-numbered columns, this caused
an out-of-bounds read that bypassed domain NOT NULL checks, silently
inserting NULL into NOT NULL domain columns.

Fix by allocating with num_phys_attrs and indexing by attnum - 1,
consistent with all other per-column arrays in BeginCopyFrom().

Patch is attached, and added a new test case to cover this scenario.

The patch looks good to me.
I simplified the tests.

--
jian
https://www.enterprisedb.com/

Attachments:

v2-0001-Fix-COPY-FROM-ON_ERROR-SET_NULL-with-selective-column-list.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Fix-COPY-FROM-ON_ERROR-SET_NULL-with-selective-column-list.patchDownload+10-7
#3Fujii Masao
masao.fujii@gmail.com
In reply to: jian he (#2)
Re: COPY FROM ON_ERROR SET_NULL bypasses domain NOT NULL with partial column list

On Fri, Apr 17, 2026 at 12:59 PM jian he <jian.universality@gmail.com> wrote:

On Fri, Apr 17, 2026 at 1:09 AM SATYANARAYANA NARLAPURAM
<satyanarlapuram@gmail.com> wrote:

HI hackers,

domain_with_constraint[] was allocated with list_length(attnumlist)
elements and indexed sequentially via foreach_current_index(), but
copyfromparse.c accesses it via attnum - 1 (physical attribute index).
With a partial column list targeting high-numbered columns, this caused
an out-of-bounds read that bypassed domain NOT NULL checks, silently
inserting NULL into NOT NULL domain columns.

Fix by allocating with num_phys_attrs and indexing by attnum - 1,
consistent with all other per-column arrays in BeginCopyFrom().

Patch is attached, and added a new test case to cover this scenario.

The patch looks good to me.
I simplified the tests.

The patch looks good to me. Barring any objections, I will commit it.

Regards,

--
Fujii Masao

#4Chao Li
li.evan.chao@gmail.com
In reply to: Fujii Masao (#3)
Re: COPY FROM ON_ERROR SET_NULL bypasses domain NOT NULL with partial column list

On May 18, 2026, at 21:22, Fujii Masao <masao.fujii@gmail.com> wrote:

On Fri, Apr 17, 2026 at 12:59 PM jian he <jian.universality@gmail.com> wrote:

On Fri, Apr 17, 2026 at 1:09 AM SATYANARAYANA NARLAPURAM
<satyanarlapuram@gmail.com> wrote:

HI hackers,

domain_with_constraint[] was allocated with list_length(attnumlist)
elements and indexed sequentially via foreach_current_index(), but
copyfromparse.c accesses it via attnum - 1 (physical attribute index).
With a partial column list targeting high-numbered columns, this caused
an out-of-bounds read that bypassed domain NOT NULL checks, silently
inserting NULL into NOT NULL domain columns.

Fix by allocating with num_phys_attrs and indexing by attnum - 1,
consistent with all other per-column arrays in BeginCopyFrom().

Patch is attached, and added a new test case to cover this scenario.

The patch looks good to me.
I simplified the tests.

The patch looks good to me. Barring any objections, I will commit it.

Regards,

--
Fujii Masao

I also found this bug and reported it here [1]/messages/by-id/8A72720A-55AC-4D41-B9DF-5610307600E6@gmail.com. I just reviewed v2, the code change of the fix itself is exactly the same as my version in [1]/messages/by-id/8A72720A-55AC-4D41-B9DF-5610307600E6@gmail.com. The new test is slightly different from my version, but I think that also works.

[1]: /messages/by-id/8A72720A-55AC-4D41-B9DF-5610307600E6@gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#5Fujii Masao
masao.fujii@gmail.com
In reply to: Chao Li (#4)
Re: COPY FROM ON_ERROR SET_NULL bypasses domain NOT NULL with partial column list

On Tue, May 19, 2026 at 7:28 AM Chao Li <li.evan.chao@gmail.com> wrote:

I also found this bug and reported it here [1]. I just reviewed v2, the code change of the fix itself is exactly the same as my version in [1]. The new test is slightly different from my version, but I think that also works.

Thanks for the review! I've pushed the patch.

Regards,

--
Fujii Masao