Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHNUI-00000002yGI-3TfT for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 15:35:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xHNUH-00000009n5l-3dxA for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Oct 2026 15:35:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHMz7-00000009Uwi-00zD for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:03:09 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHMz4-00000002AOv-3cN6 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:03:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=9KEZFp1d/ErV2SQZ9iBNcSirWoU5Fe7JK7/UFioQWcw=; b=pMFx85gF5Pz4YQdcValRgCBzGv 8L1XBomghZO5jdt/uie1eImLEa4anfxEJGbWClPDQjG0PSoliakpud0+j+xZcT2oE80F4MmXMnM1z 9iARZH3/vGGixDelV2H+Or+yQufHGpt9/kniaJnToVF4kcxki47ACNuyTLaOnlG0p2YN5OZTqbLg+ 5N1z21fzomgB24tEKHZLxxUtGuk3wS7KBpaOTF0d6NgZihPkIg6HOTz7PXh/WgecZEYKTWgLKL2Dt sIcn4yvoKr0u1WmFZTiKfPhS6HkcL19n4n65ux3QNyCk4b4zKkzvVwHdaR6zH8cKC10jhfUuv/vi6 ECAg13cg==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xHMz3-00000006Isj-1Fz6 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:03:06 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xHMz1-00000001iAU-2T28 for pgsql-bugs@lists.postgresql.org; Thu, 15 Oct 2026 15:03:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19762: ManualBug1 tcn: NULL pointer dereference in triggered_change_notification() via strcpy_quoted To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 1217816127@qq.com Reply-To: 1217816127@qq.com, pgsql-bugs@lists.postgresql.org Date: Thu, 15 Oct 2026 15:02:57 +0000 Message-ID: <19762-04e5ab8e5459b958@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19762 Logged by: Yuelin Wang Email address: 1217816127@qq.com PostgreSQL version: 19rc1 Operating system: Linux (Ubuntu 24.04, x86_64) Description: =20 ### What happens? In `contrib/tcn/tcn.c` at line 165, the function `triggered_change_notification()` calls `SPI_getvalue(trigtuple, tupdesc, colno)` and passes the return value directly to `strcpy_quoted()` without checking whether it is NULL: ```c /* contrib/tcn/tcn.c:165 */ strcpy_quoted(&payload, SPI_getvalue(trigtuple, tupdesc, colno), '\''); ``` `SPI_getvalue()` returns NULL when the requested column value is SQL NULL (it sets `isnull =3D true` and returns NULL from `heap_getattr`). The function `strcpy_quoted()` defined at line 36 immediately dereferences the pointer at line 39: ```c static void strcpy_quoted(StringInfo r, const char *s, const char q) { appendStringInfoCharMacro(r, q); while (*s) /* line 39: unconditional dereference */ ... } ``` The violated invariant is: any caller of `strcpy_quoted` must guarantee the string pointer `s` is non-NULL before passing it. The Entry61 fix in `refint.c` showed the identical invariant being violated by `quote_literal_cstr(nv)` where `nv` could be NULL. Here the same class of bug appears: a nullable value returned by `SPI_getvalue` is passed to a string-consuming function that dereferences the pointer unconditionally. The column `colno` comes from `index->indkey.values[i]` for the primary key index. Under normal DDL, primary key columns are NOT NULL, which prevents `SPI_getvalue` from returning NULL. However, the code contains no guard against NULL and will crash if `SPI_getvalue` returns NULL for any reason (for example when a superuser bypasses the NOT NULL constraint by directly updating `pg_index.indisprimary` on an index whose key columns lack a NOT NULL constraint, or if a future code path makes the invariant reachable through other means). The correct fix mirrors Entry61: replace the bare `SPI_getvalue(...)` argument with a ternary expression that substitutes the unquoted SQL keyword `NULL` when the value is NULL, exactly as `dblink.c` already does at lines 2197-2203. ### To Reproduce Start a fresh PostgreSQL ASAN instance and run: ```sql CREATE EXTENSION tcn; CREATE TABLE tcn_test (id int, data text); CREATE UNIQUE INDEX tcn_test_uniq ON tcn_test (id); SET allow_system_table_mods =3D on; UPDATE pg_index SET indisprimary =3D true WHERE indexrelid =3D 'tcn_test_uniq'::regclass; CREATE TRIGGER tcn_trig AFTER INSERT OR UPDATE OR DELETE ON tcn_test FOR EACH ROW EXECUTE FUNCTION triggered_change_notification(); INSERT INTO tcn_test VALUES (NULL, 'crash test'); ``` Trigger output: ``` CREATE EXTENSION CREATE TABLE CREATE INDEX SET UPDATE 1 CREATE TRIGGER server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. connection to server was lost =3D=3D=3D pg.log (relevant lines) =3D=3D=3D AddressSanitizer:DEADLYSIGNAL [postmaster] LOG: client backend (PID 2605793) was terminated by signal 6: Aborted [postmaster] DETAIL: Failed process was running: INSERT INTO tcn_test VALUES (NULL, 'crash test'); [postmaster] LOG: terminating any other active server processes [postmaster] LOG: all server processes terminated (reinitializing) =3D=3D=3D ASAN report =3D=3D=3D =3D=3D2605793=3D=3DERROR: AddressSanitizer: SEGV on unknown address 0x00000= 0000000 (pc 0x7f1ed965ed8a bp 0x7fffdfe2c0b0 sp 0x7fffdfe2c090 T0) =3D=3D2605793=3D=3DThe signal is caused by a READ memory access. =3D=3D2605793=3D=3DHint: address points to the zero page. #0 0x7f1ed965ed8a in strcpy_quoted /tmp/tcn_v2_FUbB/tcn.c:39 #1 0x7f1ed965ff08 in triggered_change_notification /tmp/tcn_v2_FUbB/tcn.c:165 #2 0x60a8368cbd69 in ExecCallTriggerFunc trigger.c:2407 #3 0x60a8368d5875 in AfterTriggerExecute trigger.c:4620 #4 0x60a8368d65fd in afterTriggerInvokeEvents trigger.c:4861 #5 0x60a8368d7af9 in AfterTriggerEndQuery trigger.c:5247 #6 0x60a83697c0ec in standard_ExecutorFinish execMain.c:454 ... SUMMARY: AddressSanitizer: SEGV /tmp/tcn_v2_FUbB/tcn.c:39 in strcpy_quoted =3D=3D2605793=3D=3DABORTING ``` ### Environment - OS: Ubuntu 24.04.2 LTS, x86_64 (Linux 6.8.0-55-generic) - PostgreSQL Version: PostgreSQL 20devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit - Client: psql, ASAN instrumented build (built from source with -fsanitize=3Daddress) - Hardware: N/A ### Identity Disclosure - Full Name: Yuelin - Affiliation: Individual